目的:
添加第三方編寫的插件,以nginx-sticky-module為例,以下簡稱 sticky
通過 /usr/local/nginx/sbin/nginx -V 查看nginx已安裝的模塊
(推薦教程:nginx教程)
sticky模塊與Ip_hash都是與負載均衡算法相關,但又有差別,差別是:
1、ip hash,根據客戶端的IP,將請求分配到不同的服務器上
2、sticky,根據服務器給客戶端的cookie,客戶端再次請求時會帶上此cookie,nginx會把有此cookie的請求轉發到頒發cookie的服務器上
注意:在一個局域網內有3臺電腦,他們有3個內網IP,但是他們發起請求時,卻只有一個外網IP,是電信運營商分配在他們連接那個路由器上的,如果使用 ip_hash 方式,則Nginx會將請求分配到不同上游服務器,如果使用 sticky 模塊,則會把請求分配到辦法cookie的服務器上,實現:內網nat用戶的均衡。這是iphash無法做到的
Sticky工作原理:
Sticky是基于cookie的一種負載均衡解決方案,通過分發和識別cookie,使來自同一個客戶端的請求落在同一臺服務器上,默認cookie標識名為route :
1、客戶端首次發起訪問請求,nginx接收后,發現請求頭沒有cookie,則以輪詢方式將請求分發給后端服務器。
2、后端服務器處理完請求,將響應數據返回給nginx。
3、此時nginx生成帶route的cookie,返回給客戶端。route的值與后端服務器對應,可能是明文,也可能是md5、sha1等Hash值
4、客戶端接收請求,并保存帶route的cookie。
5、當客戶端下一次發送請求時,會帶上route,nginx根據接收到的cookie中的route值,轉發給對應的后端服務器。
Sticky官網地址
官方地址:
https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/src
下載地址:
wget?https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/get/master.tar.gz
Nginx安裝Sticky模塊
#1.下載的文件上傳,解壓 tar?-xvzf?nginx-goodies-nginx-sticky-module-ng-08a395c66e42.tar #2.重命名為nginx-sticky-module mv?nginx-goodies-nginx-sticky-module-ng-08a395c66e42?/usr/local/nginx-sticky-module #3.進入nginx源碼目錄進行編譯 ./configure?--prefix=/usr/local/nginx?--add-module=/usr/local/nginx-sticky-module?--with-http_stub_status_module?--with-http_ssl_module? #4.安裝 ?1.停止nginx后進行安裝:make?&&?make?install ?2.在線更新安裝:?make?upgrade
這樣就安裝完了,通過 ./sbin/nginx -V 查看編譯參數,可看到sticky模塊已被編譯進nginx
[root@bogon?nginx]#?./sbin/nginx?-V nginx?version:?nginx/1.16.0 built?by?gcc?4.8.5?20150623?(Red?Hat?4.8.5-36)?(GCC)? built?with?OpenSSL?1.0.2k-fips??26?Jan?2017 TLS?SNI?support?enabled configure?arguments:?--prefix=/usr/local/nginx?--add-module=/usr/local/nginx-sticky-module?--with-http_stub_status_module?--with-http_ssl_module?--with-http_realip_module
修改nginx.conf,啟用 sticky 功能
upstream?zyi?{ ????#使用sticky,不設置expires則瀏覽器關閉時結束會話 ????sticky?domain=zy.csxiuneng.com?path=/; ????server?localhost:9001; } server?{ ?????listen???????80; ?????server_name??zy.csxiuneng.com; ?????access_log??logs/zy.access.log??main; ?????location?/?{ ???????? ?????????proxy_pass?http://zyi; ?????????proxy_set_header?X-Forwarded-For?$proxy_add_x_forwarded_for; ?????????proxy_set_header?Host?$host; ?????????client_max_body_size?10m; ?????????client_body_buffer_size?256k; ?????????proxy_connect_timeout?90; ?????????proxy_send_timeout?90; ?????????proxy_buffer_size?4k; ?????????proxy_buffers?4?32k; ?????}
sticky 語法:
sticky?[name=route]?[domain=.foo.bar]?[path=/]?[expires=1h]? ???????[hash=index|md5|sha1]?[no_fallback]?[secure]?[httponly]; ????[name=route] 設置用來記錄會話的cookie名稱 ????[domain=.foo.bar] 設置cookie作用的域名 ????[path=/] ??設置cookie作用的URL路徑,默認根目錄 ????[expires=1h]? ?設置cookie的生存期,默認不設置,瀏覽器關閉即失效 ????[hash=index|md5|sha1]???設置cookie中服務器的標識是用明文還是使用md5值,默認使用md5 ????[no_fallback] ?設置該項,當sticky的后端機器掛了以后,nginx返回502?(Bad?Gateway?or?Proxy?Error)?,而不轉發到其他服務器,不建議設置 ????[secure] ??設置啟用安全的cookie,需要HTTPS支持 ????[httponly] ??允許cookie不通過JS泄漏,沒用過
重啟Nginx:./sbin/nginx -s reload
訪問:zy.csxiuneng.com ,可以看到cookies中有一項為route
注意點:
1.同一客戶端,如果啟動時同時發起多個請求,有可能落在不同的后端服務器上
2.由于cookie最初由服務器端下發,如果客戶端禁用cookie,則cookie不會生效。
3.客戶端可能不帶cookie ,Android客戶端發送請求時,一般不會帶上所有的cookie,需要明確指定哪些cookie會帶上。如果希望用sticky做負載均衡,請對Android開發說加上cookie。
4.cookie名稱不要和業務使用的cookie重名。Sticky默認的cookie名稱是route,可以改成任何值
5.客戶端發的第一個請求是不帶cookie的。服務器下發的cookie,在客戶端下一次請求時才能生效。
6.Nginx sticky模塊不能與ip_hash同時使用
如果要添加多個第三方模塊,使用多個 –add-module 指令:
./configure?--prefix=/usr/local/nginx?--add-module=/usr/local/nginx-sticky-module/?--add-module=/usr/local/nginx-http-concat-1.2.2/