parameters - 如何使用nginx通过proxy_pass转发查询字符串参数?

标签 parameters nginx

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

上面的代码片段会将 url 包含字符串“service”的请求重定向到另一台服务器,但它不包含查询参数。

最佳答案

来自 proxy_pass文档:

A special case is using variables in the proxy_pass statement: The requested URL is not used and you are fully responsible to construct the target URL yourself.



由于您在目标中使用了 $1,因此 nginx 依赖于您确切地告诉它要传递什么。您可以通过两种方式解决此问题。首先,使用 proxy_pass 去除 uri 的开头是微不足道的:
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/;
}

或者,如果您想使用正则表达式位置,只需包含参数:
location ~* ^/service/(.*) {
  proxy_pass http://apache/$1$is_args$args;
}

关于parameters - 如何使用nginx通过proxy_pass转发查询字符串参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8130692/

相关文章:

Django channel 与 uWSGI

nginx - 将默认 (80) 端口重定向到 5000 - Flask + NGINX + Ubuntu

c# - 将接口(interface)类型作为参数传递

c++ - 模板参数修饰符

c# - 在重载方法上使用泛型参数

java - 如何在静态方法中使用类作为参数

linux - 如何将.key文件的权限设置为仅由nginx访问

javascript - 如何将 JavaScript 变量添加到表单操作

ruby-on-rails - 通过 Rails 2.3 使用 x-sendfile 通过 Nginx 提供大文件

javascript - 我希望能够在网络上查看闭路电视屏幕