nginx会话保持值sticky模块
閱讀目錄
- 1.Sticky工作原理
- 2.重新編譯nginx增加nginx-sticky-module模塊
- 3.sticky模塊的使用
- 4.其他的參數(shù):語法
- 5.其他需要注意的
nginx會話保持之nginx-sticky-module模塊
在使用負(fù)載均衡的時候會遇到會話保持的問題,常用的方法有:
1.ip hash,根據(jù)客戶端的IP,將請求分配到不同的服務(wù)器上;
2.cookie,服務(wù)器給客戶端下發(fā)一個cookie,具有特定cookie的請求會分配給它的發(fā)布者,注意:cookie需要瀏覽器支持,且有時候會泄露數(shù)據(jù)
1.Sticky工作原理
Sticky是nginx的一個模塊,它是基于cookie的一種nginx的負(fù)載均衡解決方案,通過分發(fā)和識別cookie,來使同一個客戶端的請求落在同一臺服務(wù)器上,默認(rèn)標(biāo)識名為route
1.客戶端首次發(fā)起訪問請求,nginx接收后,發(fā)現(xiàn)請求頭沒有cookie,則以輪詢方式將請求分發(fā)給后端服務(wù)器。
2.后端服務(wù)器處理完請求,將響應(yīng)數(shù)據(jù)返回給nginx。
3.此時nginx生成帶route的cookie,返回給客戶端。route的值與后端服務(wù)器對應(yīng),可能是明文,也可能是md5、sha1等Hash值
4.客戶端接收請求,并保存帶route的cookie。
5.當(dāng)客戶端下一次發(fā)送請求時,會帶上route,nginx根據(jù)接收到的cookie中的route值,轉(zhuǎn)發(fā)給對應(yīng)的后端服務(wù)器。
2.重新編譯nginx增加nginx-sticky-module模塊
查詢bitbucket.org上的該模塊相關(guān)文檔
https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/overview# 查詢當(dāng)前的nginx編譯參數(shù)可以使用以下命令:
[root@nginx ~]# mkdir -p /server/tools
[root@nginx ~]# cd /server/tools
[root@nginx tools]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.8.0
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)?
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module
# 下載該模塊的軟件包()
# 其他平臺上的nginx-sticky-module模塊包多是國外的資源的不方便下載,找到GITHUB上面的該軟件包
# 解壓
[root@nginx tools]# unzip -D nginx-goodies-nginx-sticky-module-ng-08a395c66e42.zip [root@nginx tools]# mv nginx-goodies-nginx-sticky-module-ng-08a395c66e42 nginx-sticky-module-ng# 備份之前的nginx目錄(注意:nginx日志可能很大)
[root@nginx tools]# cp -rf /usr/local/nginx/ /server/backup/# 進(jìn)入到之前編譯好的nginx目錄下,重新進(jìn)行編譯安裝
# 注意:是覆蓋安裝
[root@nginx tools]# cd nginx-1.8.0 [root@nginx nginx-1.8.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --add-module=/server/tools/nginx-sticky-module-ng [root@nginx nginx-1.8.0]# make [root@nginx nginx-1.8.0]# make install# 安裝完再次查看nginx編譯參數(shù)
[root@nginx tools]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.8.0
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)?
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --add-module=/server/tools/nginx-sticky-module-ng
# 需要重新啟動nginx才可以使用sticky模塊
[root@nginx tools]# service nginx restart# 編輯配置文件,實例:
upstream www_web_com { # ip_hash;sticky expires=1h domain=web.com path=/;server 10.0.0.16:8080;server 10.0.0.17:8080; }# 具體的配置根據(jù)公司的相關(guān)業(yè)務(wù)配置即可
# 之后打開網(wǎng)站進(jìn)行測試,使用sticky的情況下,不管怎么刷新都是如下結(jié)果
# 不使用Nginx sticky模塊時,刷幾次就變了(有時候刷一次,有時候多刷幾次)
備注:每臺后端真實服務(wù)器都會有一個唯一的route值,所以不管你真實服務(wù)器前端有幾個裝了sticky的nginx代理,他都是不會變化的.
3.sticky模塊的使用
# 位置:upstream標(biāo)簽內(nèi)
upstream {sticky;server 127.0.0.1:9000;server 127.0.0.1:9001;server 127.0.0.1:9002; }# 參數(shù),解析
sticky [name=route] [domain=.foo.bar] [path=/] [expires=1h] [hash=index|md5|sha1] [no_fallback] [secure] [httponly];[name=route] 設(shè)置用來記錄會話的cookie名稱
[domain=.foo.bar] 設(shè)置cookie作用的域名
[path=/] ? 設(shè)置cookie作用的URL路徑,默認(rèn)根目錄
[expires=1h] ?設(shè)置cookie的生存期,默認(rèn)不設(shè)置,瀏覽器關(guān)閉即失效,需要是大于1秒的值
[hash=index|md5|sha1] ? 設(shè)置cookie中服務(wù)器的標(biāo)識是用明文還是使用md5值,默認(rèn)使用md5
[no_fallback] ?設(shè)置該項,當(dāng)sticky的后端機(jī)器掛了以后,nginx返回502 (Bad Gateway or Proxy Error) ,而不轉(zhuǎn)發(fā)到其他服務(wù)器,不建議設(shè)置
[secure] ? 設(shè)置啟用安全的cookie,需要HTTPS支持
[httponly] ? 允許cookie不通過JS泄漏,沒用過
官方說明:
name: the name of the cookies used to track the persistant upstream srv; default: route
domain: the domain in which the cookie will be valid default: nothing. Let the browser handle this.
path: the path in which the cookie will be valid default: /
expires: the validity duration of the cookie default: nothing. It's a session cookie. restriction: must be a duration greater than one second
hash: the hash mechanism to encode upstream server. It cant' be used with hmac. default: md5
md5|sha1: well known hash
index: it's not hashed, an in-memory index is used instead, it's quicker and the overhead is shorter Warning: the matching against upstream servers list is inconsistent. So, at reload, if upstreams servers has changed, index values are not guaranted to correspond to the same server as before! USE IT WITH CAUTION and only if you need to!
hmac: the HMAC hash mechanism to encode upstream server It's like the hash mechanism but it uses hmac_key to secure the hashing. It can't be used with hash. md5|sha1: well known hash default: none. see hash.
hmac_key: the key to use with hmac. It's mandatory when hmac is set default: nothing.
no_fallback: when this flag is set, nginx will return a 502 (Bad Gateway or Proxy Error) if a request comes with a cookie and the corresponding backend is unavailable.
secure enable secure cookies; transferred only via https
httponly enable cookies not to be leaked via js
4.其他的參數(shù):語法
以下內(nèi)容摘錄于:http://blog.csdn.net/yu870646595/article/details/52056340
session_sticky [cookie=name] [domain=your_domain] [path=your_path] [maxage=time][mode=insert|rewrite|prefix] [option=indirect] [maxidle=time] [maxlife=time] [fallback=on|off] [hash=plain|md5]mode設(shè)置cookie的模式:
insert: 在回復(fù)中本模塊通過Set-Cookie頭直接插入相應(yīng)名稱的cookie。
prefix: 不會生成新的cookie,但會在響應(yīng)的cookie值前面加上特定的前綴,當(dāng)瀏覽器帶著這個有特定標(biāo)識的cookie再次請求時,模塊在傳給后端服務(wù)前先刪除加入的前綴,后端服務(wù)拿到的還是原來的cookie值,這些動作對后端透明。如:”Cookie: NAME=SRV~VALUE”。
rewrite: 使用服務(wù)端標(biāo)識覆蓋后端設(shè)置的用于session sticky的cookie。如果后端服務(wù)在響應(yīng)頭中沒有設(shè)置該cookie,則認(rèn)為該請求不需要進(jìn)行session sticky,使用這種模式,后端服務(wù)可以控制哪些請求需要sesstion sticky,哪些請求不需要。
option設(shè)置用于session sticky的cookie的選項,可設(shè)置成indirect或direct。indirect不會將session sticky的cookie傳送給后端服務(wù),該cookie對后端應(yīng)用完全透明。direct則與indirect相反。
maxidle設(shè)置session cookie的最長空閑的超時時間
maxlife設(shè)置session cookie的最長生存期
maxage是cookie的生存期。不設(shè)置時,瀏覽器或App關(guān)閉后就失效。下次啟動時,又會隨機(jī)分配后端服務(wù)器。所以如果希望該客戶端的請求長期落在同一臺后端服務(wù)器上,可以設(shè)置maxage。
hash不論是明文還是hash值,都有固定的數(shù)目。因為hash是server的標(biāo)識,所以有多少個server,就有等同數(shù)量的hash值。
5.其他需要注意的
1.同一客戶端的請求,有可能落在不同的后端服務(wù)器上
如果客戶端啟動時同時發(fā)起多個請求。由于這些請求都沒帶cookie,所以服務(wù)器會隨機(jī)選擇后端服務(wù)器,返回不同的cookie。當(dāng)這些請求中的最后一個請求返回時,客戶端的cookie才會穩(wěn)定下來,值以最后返回的cookie為準(zhǔn)。
2.cookie不一定生效
由于cookie最初由服務(wù)器端下發(fā),如果客戶端禁用cookie,則cookie不會生效。
3.客戶端可能不帶cookie
Android客戶端發(fā)送請求時,一般不會帶上所有的cookie,需要明確指定哪些cookie會帶上。如果希望用sticky做負(fù)載均衡,請對Android開發(fā)說加上cookie。
4.cookie名稱不要和業(yè)務(wù)使用的cookie重名。Sticky默認(rèn)的cookie名稱是route,可以改成任何值。
5.客戶端發(fā)的第一個請求是不帶cookie的。服務(wù)器下發(fā)的cookie,在客戶端下一次請求時才能生效。
6.Nginx sticky模塊不能與ip_hash同時使用
?
其他使用方法可以參考以下:
http://tengine.taobao.org/document_cn/http_upstream_session_sticky_cn.html
轉(zhuǎn)自:https://www.cnblogs.com/tssc/p/7481885.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的nginx会话保持值sticky模块的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux调试分析诊断利器----str
- 下一篇: nginx1.11.*版本追加安装sti