Redirect Nginx反向代理导致无限循环

Redirect Nginx反向代理导致无限循环,redirect,nginx,virtualhost,reverse-proxy,redirect-loop,Redirect,Nginx,Virtualhost,Reverse Proxy,Redirect Loop,我的Nginx站点配置文件中包含以下内容: server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; root /usr/share/nginx/html; index index.php index.html index.htm; server_name example.com; location / { try_files $

我的Nginx站点配置文件中包含以下内容:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.php index.html index.htm;

    server_name example.com;

    location / {
        try_files $uri $uri/ /index.php?q=$uri&$args;
    }

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }
}


server {
    listen 80;
    server_name example2.com;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $host;
        proxy_pass http://localhost/page-1/;
    }
}
这个问题似乎只有在我添加
proxy\u set\u头Host$Host时才会出现行。似乎
$host
变量创建了一个重定向循环,来自错误服务器日志的GET请求类似于
…page-1/page-1/page-1/page-1…
,服务器响应内部错误500


如果有人能告诉我我做错了什么,我将不胜感激。非常感谢

我也遇到了同样的问题,正如Alexey Ten所建议的,解决方案是删除主机头。

您希望看到什么?我希望example2.com会显示出来。您知道它为什么不工作吗?那么您不应该设置主机头来读取nginx如何处理您的请求。在您的情况下,包括
Host
会导致代理请求落入同一个服务器块。这取决于您的需要,但您可能应该这样做