gpt4 book ai didi

nginx - 在 nginx 中编辑 header 值

转载 作者:行者123 更新时间:2023-12-04 03:49:40 26 4
gpt4 key购买 nike

背景
所以我有一个服务器运行隐藏在 Apache 代理后面的 tomcat 应用程序。该代理提供了更用户友好的 url 以及具有自动重定向功能的 SSL 加密,因此该应用程序只能在 https 上访问。

我正忙于将其迁移到 nginx 代理。

我遇到的问题之一是,在登录时,我的应用程序在 http 响应中以以下形式发回“LocationAfterLogon” header

http://192.168.x.x:8080/myapp/index.jsp. 

返回的 IP 地址来自在 Internet 上不可见的代理服务器。因此,浏览器在尝试导航到它时会出现连接错误。

作为一种解决方法,我使用了 nginx 指令:
  • proxy_hide_header:隐藏从代理服务器返回的 LocationAfterLogin header
  • add_header:添加一个新的 LocationAfterLogin url。

  • 所以我的配置如下
                #header for location after logon of demo app
    add_header LocationAfterLogon http://example.com/demo/index.jsp;
    #hide the real LocationAfterLogon
    proxy_hide_header LocationAfterLogon;

    问题
    我需要能够在 LocationAfterLogon 上执行正则表达式替换或类似操作,因为它并不总是指向 index.jsp,具体取决于登录页面拦截了哪个 url。

    我知道我也可以重写 tomcat 应用程序来发回一个相对 URL,但我想在 nginx 配置中完成这一切。

    我还阅读了有关 nginx more_set_headers 的信息。还没试过。它允许我编辑标题吗?

    Apache 有 标题编辑 我以前使用的指令,所以我正在寻找类似的东西。

    TL;博士
    是否可以在 Nginx 中使用正则表达式替换或类似功能来编辑标题位置?

    最佳答案

    您可以使用 map指令重写您的标题:

     map $upstream_http_locationafterlogon $new_location {
    ~regexp new_value;
    }

    proxy_hide_header LocationAfterLogon;
    add_header LocationAfterLogon $new_location;

    请参阅文档: http://nginx.org/en/docs/http/ngx_http_map_module.html

    关于nginx - 在 nginx 中编辑 header 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38968375/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com