首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >nginx允许从应用程序重定向到客户端。

nginx允许从应用程序重定向到客户端。
EN

Server Fault用户
提问于 2022-08-15 14:28:12
回答 1查看 255关注 0票数 0

在成功登录后,我试图将用户重定向回我的移动应用程序。(链接回应用程序)

为此,我使用以下链接发送307响应:exp://192.168.178.33:19000?steamId=76561198154889373

我用go编写的服务器代码如下所示:

代码语言:javascript
运行
复制
http.Redirect(w, r, redirectUrl+"?steamId="+steamId, 307)

nginx对运行rest的2台服务器进行反向代理负载平衡。

下面是配置:

代码语言:javascript
运行
复制
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;
             }

        }

在不使用nginx的情况下运行api并通过111.222.333.444:8081/login访问/login路由时,它可以正常工作,并且可以重定向。但是,当使用nginx作为反向代理时,我得到了以下结果(注意,浏览器窗口的标题是rest_api,就像nginx.conf中的upstream

https://i.stack.imgur.com/4tlk3.png

链接这里,因为我还没有10个代表。

如何配置我的nginx以将307响应传递回应用程序,而不是陷入上游声明中?

提前谢谢。

<#>编辑

在使用nginx上游进行负载平衡时,我在登录后会得到以下响应:

代码语言:javascript
运行
复制
https://steamcommunity.com/openid/login?....&openid.realm=https://rest_api&openid.return_to=https://rest_api/login?redirectUrl=exp://THE_ACTUAL_REDIRECT_URL&....

这不管用

但是,当我使用没有nginx的普通IP时,这是可行的:

代码语言:javascript
运行
复制
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的形式向世界公开。

EN

回答 1

Server Fault用户

回答已采纳

发布于 2022-08-15 16:09:52

在默认情况下,nginx在$proxy_host头中传递Host (参见下面),您需要将其更改为实际的主机:

代码语言:javascript
运行
复制
         location / {
                 proxy_pass https://rest_api;
                 proxy_set_header Host $host;
                 // ...
         }

语法:默认情况下,只有两个字段被重新定义: proxy_set_header主机$proxy_host;proxy_set_header连接关闭;-- https://nginx.org/en/docs/http/ngx_http_代理_module.html#proxy_设置_标题$proxy_host指令中指定的代理服务器的名称和端口;-- https://nginx.org/en/docs/http/ngx_http_代理_module.html#variables

票数 0
EN
页面原文内容由Server Fault提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://serverfault.com/questions/1108234

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档