搜尋
首頁後端開發php教程nginx+tomcat+redis實作session共享

nginx 作為目前最受歡迎的開源反向代理HTTP Server,用於實現資源快取、web server負載等功能,由於其輕量級、高效能、高可靠度特徵在高績效、高可靠性等功能互聯網專案中有著非常普遍的應用,相關概念網上有豐富的介紹。分散式web server叢集部署後需要實作session共享,針對 tomcat 伺服器的實作方案 hash策略、nginx sticky module等方案,本文主要介紹了使用 redis 伺服器進行 session    相關應用結構參考下圖:二、環境配置  測試環境基於 

Linux CentOS 6.5

nginx 

相關環境,不作詳細描述,本文檢驗配置如下: Version IP_Port

 IP_Port1 7.0.54 2.8.19

三、建置 tomcat-redis-session-manager-master

1、由於原始碼建置基於 gradle,請先設定版本。 2

、從 

github 取得 tomcat-redis-session-manager-master ?1. https://github.com/jcoleman/tomcat-redis-session-manager

3

、找到源碼中的 build.gradle 文件,由於作者使用了第三方倉庫( ),需要註冊帳號,太麻煩,註釋後直接使用

maven

中央倉庫,同時註釋簽名相關腳本並增加依賴包的輸出腳本 

copyJarsdist目錄),修改後的build.gradle 文件如下:view sourceprint?. 003.apply plugin: 'signing'004. 005.group = 'com.orangefunction'006.version = '2.0.0'com )

010. }011. 012.compileJava {013.sourceCompatibility = 1.7

014.targetCompatibility = 1.7

014.targetCompatibility dependencies {

018.compile group: 'org. apache。 compile group: 'org.apache.commons', name: 'commons-pool2', version: '2.2'

021.//compile group: 'commons-codec', name: 'commons- 1.9'

022. 

023.testCompile group: 'junit', name: 'junit', version: '4.+'

024.testile version: '4.+'

core',0.com testCompile 'org.hamcrest:hamcrest-library:1.3'

026.testCompile 'org.mockito:mockito-all:1.9.5'

027.testCompile group: 'org.apachename.cat', coyote', version: '7.0.27'

028.}

029. 

030.task com

(type: Jar, dependsOn:030.task 🠎)🠟S.atoOn:

032.from ' build/docs/javadoc'

033.}

034. 

035.task sourcesJar(type: Jar) {

036.from .

038. }

039. 

040.artifacts {

041.archives jar

042. 

043.archives 

042. 

043.archives 

042. 

043.archives 

042。

046. 

047.//signing {

048.//  sign configurations.archives

into 'dist' 

054.}

055. 

056.​​uploadArchives {

057.repositories {

058.mavenDeployer {p. > signing.signPom(deployment) }

060. 

061 .//repository(url: '

https://oss.sonatype.org/service/local/staging/deploy/maven2/

') {

062.//  authentication(userName: sonatype1

063.//}

064.//repository(url: '

https://oss.sonatype.org/content/repositories/snapshots

') {

,065.////> : sonatypePassword)

066.//}

067. 

068.pom.project {

069.name 'tomcat-redis-session-manager'

069.name 'tomcat-redis-session-manager'

.07. scription ' Tomcat Redis Session Manager is a Tomcat extension to store sessions in Redis'

072.url '

https://github.com/jcoleman/tomcat-redis-session-man

https://github.com/jcoleman/tomcat-redis-034man issueManagement {

075.url '

https://github.com

:jcoleman/tomcat-redis-session-manager/issues'

076.system 'GitHub Issues'

076.system 'GitHub Issues'

077..7. scm {

080.url '

https://github.com

:jcoleman/tomcat-redis-session-manager'

081.connection 'scm:tomgit:

git://github.com/ -redis-session-manager.git

'

082.developerConnection 'scm:git:git@github.com:jcoleman/tomcat-redis-session-manager.git'

083.}

084. 

🜎085.licenses 087.name 'MIT'

088.url '

http://opensource.org/licenses/MIT

'

089.distribution 'repo'

090.}091. 093.developers {

094.developer {

095.id 'jcoleman'

096.name 'James Coleman'

097.e2P.com github.com/jcoleman

'

099.}

100.}

101.}

102.}

103.}

102.}103.}

104. gradle

指令建構原始碼,編譯輸出

tomcat-redis-session-manager-master 

及依賴

jar

🎠 test  copyJars   所有輸出清單檔案如下:四、tomcat 設定  安裝配置兩台伺服器80809090

,並確保都能正常運作,當然如果分佈在不同的主機則可以使用相同連接埠號碼。 五、編寫測試頁  為了區別2

tomcat

的訪問,分別編寫頁面並打包部署:tomcat的訪問,分別編寫頁面並打包部署:

1 編寫測驗頁面,顯示 「 response from tomcat_1 」,而頁面提供按鈕顯示目前session值,包裝並發佈到 tomcat_1 、為tomcat_2

編寫測試頁面,顯示 

「 response from tomcat_2 」

,同時頁面提供按鈕顯示目前session值,包裝並發佈到 tomcat_2; http:// 10.129.221.70:8080 和 

http://10.129.221.70:9090 地址,因為訪問的是不同地址,因為訪問的是不同值肯定不同。 六、tomcat session manager 配置  修改配置使用 tomcat-redis-session-manager-master sion作為管理器作為管理器

1、分別將第三步驟產生的 tomcat-redis-session-manager-master 及依賴jar包覆蓋到 tomcat 安裝目錄的檔案夾雜2、分別修改2

台 tomcat 的 context.xml 管理器,同時指定 redis位址和連接埠。 context.xml 

增加以下設定:view source

print?.Confopmm. essions. RedisSessionHandlerValve'/>3.4.host='localhost'

5.port='6379' 7.maxInactiveInterval='60' />8.3、分別重啟2、分別重啟2七、

nginx 

設定1、修改 default.conf up1、修改 default.conf tomcat Cluster,預設使用輪詢方式。 view sourceprint?01.upstream site { 

ip_hash分發

02.server localhost:8080;

03.server localhost :9090;04.} 05. 

06.server {

07.listen       80;

07.listen       

08.server_name  localhost;

09. 

10.#charset koi8-r;

11.#access_log  /var/log/nginx/log/host.access.log  main;

12. 

13.location / {

14.#root   /usr/share/nginx/html;

15.#index  index.html index.htm;

15.#index  index.html index.htm;15.#index  index.html index.htm;16.index  index.html index.htm; //www.it165.net/pro/webjsp/

" target="_blank"class="keylink">jsp; 索引。 

17.proxy_redirect          關閉;   

18.proxy_set_header         

19.proxy_set_header        X-真實IP       $remote_addr;   

20.proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;   

21.client_max_body_size   

21.client_max_body_size   

21.client_max_body_size   11 28k;   

23.proxy_buffers           32 4k; 

   30;   

26.proxy_read_timeout      30;  

27.proxy_pass http://site;

28. 

29.}

30. 

31.#error_page  404              /404.html;

32。 page   500 502 503 50 4  / 50x .html;

36.location = /50x.html {

37 .root   /usr/share/nginx/html;

38.}

39. 

40.#將 PHP 腳本代理到 Apache 監聽 127.0.0.1:80

41.#

42.#location ~.php$ {

43. 44 .#}

45。 

46.# 將PHP 腳本載入到FastCGI 伺服器,監聽127.0.0.1:9000

47.#

48.#location  ;

50.#    fastcgi _pass 127.0 .0.1:9000;

51.#    fastcgi_index  index.php;

52.#    fastcgi_param           fastcgi_params;包括

5 4.#}

55。 

56.# 如果 Apache 的文檔根

57.# 與 nginx 的文檔根一致

58.#

59.#location ~ /.ht {

59.#location ~ /.ht {

.htaccess .nginx -s re負載

八、配置Tomcat

保存的

session

實體被放置

redis

中,

tomcat

進行對比,

修改session-id是從cookie是從

cookie

中取得的,不同的tomcat儲存

sessioncookie

的位置

儲存

sessioncookie的位置.xml,如下: session修改CookiePath="/">九、測試結果120、訪問直接要求到tomcat_1伺服器,不顯示 「來自tomcat_1的回應」會話等於工作 3'

2、訪問 http://10.129.221.70:9090 直接要求到tomcat_2㟎直接要求到

tomcat_2㟎

直接要求到 ponse from tomcat_2 ” session 值為'56E2FAE376A47F1C0961D722326B8423'

3、訪問80埠)請求到 nginx 反向代理到指定 Web伺服器,由於預設使用輪詢負載方式,    反覆刷新頁面顯示的內容在“ response from tomcat_1 ” 

“ response from tomcat_1 ” 

之間切換,但 session 值維持為 '56E2FAE376A47F1C0961D722326B8423'4cli redis 

伺服器,查看會顯示有 「56E2FAE376A47F1C0961D722326B8423」 keysession 數據,value為序列化數據。 十、至此實現了基於nginx負載平衡下 tomcat 叢集的 session 

一致。 啟動順序:redis——nginx——tomcat

redis/c nginx+tomcat+redis實作session共享 以上就介紹了nginx+tomcat+redis實現session共享,包含了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

 IP_Port

.6.2

 10.129.221.70:80

 tomcat_1

 7.0.54

 10.129.221.70:80800

 10.129.221.70:9090

redis

 10.129.221.70:6379

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
PHP如何識別用戶的會話?PHP如何識別用戶的會話?May 01, 2025 am 12:23 AM

phpIdentifiesauser'ssessionSessionSessionCookiesAndSessionId.1)whiwsession_start()被稱為,phpgeneratesainiquesesesessionIdStoredInacookInAcookInAcienamedInAcienamedphpsessIdontheuser'sbrowser'sbrowser.2)thisIdallowSphptpptpptpptpptpptpptpptoretoreteretrieetrieetrieetrieetrieetrieetreetrieetrieetrieetrieetremthafromtheserver。

確保PHP會議的一些最佳實踐是什麼?確保PHP會議的一些最佳實踐是什麼?May 01, 2025 am 12:22 AM

PHP會話的安全可以通過以下措施實現:1.使用session_regenerate_id()在用戶登錄或重要操作時重新生成會話ID。 2.通過HTTPS協議加密傳輸會話ID。 3.使用session_save_path()指定安全目錄存儲會話數據,並正確設置權限。

PHP會話文件默認存儲在哪裡?PHP會話文件默認存儲在哪裡?May 01, 2025 am 12:15 AM

phpsessionFilesArestoredIntheDirectorySpecifiedBysession.save_path,通常是/tmponunix-likesystemsorc:\ windows \ windows \ temponwindows.tocustomizethis:tocustomizEthis:1)useession_save_save_save_path_path()

您如何從PHP會話中檢索數據?您如何從PHP會話中檢索數據?May 01, 2025 am 12:11 AM

ToretrievedatafromaPHPsession,startthesessionwithsession_start()andaccessvariablesinthe$_SESSIONarray.Forexample:1)Startthesession:session_start().2)Retrievedata:$username=$_SESSION['username'];echo"Welcome,".$username;.Sessionsareserver-si

您如何使用會議來實施購物車?您如何使用會議來實施購物車?May 01, 2025 am 12:10 AM

利用會話構建高效購物車系統的步驟包括:1)理解會話的定義與作用,會話是服務器端的存儲機制,用於跨請求維護用戶狀態;2)實現基本的會話管理,如添加商品到購物車;3)擴展到高級用法,支持商品數量管理和刪除;4)優化性能和安全性,通過持久化會話數據和使用安全的會話標識符。

您如何在PHP中創建和使用接口?您如何在PHP中創建和使用接口?Apr 30, 2025 pm 03:40 PM

本文解釋瞭如何創建,實施和使用PHP中的接口,重點關注其對代碼組織和可維護性的好處。

crypt()和password_hash()有什麼區別?crypt()和password_hash()有什麼區別?Apr 30, 2025 pm 03:39 PM

本文討論了PHP中的crypt()和password_hash()的差異,以進行密碼哈希,重點介紹其實施,安全性和對現代Web應用程序的適用性。

如何防止PHP中的跨站點腳本(XSS)?如何防止PHP中的跨站點腳本(XSS)?Apr 30, 2025 pm 03:38 PM

文章討論了通過輸入驗證,輸出編碼以及使用OWASP ESAPI和HTML淨化器之類的工具來防止PHP中的跨站點腳本(XSS)。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。