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/