gpt4 book ai didi

Nginx 重定向到错误的虚拟主机

转载 作者:行者123 更新时间:2023-12-03 14:51:25 26 4
gpt4 key购买 nike

我在一个 nginx conf 文件中有大约 1300 个虚拟主机。所有具有以下布局(它们在 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;
}
}

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

在第一次测试中,重定向似乎只在重新加载时发生......要做更多的测试,但这可能会有所帮助..

最佳答案

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

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.

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



你能检查那些错误请求的主机头吗?

您也可以创建 一个明确的 默认服务器捕获所有这些错误请求,并将请求信息(即 $http_host)记录到不同的错误日志文件中以供调查。
server {
listen 80 default_server;
server_name _;
error_log /path/to/the/default_server_error.log;

return 444;
}

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

重新加载是这样的

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



所以老 worker 和新 worker 可以共存一段时间。例如,当您在配置文件中添加一个新的服务器 block (带有新域名)时,在重新加载期间,新的工作人员将拥有新的域,而旧的则没有。当请求恰好由旧的工作进程发送时,它将被视为未知主机并由默认服务器提供服务。

你说它每2分钟完成一次。你能跑吗
ps aux |grep nginx

并检查每个 worker 运行了多长时间?如果超过 2 分钟,重新加载可能无法按预期工作。

关于Nginx 重定向到错误的虚拟主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15797397/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com