本文介绍了如何使用nginx通过proxy_pass转发查询字符串参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

upstream apache {
   server 127.0.0.1:8080;
}
server{
   location ~* ^/service/(.*)$ {
      proxy_pass http://apache/$1;
      proxy_redirect off;
   }
 }

以上代码段会将网址中包含字符串"service"的请求重定向到另一台服务器,但其中不包含查询参数.

The above snippet will redirect requests where the url includes the string "service" to another server, but it does not include query parameters.

推荐答案

来自 proxy_pass 文档:

由于您在目标中使用了$ 1,因此nginx依靠您准确地告诉它要传递的内容.您可以通过两种方式解决此问题.首先,使用proxy_pass删除uri的开头很简单:

Since you're using $1 in the target, nginx relies on you to tell it exactly what to pass. You can fix this in two ways. First, stripping the beginning of the uri with a proxy_pass is trivial:

location /service/ {
  # Note the trailing slash on the proxy_pass.
  # It tells nginx to replace /service/ with / when passing the request.
  proxy_pass http://apache/;
}

或者,如果您想使用正则表达式位置,只需添加args:

Or if you want to use the regex location, just include the args:

location ~* ^/service/(.*) {
  proxy_pass http://apache/$1$is_args$args;
}

这篇关于如何使用nginx通过proxy_pass转发查询字符串参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 06:09