NGINX 阻止到我的家庭 IP(默认新网站页面或第一个子域)的流量,需要在家中托管的有效域名

NGINX 阻止到我的家庭 IP(默认新网站页面或第一个子域)的流量,需要在家中托管的有效域名

我的目标:禁用/丢弃请求中不包含域名的家庭 IP 的流量。

问题:目前,如果我(或任何其他人)通过 HTTP 或 HTTPS 访问我的家庭 IP,它将显示配置为虚拟主机的第一个子域。我尝试了多种不同的配置来删除或显示我的家庭 IP“域”为 410 Gone 甚至 444 断开连接,但均未成功。

这是 /etc/nginx/sites-available/default 中的默认服务器块

    # Default server configuration
#
server {
        #server_name _;

        listen 80 default_server;
        listen [::]:80 default_server;
#       listen 443 default_server ssl;
        #listen 80;
        #listen [::]:80;

#       return 444;

        #ssl_ciphers aNULL;
        #ssl_certificate /etc/ssl/certs/ssl-cert-snakeoilnpem;
        #ssl_certificate_key /etc/ssl/private/ssl-cert-snakeoil.key;

        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
            # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;


        location / {
#               return 444;
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;

        }

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

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #       deny all;
        #}
}

您可以看到,我尝试了几种不同的配置,但它们似乎都无法正常工作。最初,如果我不执行任何操作,它只会直接转到默认的新网站 NGINX 页面:

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }

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

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #       deny all;
        #}
}

最后要说的是,不确定这是否重要,但“默认”在 /etc/nginx/sites-enabled 中没有符号链接...

那么我做错了什么?

答案1

使用 Nginx 阻止未发送到我的域的连接有关于此主题的我之前的回答。

所以是的,您需要一个server带有指令的块default_server。并且包含该块的文件server需要链接到sites-enabled,否则 nginx 看不到配置。

您应该使用它nginx -T来查看 nginx 使用的配置是什么。

相关内容