使用不同的nginx位置上下文和后端服务器上下文进行负载平衡

我使用nginx和上游模块进行负载平衡,具有以下configuration

upstream lb { server 127.0.0.1:8080; server 127.0.0.1:8081; } server { listen 88; server_name localhost; location /cas/ { proxy_pass http://lb; proxy_redirect off; proxy_connect_timeout 2; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } 

问题是“位置/上下文/”必须匹配到后端服务器的上下文,所以当我请求localhost / context / index.html然后nginx路由它到127.0.0.1:8080/context/index.html或127.0.0.1 :8080 /上下文/ index.html中。

是否有可能有不同的后端上下文和Nginx的位置,例如“位置/”nginx会将请求路由到127.0.0.1:8080/context/index.html或127.0.0.1:8080/context/index.html

谢谢。

你可以有多个位置语句,每个语句都有它自己的上游

 location /cas/ { proxy_pass http://lb; } location /web/ { proxy_pass http://2b; } location /mail/ { proxy_pass http://3b; } 

您也可以使用if语句来检查$ request_urivariables,并使用它来决定将请求传递给哪个上游。

 if ($request_uri ~* "^/(.+?)/context") { proxy_pass http://domain.com$request_uri; break; }