nginx upstream作为变量

upstream wdzjbbs_varnish{
session_sticky;
server 10.174.35.11 weight=1 max_fails=2 fail_timeout=30s;
}

upstream wdzjbbs{
session_sticky;
server 10.174.39.137 weight=1 max_fails=2 fail_timeout=30s;
}

server {

...

set $upstream_x "wdzjbbs_varnish";
if ( $http_cookie ~* "auth_token"){
  set $upstream_x  "wdzjbbs";
  }
if ( $request_uri ~* "search"){
  set $upstream_x "wdzjbbs";

  }

...

location / {
proxy_pass http://$upstream_x;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}

注意:通过以上对upsteram变量进行判断设置,这样做,可以通过cookie来判断有无后跳转到后台哪台服务器上

原文地址:https://www.cnblogs.com/zenghui940/p/4738882.html