【问题标题】:Hot change "Location" in the reply header 302 code from proxy-host.来自代理主机的回复标头 302 代码中的热更改“位置”。
【发布时间】:2018-05-04 18:12:08
【问题描述】:

我有 kubernetes-cluster 和一些带有 web-app 的 Pod/容器。 Pod 通过 pod 的名称相互连接,监听端口为 9999(如 security-rest-api:9999、common-rest-api:9999 等)。 使用外部地址http://e.net:30200/ 在外部监听 nginx-pod。

((app-pods:9999)-(nginx-pod:80)-(nginx-service:30200))-网络

Nginx 使用以下配置与 app-pod 进行交互。

    server {

    listen       80;
    server_name  e.net;

    location / {
        proxy_pass              http://web-console:9999/;
        proxy_redirect          http://web-console:9999/ http://e.net:30200/;  
    }

    location /common {
        proxy_pass              http://common-rest-api:9999/common;
        proxy_redirect          http://common-rest-api:9999/ http://e.net:30200/;  

    }

    location /security {
        proxy_pass              http://security-rest-api:9999/security;
        proxy_redirect          http://security-rest-api:9999/ http://e.net:30200/;     

    } }

它工作得很好,但我有一个来自 app-pods 的 302 回复的问题:

如果我尝试登录我的应用程序,我会收到 302 回复标题:

HTTP/1.1 302 Found
Connection: keep-alive
Content-Length: 0
Date: Wed, 25 Apr 2018 10:37:50 GMT
Location: http://e.net:30200/security/rest/auth/login.html?callbackUrl=http://security-rest-api:9999/security/rest/namespace
Server: nginx/1.13.9

App-pods 从容器网络内的主机请求标头生成 URL 参数“callbackUrl”和此 URL 参数以到达端点浏览器。当然,下一个请求会得到 404 码。

我无法编辑 app-code(在 app-pods 中不要使用 nginx),但我想将 Location 中的 'security-rest-api:9999' 更改为 'e.net:30200' 参数302 回复标头。我该怎么做?

redirect 不适合,因为这会生成新的 302-reply 并不能解决我的问题。 sub_filter 仅更改回复正文,但不更改回复头(位置参数在哪里)。 request_uri 也不起作用,因为此参数仅适用于请求标头。

【问题讨论】:

  • 您的应用需要了解并考虑到可能需要向用户呈现不同 URL 的事实。许多其他应用程序(例如 Jenkins)就是这样做的。

标签: nginx kubernetes


【解决方案1】:

不,它不起作用。

我测试了这种情况并找到了工作的配置:

if ($args ~* (.*)(callbackUrl=http://security-rest-api:9999/)(.*)) {
    set $args $1callbackUrl=http://e.net:30200/$3;
    rewrite ^(.*)$ $1;
}
if ($args ~* (.*)(callbackURL=http%3A%2F%2Fsecurity-rest-api%3A9999%2F)(.*)) {
    set $args $1callbackURL=http%3A%2F%2Fe.net%3A30200%2F$3;
    rewrite ^(.*)$ $1;
}


location /security {
    proxy_pass http://security-rest-api:9999/security;
    proxy_set_header Host $http_host;
    proxy_redirect http://security-rest-api:9999/ http://e.net:30200/;

}

稍后,我将尝试在预生产台上使用此配置,如果此配置有效(或在更正后有效) - 我会在这里写。

感谢您的帮助信息: https://blog.imaginea.com/modifying-query-parameters-nginx-in-reverse-proxy-mode/

也谢谢大家!

编辑

我测试了这个配置并进行了 2 次修改:

  1. 如果你使用非标准端口 - 你需要写“proxy_set_header Host $http_host;”在位置部分;
  2. 属性中的 URL 可以像“http://security-rest-api:9999/”和“http%3A%2F%2Fsecurity-rest-api%3A9999%2F”。您需要为每种类型的属性使用这两个条件。

我用这个编辑更正了代码

【讨论】:

    猜你喜欢
    • 2012-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-04
    • 2012-03-08
    • 1970-01-01
    • 2013-09-15
    • 2023-02-02
    相关资源
    最近更新 更多