2013-09-27 135 views
1

我想将nginx设置为负载均衡器。但我想设置它的方式是某些请求(具有特定参数)只会发送到某些主机。基本上想法是在原始请求中使用任何主机,然后如果用户指定某个参数,例如bla0,然后将请求重定向到主机0,而对于BLA1主办1.因此,这里是我的配置想出了:nginx负载均衡器/位置难题

# load balancing server 
server { 
    listen 8000; 
    server_name example.com www.example.com; 

    # requests to bla0 server 
    location ~ ^(/request).*bla0$ { 
     proxy_pass http://localhost:8081; 
    } 

    # requests to bla1 server 
    location ~ ^(/request).*bla1$ { 
     proxy_pass http://localhost:8082; 
    } 
    # for default location use balancer 
    location/{ 
     proxy_pass http://cluster; 
    } 

} 

upstream cluster { 
    server localhost:8081; 
    server localhost:8082; 
} 

但不幸的是这种配置不起作用。我总是得到轮询请求,即/请求?q = bla0进入任一主机。我错过了什么。

回答

1

位置与参数不匹配。从http://wiki.nginx.org/HttpCoreModule#location

位置指令仅尝试从第一场比赛/在 主机名后,以之前的第一?要么 #。 (在此范围内,它 转义的URL匹配。)

看起来你需要使用if()arg_*指令,但我还不能肯定这一点。啊,我们走吧,this看起来像你想要的。

+0

伟大的提示,这是我一直在寻找。所以位置规则变成这样:location/{proxy_pass http:// cluster; if($ args〜pattern1){proxy_pass http:// host1} if($ args〜pattern2){proxy_pass http:// host2}} – Valentin