Nginx

来源:互联网 发布:桌面整理软件下载 编辑:程序博客网 时间:2024/06/18 16:40
nginx支持同时设置多组负载均衡,用来给不同的server来使用
nginx的负载均衡模块upstream目前支持4种算法:
1.轮询(默认):
   每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除
2.ip_hash:
   每个请求按访问IP的hash结果分配,这样每个访客固定访问一个后端服务器,可解决session共享的问题
3.fair(第三方):
   按后端服务器的响应时间来分配请求,响应时间短的优先分配
4.url_hash(第三方):
   按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效
nginx健康检查
重新编译安装第三方模块
cd nginx-1.3.7
git clone https://github.com/yaoweibin/nginx_upstream_check_module
patch -p1 nginx_upstream_check_module/check_1.2.6+.patch
./configure --add-modules=./nginx_upstream_check_module
实例:
   upstream myserver {
       ip_hash;
       server 192.168.1.1:80 weight=1 max_fails=2 fail_timeout=30s;
       server 192.168.1.2:80 weight=1 max_fails=2 fail_timeout=30s ;
       server 192.168.1.3:80 weight=1 max_fails=2 fail_timeout=30s down;
       server 192.168.1.4:80 weight=1 max_fails=2 fail_timeout=30s backup;
       check interval=3000 rise=2 fall=5 timeout=1000;
   }
location / {
   proxy_pass http://myserver           #将请求转发给upstream中定义好的服务器
   proxy_next_upstream http_502 http_504 error timeout invalid_header;    #如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器
   proxy_set_header Host $host;
   proxy_cache cache_one;                        #使用proxy_cache_path定义的缓存
   proxy_set_header X-Real-IP $remote_addr;    #检测客户端真实IP
   proxy_set_header X-Forwarded-For $remote_addr;    #将客户端请求的IP返回给后端服务器
}
weight:
   默认为1,weight越大,负载的权重就越大
max_fails:
   允许请求失败的次数,默认为1,当超过最大次数时,返回proxy_next_upstream模块定义的错误
fail_timeout:
  max_fails次失败后,暂停的时间
down:
   表示当前的server暂时不参与负载
backup:
   当其他所有非backup机器down或者忙的时候,请求backup机器,所以这台机器压力会最轻
interval:主动健康检查检测间隔时间,单位为毫秒
rsie:请求2次正常的话,标记此realserver的状态为up
fall:请求5次都失败的情况下,标记此realserver的状态为down
timeout:超时时间,单位为毫秒
#添加查看realserver状态页面
location /ups_status {
   check_status;
   access_log   off;
   allow 211.157.169.10;
   deny all;
}
原创粉丝点击