Nginx反向代理导致无限循环

时间:2015-09-02 20:20:57

标签: 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 $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_set_header Host $host;行时,似乎只会出现此问题。似乎$host变量创建了一个重定向循环,来自错误服务器日志的GET请求与此...page-1/page-1/page-1/page-1...类似,服务器响应内部错误500.

如果有人能告诉我自己做错了什么,我真的很感激。非常感谢提前!

1 个答案:

答案 0 :(得分:1)

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