我试图在成功登录后将用户重定向回我的移动应用程序。(链接回应用程序)
为此,我307
通过以下链接发送回复:exp://192.168.178.33:19000?steamId=76561198154889373
我用 go 编写的服务器代码如下所示:
http.Redirect(w, r, redirectUrl+"?steamId="+steamId, 307)
它nginx
作为运行 rest-API 的 2 个服务器的反向代理负载平衡。
配置如下:
upstream rest_api {
server 123.456.789.123:8081 fail_timeout=10s;
server 111.222.333.444:8081 fail_timeout=10s weight=5;
}
server {
listen 443 ssl ipv6only=on;
ssl_protocols SSLv2 SSLv3 TLSv1 TLSv1.1 TLSv1.2;
server_name example.com www.example.com;
ssl_certificate /etc/nginx/ssl/chain.crt;
ssl_certificate_key /etc/nginx/ssl/privkey.key;
root /usr/share/nginx/html;
location / {
index index.html index.htm;
include /etc/nginx/headers/security_headers.conf;
include /etc/nginx/headers/default_headers.conf;
}
location /hi {
index hi.html;
}
location ~* /notfound {
index not_found.html;
}
}
server {
listen 443 ssl;
ssl_protocols SSLv2 SSLv3 TLSv1 TLSv1.1 TLSv1.2;
server_name api.example.com;
ssl_certificate /etc/nginx/ssl/chain.crt;
ssl_certificate_key /etc/nginx/ssl/privkey.key;
location / {
proxy_pass https://rest_api;
include /etc/nginx/headers/security_headers.conf;
include /etc/nginx/headers/default_headers.conf;
}
}
server {
listen 80;
listen [::]:80;
server_name example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
ssl_protocols SSLv2 SSLv3 TLSv1 TLSv1.1 TLSv1.2;
server_name example.com;
ssl_certificate /etc/nginx/ssl/chain.crt;
ssl_certificate_key /etc/nginx/ssl/privkey.key;
location ~* /notfound {
root /usr/share/nginx/html/notfound;
index not_found.html;
}
}
当运行没有 的 api并通过nginx
访问路由时,它工作正常,并且我被重定向。但是当使用as 反向代理时,我得到这个(注意浏览器窗口的标题就像中的一样:/login
111.222.333.444:8081/login
nginx
rest_api
upstream
nginx.conf
https://i.stack.imgur.com/4tlk3.png
链接在这里,因为我还没有 10 个代表。
我该如何配置nginx
才能将307
响应传回应用程序,而不是卡在上游声明中?
提前致谢。
编辑
似乎当使用nginx
上游进行负载平衡时,我登录后收到了 steam 的以下响应:
https://steamcommunity.com/openid/login?....&openid.realm=https://rest_api&openid.return_to=https://rest_api/login?redirectUrl=exp://THE_ACTUAL_REDIRECT_URL&....
这不起作用
但当我使用不带以下项的纯 IP 时,此方法有效nginx
:
https://steamcommunity.com/openid/login?....&openid.realm=https://111.222.333.444:8081&openid.return_to=https://111.222.333.444:8081/login?redirectUrl=exp://THE_ACTUAL_REDIRECT_URL
因此,在我看来,运行的服务器正在向全世界loadbalancer/proxy
暴露。rest_api
答案1
似乎默认情况下 nginx 传递$proxy_host
标Host
头(见下文),您需要将其更改为实际主机:
location / {
proxy_pass https://rest_api;
proxy_set_header Host $host;
// ...
}
句法:
proxy_set_header field value;
[...]
默认情况下,仅重新定义两个字段:
proxy_set_header Host $proxy_host; proxy_set_header Connection close;
--https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header
$proxy_host
如 proxy_pass 指令中指定的代理服务器的名称和端口;
--https://nginx.org/en/docs/http/ngx_http_proxy_module.html#variables