加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > Nginx > 正文

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

发布时间:2020-12-13 21:29:47 所属栏目:Nginx 来源:网络整理
导读:我有一个配置的nginx服务器使用queryparam作为上游哈希.网址如下所示 http://www.my-server.com/xyz/WXYZ?abc=123 并配置如下 upstream test { hash $arg_abc; ....} 是否有可能使用URL的WXYZ部分作为上游哈希? WXYZ是动态值,xyz总是相同的,并且会在那里.

我有一个配置的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;
 }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读