Nginx重定向到错误的vhost

W. *_*ckx 16 nginx

我在一个nginx conf文件中有大约1300vhosts.全部具有以下布局(它们在vhost文件中彼此列出).

现在我的问题是有时我的浏览器会将site2重定向到site1.出于某种原因,虽然域名没有事件匹配.

看起来nginx总是重定向到vhosts文件中的第一个站点.

有人知道这个问题是什么吗?

server {
    listen   80;

    server_name site1.com;
    rewrite ^(.*) http://www.site1.com$1 permanent;
}

server {
    listen   80;

    root /srv/www/site/public_html/src/public/;
    error_log /srv/www/site/logs/error.log;
    index index.php;

   server_name www.site1.com;

    location / {
        if (!-e $request_filename) {
            rewrite ^.*$ /index.php last;
        }
    }

    location ~ .(php|phtml)$ {
        try_files $uri $uri/ /index.php;
        fastcgi_param SCRIPT_FILENAME /srv/www/site/public_html/src/public$fastcgi_script_name;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi_params;
    }
}

server {
    listen   80;

    server_name site2.com;
    rewrite ^(.*) http://www.site2.com$1 permanent;
}

server {
    listen   80;

    root /srv/www/site/public_html/src/public/;
    error_log /srv/www/site/logs/error.log;
    index index.php;

   server_name www.site2.com;

    location / {
        if (!-e $request_filename) {
            rewrite ^.*$ /index.php last;
        }
    }

    location ~ .(php|phtml)$ {
        try_files $uri $uri/ /index.php;
        fastcgi_param SCRIPT_FILENAME /srv/www/site/public_html/src/public$fastcgi_script_name;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi_params;
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑也许另外要提的是,我使用nginx -s reload每2分钟重新加载所有这些虚拟主机.

在第一次测试时看起来重定向仅在重新加载时发生...要做更多的测试,但这可能会有所帮助..

Chu*_* Ma 19

参考(nginx如何处理请求):http://nginx.org/en/docs/http/request_processing.html

在此配置中,nginx仅测试请求的标头字段"Host",以确定请求应路由到哪个服务器.如果其值与任何服务器名称都不匹配,或者请求根本不包含此标头字段,则nginx会将请求路由到此端口的默认服务器.

默认服务器是第一个 - 这是nginx的标准默认行为

你能检查那些不良请求的主机头吗?

您还可以创建显式默认服务器来捕获所有这些错误请求,并将请求信息(即$ http_host)记录到不同的错误日志文件中以供调查.

server {
    listen       80  default_server;
    server_name  _;
    error_log /path/to/the/default_server_error.log;

    return       444;
}
Run Code Online (Sandbox Code Playgroud)

[ 更新 ]正如您所做的那样nginx -s reload,在nginx conf文件中有这么多域,以下是可能的:

重装是这样的

使用新配置启动新的工作进程,正常关闭旧工作进程

所以老工人和新工人可以共存一段时间.例如,当您向配置文件添加新的服务器块(使用新域名)时,在重新加载时,新工作人员将拥有新域,旧域将不会.当请求恰好由旧工作进程发送时,它将被视为未知主机并由默认服务器提供服务.

你说它每2分钟完成一次.你能跑吗?

ps aux |grep nginx
Run Code Online (Sandbox Code Playgroud)

并检查每个工人的运行时间?如果超过2分钟,重新加载可能无法按预期工作.