在Nginx中编辑标头值 [英] Edit a header value in nginx

查看:80
本文介绍了在Nginx中编辑标头值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Background
So I've got a server running a tomcat application hidden behind an Apache proxy. The proxy provides a more user friendly url as well as SSL encryption with automatic redirects so that the app is only accessible on https.

我正忙于将此迁移到Nginx代理.

I'm busy migrating this to an nginx proxy.

我遇到的问题之一是,登录后,我的应用程序以http响应的形式发送回"LocationAfterLogon"标头

One of the issues I've had is that upon login, my app sends back a "LocationAfterLogon" header in the http response in the form of

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

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

That IP address returned is from the proxied server not visible on the internet. So then the browser gets a connection error trying to navigate to it.

作为一种解决方法,我使用了nginx指令:

As a workaround, I've used nginx directives:

  • proxy_hide_header:隐藏从代理服务器返回的LocationAfterLogin标头
  • add_header:添加新的LocationAfterLogin网址.

所以我的配置如下

            #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.

The Problem
I need to be able to do a regex replace or similar on LocationAfterLogon because it won't always be to index.jsp, depending on which url was intercepted by the login page.

我知道我也可以重写tomcat应用程序以发送回相对URL,但是我想在nginx config中完成所有操作.

I am aware that I can also rewrite the tomcat app to send back a relative URL instead, but I'd like to do it all in nginx config.

我还阅读了有关nginx more_set_headers的文章.还没有尝试过.它可以让我编辑标题吗?

I've also read about nginx more_set_headers. Haven't tried it yet. Does it allow me to edit the headers?

Apache具有我以前使用的 Header edit 指令,所以我正在寻找类似的东西.

Apache has the Header edit directive which I was using previously, so I'm looking for something like that.

TL; DR
是否可以在Nginx中使用正则表达式替换或类似内容来编辑标头位置?

TL;DR
Is is possible to edit a header location using regex replace or similar in Nginx?

推荐答案

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

You can use the map directive to rewrite your header:

 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中编辑标头值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆