nginx 反向代理重定向使用代理的端口号,而不是主机的端口号

我正在配置一个 Web/App/DB 的堆栈,并且 Nginx 代理配置并没有按照我想的那样工作。

所以这里是堆栈的一个例子... 应用程序的 URL 是: https://testapp.com

这里是 Nginx 配置:

   server {
   listen       8886;
   server_name  _;
   root         /usr/share/nginx/html;
   include /etc/nginx/default.d/*.conf;

   #ELB
   if ($http_user_agent = 'ELB-HealthChecker/2.0') {
    return 200 working;
   }

   #HTTP to HTTPS
   if ($http_x_forwarded_proto != 'https') {
       return 301 https://$host$request_uri;
   }
   location / {
        set $proxy_upstream_name "testapp.com";
        port_in_redirect off;
        proxy_pass         http://internal-alb.amazonaws.com:8083/;
        proxy_redirect     off;
        proxy_set_header   Host $host;
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Host $server_name;
        proxy_set_header   Access-Control-Allow-Origin $http_origin;}

该应用程序被代理到了一个内部的 AWS ALB 上,并将其转发到一个单一的(此时)应用程序服务器。

我可以让网站服务。 然而,应用程序在登录时创建了一个重定向,并且我收到了以下响应。

Request URL:https://testapp.com/login
Request Method:POST
Status Code:302
Remote Address:34.192.444.29:443
Referrer Policy:no-referrer-when-downgrade
Response Headers
content-language:en-US
content-length:0
date:Mon, 11 Sep 2017 18:35:34 GMT
location:http://testapp.com:8083/testCode
server:openresty/1.11.2.5
status:302

重定向失败是因为它是在 443 上服务的,而不是 8083。

由于某些原因,应用程序或代理没有在进行反向代理时更新端口,因此重定向具有代理的端口,而不是实际的应用程序端口 443。

我需要在 Nginx 配置中做什么才能正确地重定向呢?

谢谢。 myles.

点赞
用户732547
用户732547

正常情况下,nginx 会将上游地址重写为页面所提供的地址。看起来,您的应用程序没有使用上游地址(http://internal-alb.amazonaws.com:8083/),而是使用了两种地址的混合(http://testapp.com:8083)。您可以更改应用程序行为,或者在 nginx 级别上修复它,可以使用 proxy_redirect 指令

我相信修复此问题的指令是 proxy_redirect http://testapp.com:8083/ https://testapp.com/;

2017-09-11 18:53:10