[英]Modifying a Location header with nginx proxy_pass
I have an nginx proxy_pass
setup to pass every request on /api
through to a backend Tomcat REST service. 我有一个nginx proxy_pass
设置将/api
上的每个请求传递给后端Tomcat REST服务。 This service in some cases returns a Location
header which varies according to the type of request, eg, Location: http://foo.bar/baz/api/search/1234567
-- the baz
part is due to it being hosted on Tomcat. 在某些情况下,此服务返回一个Location
头,该头根据请求的类型而变化,例如, Location: http://foo.bar/baz/api/search/1234567
- baz
部分是由于它托管在Tomcat上。
My current configuration rewrites the foo.bar
host name correctly, but leaves the baz
part intact. 我当前的配置正确地重写了foo.bar
主机名,但保留了baz
部分。 I'd like to strip this, but the proxy_pass options seem to be limited to clearing or setting a new value for the header. 我想剥离它,但proxy_pass选项似乎仅限于清除或设置标头的新值。
Is there a way to modify headers dynamically before being passed on to the client, using a regex substitute, for instance? 有没有办法在传递到客户端之前动态修改标头,例如使用正则表达式替换? This is my nginx configuration: 这是我的nginx配置:
location /api {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_max_temp_file_size 0;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffers 32 4k;
proxy_redirect off;
proxy_pass http://foo.bar:8080/baz/api;
}
You may be able to use regexp to modify it but a better way is to use a proxy redirect: 您可以使用regexp来修改它,但更好的方法是使用代理重定向:
proxy_redirect http://foo.bar/baz/ /;
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect
Any Location headers for foo.bar/baz/
will go to /
foo.bar/baz/
任何位置标头都会转到/
If you just want to redirect /baz/api
, that'll work too. 如果你只是想重定向/baz/api
,那也会有效。
If any redirects are also adding the port, you'll need to add http://foo.bar:8080/baz/
as well (separate redirect). 如果任何重定向也添加端口,您还需要添加http://foo.bar:8080/baz/
(单独重定向)。
Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.