使用nginx proxy_pass修改位置标题 [英] Modifying a Location header with nginx proxy_pass
问题描述
我有一个nginx proxy_pass
setup来将 / api
上的每个请求传递给后端Tomcat REST服务。此服务在某些情况下会返回位置
标头,该标头根据请求类型而不同,例如位置:http://foo.bar/baz / api / search / 1234567
- baz
部分归因于它托管在Tomcat上。
我的当前配置正确地重写了 foo.bar
主机名,但保留了 baz
部分不变。我想删除这个,但proxy_pass选项似乎仅限于清除或设置标题的新值。
有没有办法动态修改标题在传递给客户端之前,使用正则表达式替换,例如?这是我的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;
您可以使用regexp修改它,但更好的方法是使用代理重定向:
proxy_redirect http://foo.bar/baz/ /;
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect
任何位置 foo.bar/baz /
的标题将会转到 /
如果您只想重定向 / baz / api
,那也可以。
如果有重定向也添加了端口,您还需要添加 http://foo.bar:8080/baz/
(单独重定向)。
希望这有助于!
I have an nginx proxy_pass
setup to pass every request on /api
through to a backend Tomcat REST service. This service in some cases returns a Location
header which varies according to the type of request, e.g., Location: http://foo.bar/baz/api/search/1234567
-- the baz
part is due to it being hosted on Tomcat.
My current configuration rewrites the foo.bar
host name correctly, but leaves the baz
part intact. 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.
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:
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:
proxy_redirect http://foo.bar/baz/ /;
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect
Any Location headers for foo.bar/baz/
will go to /
If you just want to redirect /baz/api
, that'll work too.
If any redirects are also adding the port, you'll need to add http://foo.bar:8080/baz/
as well (separate redirect).
Hope this helps!
这篇关于使用nginx proxy_pass修改位置标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!