🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
1)ip_hash 其中请求在基于客户端IP地址的服务器之间分配,该方法确保来自同一客户端的请求总是被传递到同一个服务器,除非该服务器不可用 案例: upstream StaticServer { ip_hash; server 10.100.100.106:8082 weight=2; server 10.100.100.107:8082 weight=1; } 2)Nginx学习之负载均衡fair模块 功能:按后端服务器的响应时间来分配请求,响应时间短的优先分配 下载模块: [root@proxy01 src]# git clone https://github.com/gnosek/nginx-upstream-fair.git [root@proxy01 nginx-1.9.2]# pwd /opt/nginx-1.9.2 [root@proxy01 nginx-1.9.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --error-log-path=/var/log/nginx/error_log --http-log-path=/var/log/nginx/access_log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --with-http_ssl_module --with-http_stub_status_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_flv_module --with-http_mp4_module --http-client-body-temp-path=/var/tmp/nginx/client --http-proxy-temp-path=/var/tmp/nginx/proxy --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi --add-module=/opt/ngx_cache_purge-master --add-module=/usr/local/src/nginx-upstream-fair && make [root@proxy01 nginx-1.9.2]# cp objs/nginx /usr/local/nginx/sbin/nginx 案例 upstream StaticServer { #ip_hash; server 10.100.100.105:8082 weight=2 max_fails=2 fail_timeout=2; server 10.100.100.107:8082 weight=1 max_fails=2 fail_timeout=2; server 10.100.100.106 backup; fair; } 3) url_hash(第三方) 按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。 hash key [consistent]; Context: upstream (在1.7.2版本以上可以使用) 案例1: 根据url来调度 upstream StaticServer { #ip_hash; hash $request_uri; server 10.100.100.105:8082 weight=2 max_fails=2 fail_timeout=2; server 10.100.100.107:8082 weight=1 max_fails=2 fail_timeout=2; #server 10.100.100.106 backup; #fair; } 案例2: 当用户访问 http://test.51yuki.cn/6657/5347/index.html 我们通过哈希url中6657,固定访问到一个server if ( $uri ~* "^\/([^\/]+)\/.*" ) { set $defurlkey $1 } upstream www { hash $defurlkey; server 10.100.100.105:8082 weight=2 max_fails=2 fail_timeout=2; server 10.100.100.107:8082 weight=1 max_fails=2 fail_timeout=2; } 4)加权轮询 weight参数 5)基于cookie的调度 语法: 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]; Default: — Context: upstream 安装nginx_sticky_module模块 [root@proxy01 opt]# git clone https://github.com/bymaximus/nginx-sticky-module-ng.git [root@proxy01 nginx-1.9.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --error-log-path=/var/log/nginx/error_log --http-log-path=/var/log/nginx/access_log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --with-http_ssl_module --with-http_stub_status_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_flv_module --with-http_mp4_module --http-client-body-temp-path=/var/tmp/nginx/client --http-proxy-temp-path=/var/tmp/nginx/proxy --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi --add-module=/opt/ngx_cache_purge-master --add-module=/usr/local/src/nginx-upstream-fair --add-module=/opt/nginx-sticky-module-ng/ && make [root@proxy01 nginx-1.9.2]# /usr/local/nginx/sbin/nginx -s stop [root@proxy01 nginx-1.9.2]# cp objs/nginx /usr/local/nginx/sbin/ cp: overwrite `/usr/local/nginx/sbin/nginx'? y [root@proxy01 nginx-1.9.2]# /usr/local/nginx/sbin/nginx 使用语法: sticky [name=route] [domain=.foo.bar] [path=/] [expires=1h] [hash=index|md5|sha1] [no_fallback]; name: 可以为任何的string字符,默认是route domain:哪些域名下可以使用这个cookie path:哪些路径对启用sticky,例如path/test,那么只有test这个目录才会使用sticky做负载均衡 expires:cookie过期时间,默认浏览器关闭就过期,也就是会话方式。 no_fallbackup:如果设置了这个,cookie对应的服务器宕机了,那么将会返回502(bad gateway 或者 proxy error),建议不启用 upstream StaticServer { #ip_hash; # hash $request_uri; sticky name=cookie expires=1h domain=.51yuki.cn path=/; server 10.100.100.105:8082 weight=2 max_fails=2 fail_timeout=2; server 10.100.100.104:8082 weight=1 max_fails=2 fail_timeout=2; #server 10.100.100.106 backup; #fair; } 浏览器访问: Cookie:UM_distinctid=15dc7020b0daed-09516b462eb88e-474a0521-15f900-15dc7020b0e9f6; _ga=GA1.2.1539088355.1513867128; cookie=7e5f2b9a2ddae0a19a42ac2aedc8f52d upstream StaticServer { #ip_hash; # hash $request_uri; #sticky name=cookie expires=1h domain=.51yuki.cn path=/; server 10.100.100.105:8082 weight=2 max_fails=2 fail_timeout=2; server 10.100.100.104:8082 weight=1 max_fails=2 fail_timeout=2; sticky expires=1h domain=.51yuki.cn; #server 10.100.100.106 backup; #fair; } 浏览器访问: Cookie:UM_distinctid=15dc7020b0daed-09516b462eb88e-474a0521-15f900-15dc7020b0e9f6; _ga=GA1.2.1539088355.1513867128; cookie=7e5f2b9a2ddae0a19a42ac2aedc8f52d; route=7e5f2b9a2ddae0a19a42ac2aedc8f52d