Nginx 子域只能在本地访问

Nginx subdomain only accessable locally

我正在尝试在我的 nginx 配置上设置一个 nginx 子域。 但是我无法 access/browse 远程访问我的新子域。 尝试远程访问此子域时未注册 errors/access 日志。服务器托管在 Linode 上。

当前设置: 默认 nginx 配置 -> 未编辑

主站点配置:

server {
        listen  443 ssl;
        server_name     example.be www.example.be;
        root    /var/www/example;
        index   index.html
        gzip off;

        location ~ \.php$ {
               include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
               fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        }



    ssl_certificate /etc/letsencrypt/live/example.be/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.be/privkey.pem; # managed by Certbot

}
server {
    if ($host = www.example.be) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    if ($host = example.be) {
        return 301 https://$host$request_uri;
    } # managed by Certbot



        listen  80;
        server_name     www.example.be;
    return 404; # managed by Certbot

      location ~ \.php$ {
               include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
               fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        }


}

子域配置:

server {
        listen 80;
        listen [::]:80;

        root /var/www/sub.example.be;

        index index.html index.htm index.nginx-debian.html index.php;

        server_name sub.example.be;

        location / {
                try_files $uri $uri/ =404;
                auth_basic "Restricted Area";
                auth_basic_user_file /var/www/sub.example.be/.htpasswd;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
                auth_basic "Restricted Area";
                auth_basic_user_file /var/www/sub.example.be/.htpasswd;
        }

        location ~ /\.ht {
               deny all;
        }
}

我目前正在为主站点 DNS 等使用 cloudflare。 这是几个月前设置的并且运行良好。

我只是更改服务器上的主机文件(用于本地测试)和家用电脑上的主机文件(用于远程测试)。

然而,我的 curl 命令无法正常工作:

服务器:curl sub.example.be/index.html

<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.14.2</center>
</body>
</html>

我猜这是正确的行为。

但是当我在家用电脑上执行此操作时:curl -v sub.example.be/index.html

*   Trying [IP]...
* TCP_NODELAY set
* connect to [IP] port 80 failed: Timed out
* Failed to connect to sub.example.be port 80: Timed out
* Closing connection 0
curl: (7) Failed to connect to sub.example.be port 80: Timed out

哦,是的,我可以远程访问我的主域。

我的主网站显然将所有流量重定向到 HTTPS。

为我修复的是在 certbot 上使用 DNS 验证将 SSL 添加到我的子域。复制主要的 nginx 配置并为我的子域更改它。

现在我可以通过 HTTPS 成功访问主域和子域了。