nginx代理重定向使用代理的端口号,而不是主机
问题描述:
我正在设置一个web/app/db堆栈,并且nginx代理配置不按照我认为的方式工作。nginx代理重定向使用代理的端口号,而不是主机
所以这里是堆栈的示范...的应用程序的网址是: 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 config来做些什么来使其正确重定向。
谢谢。 myles。
答
nginx的正常行为是将上游地址重写为页面所服务的地址。它看起来像不是使用你的上游地址(http://internal-alb.amazonaws.com:8083/),你的应用程序使用两者的混合(http://testapp.com:8083)进行响应。您可以更改应用程序行为,或者,在nginx级别修复它,可以使用proxy_redirect directive。
我有理由相信该指令来解决,这是proxy_redirect http://testapp.com:8083/ https://testapp.com/;
这是一个AJAX调用或普通帖子的呼叫? –
尝试添加'proxy_redirect http://internal-alb.amazonaws.com:8083/ https://testapp.com;' –