nginx proxied redirects使用代理的端口号,而不是主机

时间:2017-09-11 18:40:51

标签: nginx proxy

我正在设置一个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配置做些什么才能让它正确重定向。

感谢。 迈尔斯。

1 个答案:

答案 0 :(得分:0)

nginx的正常行为是将上游地址重写为提供页面的地址。看起来不是使用您的上游地址(http://internal-alb.amazonaws.com:8083/),而是您的应用使用两者的混合(http://testapp.com:8083)进行响应。您可以更改应用行为,或者在nginx级别修复它,可以使用proxy_redirect directive

我合理地确定解决此问题的指令是proxy_redirect http://testapp.com:8083/ https://testapp.com/;