Nginx 服务器块未被选中

Nginx 服务器块未被选中

我已经创建了一个新的子域并更新了以下 vhost 配置文件,但 nginx 仍然选择默认值(第一个服务器块)

知道两个块都监听相同的 IP:PORT !! 知道为什么在访问子域时不选择第二个块吗?

server {
        server_name domain.com www.domain.com;
        charset off;
        disable_symlinks if_not_owner from=$root_path;
        index index.php index.html;
        root $root_path;
        set $root_path /var/www/udomain/data/www/domain.net;
        access_log /var/www/httpd-logs/domain.com.access.log ;
        error_log /var/www/httpd-logs/domain.com.error.log notice;
        include /etc/nginx/vhosts-includes/*.conf;
        location / {
            location ~* ^.+\.(jpg|jpeg|gif|png|svg|js|css|mp3|ogg|mpe?g|avi|zip|gz|bz2?|rar|swf)$ {
                try_files $uri $uri/ @fallback;
            }
            location / {
                try_files /does_not_exists @fallback;
            }
            location ~ [^/]\.ph(p\d*|tml)$ {
                try_files /does_not_exists @fallback;
            }
        }
        location @fallback {
                proxy_pass http://127.0.0.1:8080;
                proxy_redirect http://127.0.0.1:8080 /;
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
                access_log off ;
        }
        ssi on;
        listen IP-Address:80;
}
server {
    server_name subdomain.domain.com;

    location / {
        proxy_pass http://localhost:SOMEPORT/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
    listen IP-Address:80;
    ssi on;
}

答案1

我认为你应该按如下方式编写你的监听指令:

server {
    listen       80;
    server_name  domain.com;

    location {
        ...
    }
}

server {
    listen       80;
    server_name  subdomain.domain.com;

    location {
        ...
    }
}

此外,这里还有关于 Nginx 如何选择使用哪个服务器块的很好的解释:https://www.digitalocean.com/community/tutorials/understanding-nginx-server-and-location-block-selection-algorithms

如果您通过浏览器访问,我建议您查看标题Host,并根据以上教程构建适当的服务器块。

相关内容