Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 是否可以使用nginx将URI重写为反向代理,但以某种方式将其保留在请求中以供应用程序访问?_Spring_Spring Boot_Nginx - Fatal编程技术网

Spring 是否可以使用nginx将URI重写为反向代理,但以某种方式将其保留在请求中以供应用程序访问?

Spring 是否可以使用nginx将URI重写为反向代理,但以某种方式将其保留在请求中以供应用程序访问?,spring,spring-boot,nginx,Spring,Spring Boot,Nginx,我想使用nginx将代理反转为服务并重写URI,但不知何故保留了完整的原始URL。可能吗 例如,如果我有这样的配置: location /passthrough/ { proxy_pass https://service.com/; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $

我想使用nginx将代理反转为服务并重写URI,但不知何故保留了完整的原始URL。可能吗

例如,如果我有这样的配置:

location /passthrough/ {
  proxy_pass https://service.com/;
  proxy_set_header X-Forwarded-Host $host;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header Host $host;
}
如果我用如下方式调用nginx:

curl https://original.com/passthrough/api/users
我想让它代理我的
https://service.com/api/users
但保留原始URI,以便在我的应用程序中,我可以以
https://original.com/passthrough/api/users
。可能吗

到目前为止,我可以得到
https://original.com/api/users
,但它缺少
/passthrough/
部分

接收应用程序是使用spring boot构建的,如果需要的话。

更改此选项:

proxy\u passhttps://service.com/;

为此:

proxy\u passhttps://service.com;

如果向代理传递url附加任何内容,则它将替换与其位置块匹配的请求uri部分

proxy_set_header X-Forwarded-Prefix "/passthrough/";
到您的nginx配置

在应用程序中添加此过滤器bean

@Bean public Filter forwardedHeaderFilter() { return new ForwardedHeaderFilter(); }
然后,您可以通过

ServletUriComponentsBuilder.fromCurrentRequest()
如果您不想修改它,也可以使用普通的
HttpServletRequest

request.getContextPath() //returns /passthrough
request.getRequestURL()  // returns https://original.com/passthrough/api/users

这将导致请求变成
https://service.com/passthrough/api/users
,而我确实希望它替换请求URI,但仍以某种方式保留原始URI供应用程序访问;也许我解释得不对。我想代理并重写URI,但我想保留最初发送的完整原始URL,包括重写的任何部分。谢谢!我放弃了希望,这是可能的,但它的工程伟大@ZJL不用担心,我刚才也有同样的问题,非常烦人