如何在nginx中使用url pathname作为上游哈希

我有一个配置的nginx服务器使用queryparam作为上游哈希.网址如下所示

http://www.my-server.com/xyz/WXYZ?abc=123

并配置如下

upstream test {
    hash $arg_abc;
    ....
}

是否有可能使用URL的WXYZ部分作为上游哈希?

WXYZ是动态值,xyz总是相同的,并且会在那里.

这是我试过的,

location ~ ^/xyz/(.).*${
   hash $1
}

最佳答案
The deployment guide明确表示有可能:

The generic hash method: the server to which a request is sent is
determined from a user-defined key which may be a text,variable,or
their combination. For example,the key may be a source IP and port,
or URI:

upstream backend {
    hash $request_uri consistent;

    server backend1.example.com;
    server backend2.example.com;
}

哈希键是$request_uri,可以用$arg_your_key替换,但不确定是否适用于上游块,但它应该作为proxy_pass值工作:

location /xyz {
  proxy_pass http://localhost/$uri$is_args$args;
}

不确定要求,但如果你需要使用基于参数$arg_abc的某个后端,你需要map功能,如here:

map $arg_abc $backend_server {
 default  'serverdefault.domain.com:80';
 123 'server1.domain.com:80';
 234 'server2.domain.com:80';
 345 'server3.domain.com:80';
}

server {
 location / {
  proxy_pass http://$backend_server;
 }
}

dawei

【声明】:淮南站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。