nginx怎么實現(xiàn)負載均衡多站點共享Session

本文小編為大家詳細介紹“nginx怎么實現(xiàn)負載均衡多站點共享Session”,內(nèi)容詳細,步驟清晰,細節(jié)處理妥當,希望這篇“nginx怎么實現(xiàn)負載均衡多站點共享Session”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。

企業(yè)建站必須是能夠以充分展現(xiàn)企業(yè)形象為主要目的,是企業(yè)文化與產(chǎn)品對外擴展宣傳的重要窗口,一個合格的網(wǎng)站不僅僅能為公司帶來巨大的互聯(lián)網(wǎng)上的收集和信息發(fā)布平臺,創(chuàng)新互聯(lián)面向各種領域:成都高空作業(yè)車租賃成都網(wǎng)站設計、全網(wǎng)營銷推廣解決方案、網(wǎng)站設計等建站排名服務。


多站點共享session常見的作法有:
?使用.net自動的狀態(tài)服務(asp.net state service);
?使用.net的session數(shù)據(jù)庫;
?使用memcached。
?使用cookie方式實現(xiàn)多個站點間的共享(這種方式只限于幾個站點都在同一域名的情況下);
這里我們就 演練一下 以數(shù)據(jù)庫的形來存儲session,來實現(xiàn)多站點共享session。

首先我們 建好一下站點,如下圖:

nginx怎么實現(xiàn)負載均衡多站點共享Session 

default.aspx 

nginx怎么實現(xiàn)負載均衡多站點共享Session

其中 有二個button  ,setsession 主要是用于給一個 session 賦值(如:session["sharevalue"] = “abcd”) ,

getsession 主要就是獲得 一個 session 值。

具體代碼如下:

nginx怎么實現(xiàn)負載均衡多站點共享Session  

 代碼部分就這么多就行了…

下面就是要配置一下 web.config了 , 其實主要就是在 <system.web>
這個節(jié)點中 增加 machinekey 及 sessionstate 這兩個節(jié)點,
1.增加machinekey 主要作用是:
“按照msdn的標準說法:“對密鑰進行配置,以便將其用于對 forms 身份驗證 cookie 數(shù)據(jù)和視圖狀態(tài)數(shù)據(jù)進行加密和解密,并將其用于對進程外會話狀態(tài)標識進行驗證。”也就是說asp.net的很多加密,都是依賴于machinekey里面的值,例如forms 身份驗證 cookie、viewstate的加密。默認情況下,asp.net的配置是自己動態(tài)生成,如果單臺服務器當然沒問題,但是如果多臺服務器負載均衡,machinekey還采用動態(tài)生成的方式,每臺服務器上的machinekey值不一致,就導致加密出來的結果也不一致,不能共享驗證和viewstate,所以對于多臺服務器負載均衡的情況,一定要在每臺站點配置相同的machinekey?!?,具體可以查一下其它資料。
2.增加 sessionstate 主要是讓 session 保存在數(shù)據(jù)庫中。
具體配置如下:

復制代碼 代碼如下:


<machinekey validationkey="86b6275ba31d3d713e41388692fca68f7d20269411345aa1c17a7386dacc9c46e7ce5f97f556f3cf0a07159659e2706b77731779d2da4b53bc47bffd4fd48a54"
decryptionkey="9421e53e196bb56db11b9c25197a2ad470638efbc604ac74cd29dbbcf79d6046"
validation="sha1" decryption="aes"/>
<sessionstate mode="sqlserver" sqlconnectionstring="data source=pc-07195;initial catalog=awbuisession;persist security info=true;user id=jins;password=js@#$1234" allowcustomsqldatabase="true" cookieless="false" timeout="100"/>



網(wǎng)站部分 這樣就好了。。。 下面就是要配置據(jù)庫了…..

數(shù)據(jù)庫配置:
使用aspnet_regsql.exe工具
asp.net 2.0版本后微軟提供了aspnet_regsql.exe工具可以方便的配置session數(shù)據(jù)庫.該工具位于 web 服務器上的"系統(tǒng)根目錄\microsoft.net\framework\版本號"文件夾中.

使用舉例:

aspnet_regsql.exe -s . -u sa -p 123456 -ssadd -sstype p
-s參數(shù):
表示數(shù)據(jù)庫實例名稱. 可以用"."表示本機.
-u和-p參數(shù):
表示用戶名和密碼.
-e參數(shù):
可以再-u –p 與 -e中選擇一組. –e表示以當前系統(tǒng)用戶通過windows身份驗證登錄數(shù)據(jù)庫, -u -p則是使用sqlserver用戶登錄數(shù)據(jù)庫.
-ssadd / –ssremove 參數(shù):
-ssadd表示是添加session數(shù)據(jù)庫, -ssremove表示移除session數(shù)據(jù)庫.
sstype 參數(shù)說明:
t
將會話數(shù)據(jù)存儲到 sql server tempdb 數(shù)據(jù)庫中。這是默認設置。如果將會話數(shù)據(jù)存儲到 tempdb 數(shù)據(jù)庫中,則在重新啟動 sql server 時將丟失會話數(shù)據(jù)。

將會話數(shù)據(jù)存儲到 aspstate 數(shù)據(jù)庫中,而不是存儲到 tempdb 數(shù)據(jù)庫中。
c
將會話數(shù)據(jù)存儲到自定義數(shù)據(jù)庫中。如果指定 c 選項,則還必須使用 -d 選項包括自定義數(shù)據(jù)庫的名稱。
我的設置是:aspnet_regsql.exe -s . - e -d awbuisession -ssadd -sstype c

好了?;镜?我們就已經(jīng)搞定了。。
現(xiàn)在 我們分別把我們剛建的一個網(wǎng)站 部署 到 iis 中。不過我們既然要負載。至少也的部署兩份。

nginx怎么實現(xiàn)負載均衡多站點共享Session 

我們把 其中一個 服務器中的 defaut.aspx 中 “服務器 1” 改成 “服務器 2” ,這樣做的主要目地是 做一下 區(qū)別! 

具體如下:

 nginx怎么實現(xiàn)負載均衡多站點共享Session

兩個網(wǎng)站的 url分別是:

 server 1:127.0.0.1:8081;

server 2:127.0.0.1:8080;   

ok。下面我們就是 配置 nignx了。

首先 在 nginx\conf 配置  文件中找到 nginx.conf 這個文件 ,就記事本打開, 

nginx怎么實現(xiàn)負載均衡多站點共享Session 

做如上的 設置:

ok。  nginx  這樣配置 就算ok 了。 我們啟動一下 nginx ..

在瀏覽器中 輸入我們 在 nginx 中配置的 url 如:127.0.0.1:8090 

nginx怎么實現(xiàn)負載均衡多站點共享Session 

我們會看到 服務器 1 已經(jīng)開始為我們服務了,我們再點一下 “setsession”來設置一下一個 會話值,

nginx怎么實現(xiàn)負載均衡多站點共享Session

我們會看到 服務器 2 開始 工作。這時我們再點一下 “getsesion”看一下 剛才在 服務器 1 設置 的會話值,結果如下 : 

nginx怎么實現(xiàn)負載均衡多站點共享Session

出現(xiàn)這種情況 ,主要就是在數(shù)據(jù)庫中存儲 一個會話時 沒有做到 服務器1 和服務2的session 共享,主要是 在 

nginx怎么實現(xiàn)負載均衡多站點共享Session

aspstatetempsessions 這個表中的 一個sessionid ,

其中的sessionid包括兩個部分:網(wǎng)站生成的24位sessionid及8位appname對于不同的站點,其appname不同,在能夠在不同站點下使24位sessionid相同的情況下,要保證經(jīng)過組合加上appname后的sessionid相同,可以通過修改存儲過程tempgetappid,使其得到的sessionid與appname無關,修改tempgetappid如下:

復制代碼 代碼如下:


alter procedure [dbo].[tempgetappid]
@appname tappname,
@appid int output
as
set @appname = 'test' --lower(@appname) 修改這里,使多個站點的appname ,為一個固定值。
set @appid = null
select @appid = appid
from [awbuisession].dbo.aspstatetempapplications
where appname = @appname
if @appid is null begin
begin tran
select @appid = appid
from [awbuisession].dbo.aspstatetempapplications with (tablockx)
where appname = @appname
if @appid is null
begin
exec gethashcode @appname, @appid output
insert [awbuisession].dbo.aspstatetempapplications
values
(@appid, @appname)
if @@error = 2627
begin
declare @dupapp tappname
select @dupapp = rtrim(appname)
from [awbuisession].dbo.aspstatetempapplications
where appid = @appid
raiserror('sql session state fatal error: hash-code collision between applications ''%s'' and ''%s''. please rename the 1st application to resolve the problem.',
18, 1, @appname, @dupapp)
end
end
commit
end
return 0


經(jīng)過以上修改之后,下面要實現(xiàn)多個站點共用同一個sessionid.

重啟一下各站點。再在瀏覽一下網(wǎng)站 

nginx怎么實現(xiàn)負載均衡多站點共享Session

點 “setsession”, 

nginx怎么實現(xiàn)負載均衡多站點共享Session

再點:“getsession” 

nginx怎么實現(xiàn)負載均衡多站點共享Session

這樣 我們就看到 服務器2 給出了我們 剛才在 服務器 1 中設置 的會話值了。

我們 再點:“getsession”, 

nginx怎么實現(xiàn)負載均衡多站點共享Session

可以看到  服務器1 和服務器 2 返回的是相同的結果,達到了 “多站點共享session”

 附加一點: session 過期刪除,主要是 在 sql server 代理中的  作業(yè)完成。

nginx怎么實現(xiàn)負載均衡多站點共享Session

讀到這里,這篇“nginx怎么實現(xiàn)負載均衡多站點共享Session”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內(nèi)容的文章,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

名稱欄目:nginx怎么實現(xiàn)負載均衡多站點共享Session
分享鏈接:http://bm7419.com/article42/goeghc.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供建站公司、企業(yè)建站、ChatGPT手機網(wǎng)站建設、商城網(wǎng)站網(wǎng)站建設

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設計公司