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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值