GET请求到nginx反向代理会进入302重定向循环。

huangapple go评论64阅读模式
英文:

GET requests to nginx reverse proxy going in a 302 redirect loop

问题

我已配置了一个Nginx服务器,用作我的Node.js应用程序的反向代理,以提供API请求。当我向这个Nginx服务器发出POST请求时,请求会正确传递给Node应用程序,但对同一地址的任何GET请求都会导致302重定向循环。我已经尝试了不同的配置更改3天,但无法找到解决方案。

以下是我在nginx.conf中的服务器块:

server_name  demo.example.com;
root         /usr/share/nginx/html;

# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;

location /api/ {
    proxy_pass http://demoapi/;
    proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
    proxy_set_header Accept-Encoding "";
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    #proxy_set_header X-NginX-Proxy true;

    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';

    proxy_buffers 8 32k;
    proxy_buffer_size 64k;
    proxy_redirect off;
}

上游的demoapi被定义为:

upstream demoapi {
    server 127.0.0.1:1337;
    keepalive 300;
}

我开始认为这可能是与从amazon-linux-extras安装的Nginx版本1.16有关的问题,因为在一个安装了Nginx版本1.12的较旧服务器上,类似的配置正常工作。

(请注意,上述Nginx配置中的注释部分已被去掉,以满足您的要求。)

英文:

I have configured an nginx server that is setup as a reverse proxy for my node.js application to serve api requests. When I make POST requests to this nginx server, the request is going to the node app properly, but any GET request on the same results in a 302 redirect loop. I have been trying different configuration changes from 3 days but unable to find a solution

Following is my server block in nginx.conf

    server_name  demo.example.com;
    root         /usr/share/nginx/html;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location /api/ {
            proxy_pass http://demoapi/;
            proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
            proxy_set_header Accept-Encoding "";
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            #proxy_set_header X-NginX-Proxy true;

            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';

            proxy_buffers 8 32k;
            proxy_buffer_size 64k;
            proxy_redirect off;
    }

The upstream demoapi is defined as

upstream demoapi {
    server 127.0.0.1:1337;
    keepalive 300;
}

<strike>I am beginning to think that it might be an issue with the nginx version 1.16 installed from amazon-linux-extras as the similar configuration works fine on one of the older servers with nginx version 1.12 installed.</strike>

答案1

得分: 2

我终于找出了这个问题的根本原因。302重定向是由运行在127.0.0.1:1337上的Sails应用程序发送的,因为它没有在被Nginx转发(反向代理)的GET请求中收到主机标头。

为了解决这个问题,我在服务器位置块中添加了以下行:

proxy_set_header Host $host;

将其发布在这里,以便任何面临相同问题的人可以受益。

英文:

I finally figured out the root cause of this issue. The 302 redirect was being sent by the sails application running on 127.0.0.1:1337 since it wasn't getting the host header in GET request forwarded (reverse-proxied) by nginx.

To fix the issue, I added the following line to the server location block

proxy_set_header Host $host;

Posting it here so that anyone facing the same issue could get benefitted.

huangapple
  • 本文由 发表于 2020年1月6日 20:13:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/59611909.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定