使用变量时,重写nginx上的URI不起作用

时间:2015-11-12 08:16:22

标签: nginx routing

我有这张地图集:

map $id $backend_host {
    default http://primary-host;
    1 http://primary-host;
    2 http://secondary-host;
}

以下地点:

location / {
  set $id 1;
  proxy_pass $backend_host;
}

location /second/ {
  set $id 2;
  proxy_pass $backend_host/;
  rewrite ^/second(.*)$ $1 break;
}

当发布到http://nginx-host/second/some/uri时,我发现它属于第二个位置子句,URI正在被重写,但随后它被代理到主要主机而不是辅助主机。

另一方面,当我设置第二个位置时:

location /second/ {
  proxy_pass http://secondary-host/;
  rewrite ^/second(.*)$ $1 break;
}

也就是说,不使用变量,它就可以正常工作。

无法在解释该内容的文档中找到任何内容。

1 个答案:

答案 0 :(得分:0)

试试这个

map $id $backend_host {
    default primary-host;
    1 primary-host;
    2 secondary-host;
}

location / {
  set $id 1;
  proxy_pass http://$backend_host;
}

location /second/ {
  set $id 2;
  proxy_pass http://$backend_host/;
  rewrite ^/second(.*)$ $1 break;
}

http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

  

甚至是这样:

     
    

proxy_pass $ request;

  
     

在这种情况下,搜索服务器名称   描述的服务器组,如果没有找到,则使用a确定   的分解器

注意:我没有测试它