我需要两套不同的上游。 但是我所有的请求都来自同一个URL(相同的path)。 不同的是,一些请求会有一个特殊的说法,有些则不会。 根据这一点,我需要select使用哪个上游。 这是我的configuration文件的一个例子不完整的部分:
server_name localhost; root /var/www/something/; upstream pool1 { server localhost:5001; server localhost:5002; server localhost:5003; } upstream pool2 { server localhost:6001; server localhost:6002; server localhost:6003; } location / { # this is the part where I need help try_files $uri @pool1; } location @pool1 { include fastcgi_params; fastcgi_pass pool1; } location @pool2 { include fastcgi_params; fastcgi_pass pool2; }
所以…我不知道的部分是如何检查参数/参数是否在URL中,并根据它来使用位置pool1或pool2。
任何想法我怎么能实现呢?
谢谢!
@hellvinz是对的。 我不能评论,所以我正在作出另一个答案。
location / { if($myArg = "otherPool") { rewrite ^/(.*)$ /otherUpstream/$1 last; } try_files $uri pool1; } location /otherUpstream { proxy_pass http://@pool2; }
我想你将不得不将$ myArg更改为您正在testing的查询参数的名称,并将其他Pool设置为您设置的任何名称。 再加上重写没有经过testing,所以我可能也有这个错误,但你明白了。
我喜欢提出这个没有if语句的替代版本。 我知道这是一个老问题,但未来的谷歌可能仍然觉得这有帮助。
我必须承认这也意味着改变你select上游的方式。 但是我看不出有这个问题。
这个想法是发送一个自定义的HTTP头(X-Server-Select)与请求。 这允许nginx然后select正确的池。 如果标题不存在,则会select默认值。
你的configuration可能会变成这样:
upstream pool1 { server localhost:5001; server localhost:5002; server localhost:5003; } upstream pool2 { server localhost:6001; server localhost:6002; server localhost:6003; } # map to different upstream backends based on header map $http_x_server_select $pool { default "pool1"; pool1 "pool1"; pool2 "pool2"; } location / { include fastcgi_params; fastcgi_pass $pool; }
来源: nginx使用不同的后端基于http头
您可以使用if来testing$ arg所包含的参数