【问题标题】:Nginx reverse proxy causing infinite loopNginx反向代理导致无限循环
【发布时间】:2015-11-28 12:10:27
【问题描述】:

我的 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。

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

【问题讨论】:

  • 你期待什么?
  • 我希望 example2.com 显示 localhost/page-1。你知道它为什么不工作吗?
  • 那么你不应该设置Host header
  • nginx.org/en/docs/http/request_processing.html 阅读 nginx 如何处理您的请求。在您的情况下,包括 Host 会导致代理请求落入同一服务器块。
  • 这取决于你的需要,但也许你应该

标签: redirect nginx virtualhost reverse-proxy redirect-loop


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2016-01-09
    • 1970-01-01
    • 2020-09-22
    • 1970-01-01
    • 1970-01-01
    • 2011-06-04
    • 2017-11-24
    • 2021-10-21
    • 2016-02-02
    相关资源
    最近更新 更多