nginx模块之ngx_http_upstream_module

ngx_http_upstream_module

示例:

http上下文:

upstream upservers{
    ip_hash; //根据客户端IP进行调度,每个客户端ip地址访问时每个ip生成一个hash码,来自同一个客户端的请求分配到同一个server 
   server 192.168.1.102 weight=2;   
   server 192.168.1.103;
}

 server端:

proxy_pass http://upservers/;   

健康状况检测:

max_fails=numbers  //检查出的错误次数超过多少次就标记为失败了
fail_timeout=time  //每此检查的超时时长

 示例:

 upstream upservers {
   server 192.168.1.102 max_fails=2 fail_timeout=1;
 }

 如果要对服务器进行更新,可以这么做:

upstream upservers {
  server 192.168.1.103 max_fails=2 fail_timeout=1 backup;   //backup: 指定一个服务器为备用服务器 
}

 ip_hash是基于源IP进行session绑定

基于sticky进行session绑定:

格式:

Syntax:    sticky cookie name [expires=time] [domain=domain] [httponly] [secure] [path=path];
       sticky route $variable ...;
       sticky learn create=$variable lookup=$variable zone=name:size [timeout=time] [header] [sync];
Default:    —
Context:    upstream
This directive appeared in version 1.5.7.

cookie:

upstream backend {
    server backend1.example.com;
    server backend2.example.com;

    sticky cookie srv_id expires=1h domain=.example.com path=/;
}

route:

map $cookie_jsessionid $route_cookie {
    ~.+.(?P<route>w+)$ $route;
}

map $request_uri $route_uri {
    ~jsessionid=.+.(?P<route>w+)$ $route;
}

upstream backend {
    server backend1.example.com route=a;
    server backend2.example.com route=b;

    sticky route $route_cookie $route_uri;
}

 learn:

upstream backend {
   server backend1.example.com:8080;
   server backend2.example.com:8081;

   sticky learn
          create=$upstream_cookie_examplecookie
          lookup=$cookie_examplecookie
          zone=client_sessions:1m;
}

 least_conn: 调度方法,最少连接

 keepalive: 代理服务器和upstream server之间保持连接,一般后端是http server不建议使用,如果是缓存服务器可以考虑

 health_check:

  建议:关闭访问日志

自定义响应首部:

add_header X-Via $server_addr;
add_header X-Cache $upstream_cache_status;
原文地址:https://www.cnblogs.com/ckh2014/p/10875052.html