Nginx重定向到错误的虚拟主机 [英] Nginx redirecting to wrong vhost

查看:130
本文介绍了Nginx重定向到错误的虚拟主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have around 1300vhosts in one nginx conf file. All with the following layout (they are listed after each other in the vhost file).

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

Now my problem is that sometimes my browser redirects site2 to site1. For some reason, while the domain names don't event match.

nginx似乎总是重定向到vhosts文件中的第一个站点.

It looks like nginx is always redirecting to the first site in the vhosts file.

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

Somebody that know what this problem can be?

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;
    }
}

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

EDIT Maybe another thing to mention is that, I reload all this vhosts every 2 minutes with nginx -s reload.

在第一个测试中,似乎重定向仅在重新加载时发生...要进行更多测试,但这可能会有帮助.

On the first tests it looks like the redirection only happens when reloading... Going to do some more tests, but this could be helpful..

推荐答案

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

Reference (how nginx handles request): http://nginx.org/en/docs/http/request_processing.html

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

In this configuration nginx tests only the request’s header field "Host" to determine which server the request should be routed to. If its value does not match any server name, or the request does not contain this header field at all, then nginx will route the request to the default server for this port.

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

the default server is the first one — which is nginx’s standard default behaviour

您可以检查那些错误请求的主机头吗?

Could you check the host header of those bad requests?

您还可以创建一个明确的默认服务器来捕获所有这些错误请求,并将请求信息(即$ http_host)记录到另一个错误日志文件中进行调查.

Also you can create an explicit default server to catch all of these bad requests, and just log the request info (i.e, $http_host) into a different error log file for investigation.

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

    return       444;
}

[ UPDATE ]在执行nginx -s reload时,该nginx conf文件中包含多个域,因此可能会发生以下情况:

[UPDATE] As you are doing nginx -s reload and you have so many domains in that nginx conf file, the following is possible:

重新加载是这样的

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

starting new worker processes with a new configuration, graceful shutdown of old worker processes

因此,老工人和新工人可以并存一段时间.例如,当您在配置文件中添加新服务器块(具有新域名)时,在重新加载期间,新工作线程将拥有新域,而旧工作线程将没有.如果该请求恰巧是由旧工作进程发送的,则它将被视为未知主机,并由默认服务器提供服务.

So old workers and new workers could co-exist for a while. For example, when you add a new server block (with new domain name) to your config file, during the reloading time, the new workers will have the new domain and the old one will not. When the request happens to be sent by the old worker process, it will be treated as unknown host and served by the default server.

您说过每2分钟完成一次.你能跑

You said that it's done every 2 minutes. Could you run

ps aux |grep nginx

并检查每个工作人员运行了多长时间?如果超过2分钟,则重新加载可能无法如您预期的那样进行.

and check how long each worker is running? If it's much more than 2 minutes, the reloading may not work as you expected.

这篇关于Nginx重定向到错误的虚拟主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆