Windows+Nginx+Tomcat搭建負載均衡和叢集環境同時實現session共享(一)
摘要:隨著網站的訪問量越來越多,所以就考慮給網站增加伺服器了,現在比較流行的做法就是給網站做叢集環境,下面我把我做的過程記錄一下,方便日後檢視,同時也希望可以幫助到有需要的朋友!
一:首先是環境:
1.jdk 1.6.0_45
2.tomcat 6.0.44
3.nginx 1.8.0
二:jdk,tomcat,nginx的安裝:
1.jdk的安裝請參考:點選開啟連結
2.nginx的安裝步驟
(1).首先下載nginx的windows版本,我這裡下載的nginx 1.8.0版本,下載地址:點選開啟連結
(2).下載完成後,直接解壓到一個沒有中文的路徑下,我這裡是解壓到:D:\server\nginx-1.8.0
(3).通過dos命令進入nginx-1.8.0目錄下啟動nginx即可
(4)Windows下操作Nginx命令
1.啟動 nginx.exe start nginx 2.停止 nginx -s stop nginx -s quitstop表示立即停止nginx,不儲存相關資訊
quit表示正常退出nginx,並儲存相關資訊
3.重啟 nginx -s reload 重啟(因為改變了配置,需要重啟)3.tomcat的安裝(直接到tomcat的官網下載解壓縮版的即可)
三:把下載下載的tomcat分別複製3個,一共是個,其中三個做qdksDemo的叢集環境用,另外三個做qdkyDemo的叢集環境用,具體截圖如下:
複製完成後,要分別修改每個tomcat的埠和session共享的配置,具體的配置如下:
1.第一處要修改的埠號
<Server port="8005" shutdown="SHUTDOWN">
2.第二處要修改的埠號
<Connector port="8081" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
3.第三處要修改的埠號
4.第四處要增加的session共享配置,這個可以檢視tomcat的官方文件,裡面有配置直接複製到server.xml中就可以,地址:http://tomcat.apache.org/tomcat-6.0-doc/cluster-howto.html<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="6">
<Manager className="org.apache.catalina.ha.session.BackupManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"
mapSendOptions="6"/>
<!--
<Manager className="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"/>
-->
<Channel className="org.apache.catalina.tribes.group.GroupChannel">
<Membership className="org.apache.catalina.tribes.membership.McastService"
address="228.0.0.4"
port="45564"
frequency="500"
dropTime="3000"/>
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="auto"
port="5000"
selectorTimeout="100"
maxThreads="6"/>
<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>
</Channel>
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
filter=".*\.gif|.*\.js|.*\.jpeg|.*\.jpg|.*\.png|.*\.htm|.*\.html|.*\.css|.*\.txt"/>
<Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
這樣一個tomcat的配置檔案就修改完成了,其他5個tomcat的配置和這個流程一樣,只是對於的埠不能一樣
四:nginx的配置:
1.首先在nginx的目錄下新建一個新的資料夾來存放不同專案之間的叢集配置檔案,如下圖:
2.在manyvhost資料夾裡面新建一個配置檔案,分別對於兩個專案的叢集配置,如下圖:
3.qdks.conf和qdky.conf的配置內容如下:
(1).qdks.conf
upstream qdks {
server localhost:8081 weight=1;
server localhost:8082 weight=1;
server localhost:8083 weight=1;
}
server {
listen 8086;
server_name 192.168.1.103;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
root /data/projects/ycp/bill;
# - rewrite: if( path ~ "^/assets/(.*)" ) goto "/public/assets/$1"
# location ~ ^/static/assets/(.*)$
# {
#alias /data/projects/payment/web/public/assets/$1;
# access_log off;
# #expires 3d;
# }
location / {
index index.html index.htm index.jsp;
}
location ~ .* {
# proxy_pass_header Server;
proxy_set_header Host $http_host;
# proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
add_header Pragma "no-cache";
proxy_pass http://qdks;
}
rewrite ^/admin/?$ /admin/login redirect;
# for rewrite
rewrite ^/(channel|admin|mobile|api|web)/(.*)$ /public/index.php/$2 last;
#redirect to mobile wap
#rewrite ^$ /m redirect;
#rewrite ^/$ /mobile/user redirect;
}
(2).qdky.conf
upstream qdky {
server localhost:7081 weight=1;
server localhost:7082 weight=1;
server localhost:7083 weight=1;
}
server {
listen 7086;
server_name 192.168.1.103;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
root /data/projects/ycp/bill;
# - rewrite: if( path ~ "^/assets/(.*)" ) goto "/public/assets/$1"
# location ~ ^/static/assets/(.*)$
# {
#alias /data/projects/payment/web/public/assets/$1;
# access_log off;
# #expires 3d;
# }
location / {
index index.html index.htm index.jsp;
}
location ~ .* {
# proxy_pass_header Server;
proxy_set_header Host $http_host;
# proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
add_header Pragma "no-cache";
proxy_pass http://qdky;
}
rewrite ^/admin/?$ /admin/login redirect;
# for rewrite
rewrite ^/(channel|admin|mobile|api|web)/(.*)$ /public/index.php/$2 last;
#redirect to mobile wap
#rewrite ^$ /m redirect;
#rewrite ^/$ /mobile/user redirect;
}
這兩個配置檔案就是我當前環境下的配置檔案,其中qdks的專案的埠號8086,下面對應三個tomcat的埠為:8081,8082,8083;qdky的專案的埠號7086,下面對應三個tomcat的埠號為:7081,7082,7083
4.最後在nginx的conf目錄下的nginx.conf核心配置檔案中引入上面的兩個專案的叢集配置,如下:
#user nobody;
worker_processes 1;
error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;
pid logs/nginx.pid;
events {
worker_connections 5000;
}
http {
include mime.types;
default_type application/octet-stream;
#隱藏nginx的版本號
server_tokens off;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
#access日誌存放路徑和格式
access_log logs/access.log;
#提升檔案傳輸效能
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
#keepalive_timeout 65;
#設定客戶端能夠請求的單個檔案大小
client_max_body_size 300m;
keepalive_timeout 75;
#proxy引數
#跟後端伺服器連線的超時時間,發起握手等候響應超時時間
proxy_connect_timeout 5;
#連線成功後,等候後端伺服器的響應時間
proxy_read_timeout 600;
#後端伺服器資料回傳時間
proxy_send_timeout 600;
#代理請求快取區
proxy_buffer_size 16k;
#同上,告訴nginx儲存單個用的幾個buffer、最大用多空間
proxy_buffers 4 64k;
#如果系統很忙時可以申請更大的proxy_buffers,官方推薦*2
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
#gzip on;
#開啟壓縮功能
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
#壓縮級別從低到高1-9
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#qdksDemo和qdkyDemo專案的虛擬目錄(用絕對路徑表示)
include D:/server/nginx-1.8.0/manyvhost/qdks.conf;
include D:/server/nginx-1.8.0/manyvhost/qdky.conf;
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 80;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
五:分別部署對應的專案,來測試我們上面配置的叢集環境是否正常
1.下載對應的專案原始碼,下載後解壓到你的本地workspace下面,我的路徑是:D:\workspace_qdexam\qdksDemo和D:\workspace_qdexam\qdkyDemo
2.分別部署到對應的tomcat下面,這裡我採用的是直接對映路徑的方法
(1).qdksDemo的部署方法是,在對應的三個tomcat的conf\Catalina\localhost目錄下新建一個配置檔案qdksDemo.xml,該配置檔案的內容如下:
<Context path="/qdksDemo" docBase="D:\workspace_qdexam\qdksDemo\WebContent" debug="0" privileged="true"> </Context>
(2).qdkyDemo的部署方法是,在對應的三個tomat的conf\Catalina\localhost目錄下新建一個配置檔案qdkyDemo.xml,該配置檔案的內容如下:
<Context path="/qdkyDemo" docBase="D:\workspace_qdexam\qdkyDemo\WebContent" debug="0" privileged="true"></Context>
3.分別啟動這6個tomcat,如下圖:
4.啟動nginx,如下圖:
5.最後通過瀏覽器訪問這兩個專案,如下圖:
分別點選重新整理,你會發現上面的實際訪問埠會變,但是下面的Session ID是不變的,這就說明基於nginx和tocmat的叢集搭建成功了!
最後我把我的tomcat和nginx都打包上傳,供大家參考!
相關推薦
Windows+Nginx+Tomcat搭建負載均衡和叢集環境同時實現session共享(一)
摘要:隨著網站的訪問量越來越多,所以就考慮給網站增加伺服器了,現在比較流行的做法就是給網站做叢集環境,下面我把我做的過程記錄一下,方便日後檢視,同時也希望可以幫助到有需要的朋友! 一:首先是環境: 1.jdk 1.6.0_45 2.tomcat 6.0.44 3.nginx
乾貨:Nginx+Tomcat搭建負載均衡叢集(轉載)
一、 工具 nginx-1.8.0 apache-tomcat-6.0.33 二、 目標 實現高效能負載均衡
nginx+tomcat配置負載均衡叢集
1,使用工具: nginx(http://nginx.org/en/download.html) apache-tomcat(https://tomcat.apache.org/download-70.cgi) 2,配置目的: 傳統單伺服器專案(例如一個tomcat下有幾個專案),這
Nginx+Tomcat 配置負載均衡叢集
一、Hello world 1、前期環境準備 準備兩個解壓版tomcat,如何同時啟動兩個tomcat,請看我的另一篇文章《一臺機器同時啟動多個tomcat》。 nginx官網下載解壓版nginx。 建立一個簡單的web專案。為了直觀的區分訪問的哪個tomcat,
nginx之 nginx + tomcat + redis 負載均衡且session一致性
權限 gzip 通過 del 跳轉 home val zhang ctp 說明: 本文描述的是 nginx + tomcat + redis 實現應用負載均衡且滿足session一致性,從安裝到配置的全部過程,供大家學習!nginx 代理服務器ip: 10.219.24.2
apache+tomcat 搭建負載均衡系統
apache tomcat 負載均衡 apache+tomcat+mod_jk 搭建負載均衡系統。0.os系統采用centos6.8 x64 2.6.32-642.el6.x86_641.首先安裝好jdk環境本次采用jdk-8u111-linux-x64.gz jdk和jre的安裝目錄要不同,否
Nginx+Tomcat簡單負載均衡
啟動 /usr nbsp webapp server XML img 9.png 測試 Nginx,Apache安裝完成 復制一份copy,變兩個Tomcat apache-tomcat-9.0.0.M26-a apache-tomcat-9.0.0.M26-b
nginx+tomcat配置負載均衡集群
忽略 利用 jdk1.7 刷新 itl 頁面 方向 style local 一、Hello world1、前期環境準備準備兩個解壓版tomcat,如何同時啟動兩個tomcat,方法如下:首先去apache tomcat官網下載一個tomcat解壓版。解壓該壓縮包,生成n份t
Nginx + Tomcat 配置負載均衡集群簡單實例
hash weight 用戶 index 文件服務器 修改配置文件 ads 配置文件 ace 一、Hello world 1、前期環境準備 準備兩個解壓版tomcat,如何同時啟動兩個tomcat,請看我的另一篇文章《一臺機器同時啟動多個tomcat》。 nginx官網下
nginx如何搭建負載均衡?
nginx負載均衡upstream resinserver{ # 定義負載均衡設備的Ip及設備狀態ip_hash;server 10.1.1.1:8000 down;server 10.1.1.2:8080 weight=10 max_fails=3 fail_timeout=10s;s
93.Nginx配置:負載均衡和SSL配置
Nginx配置:負載均衡和SSL配置一、負載均衡 負載均衡在服務端開發中算是一個比較重要的特性。因為Nginx除了作為常規的Web服務器外,還會被大規模的用於反向代理前端,因為Nginx的異步框架可以處理很大的並發請求,把這些並發請求hold住之後就可以分發給後臺服務端(backend servers,也叫做
nginx+tomcat+redis負載均衡,實現session共享
session共享實驗環境: 系統: centos 7.4 3.10.0-327.el7.x86_64 docker: 18.03.0-ce docker-compose:docker-compose version 1.21.0 redis: 4.0.9 nginx: 1.12.2 tomcat:8.5.
Nginx+Tomcat 部署負載均衡集群
size AS 技術 tle DG itl 靜態 lse -m Nginx是一款輕量級的Web服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器。在Java的Web架構中,通常使用Tomcat和Nginx進行配合,Nginx作為反向代理服務器,可以對後臺的To
Nginx + Tomcat 配置負載均衡集群
term 機器 ati lis 官網下載 規則 ron mar 服務器性能 一、Hello world 1、前期環境準備 準備兩個解壓版tomcat,如何同時啟動兩個tomcat,請看我的另一篇文章《一臺機器同時啟動多個tomcat》。 nginx官網下載解壓版nginx。
Nginx+Tomcat做負載均衡時一臺伺服器宕機實現自動切換
思路:有兩種方式。 第一種:設定一臺伺服器為備機,只有當訪問的伺服器異常時才會訪問它; 第二種:設定伺服器轉發請求超時時間。 一、設定備機: 在安裝目錄下(例E:\nginx-1.14.0\conf)開啟nginx.conf修改 upstream netitcast.com {
nginx+php負載均衡叢集環境中的session共享方案梳理
在網站使用nginx+php做負載均衡情況下,同一個IP訪問同一個頁面會被分配到不同的伺服器上,如果session不同步的話,就會出現很多問題,比如說最常見的登入狀態。 下面羅列幾種nginx負載均衡中session同步的方式 1)不使用session,換用cookiesession是存放在伺服器端的,c
nginx Tomcat keepalived 負載均衡 高可用配置
1.什麼是負載均衡 負載均衡(Load Balance)就是將伺服器的單位時間內的訪問量分攤到多臺伺服器上進行執行的解決方案,從而共同完成工作任務。2.nginx 負載均衡演算法1).輪詢(預設) 每個請求按時間順序逐一分配到不同的後端伺服器,
nginx+tomcat+ssl+負載均衡
一、startssl證書申請 請參考如下連結 http://www.zntec.cn/archives/startssl.html 最終生成四個檔案: ssl.key ssl.crt ca.pem sub.class1.server.ca.pem 二、n
Nginx服務搭建負載均衡,反向代理,快取加速,訪問分散式檔案系統高可用
主配置檔案如下:[[email protected]~]#vim /usr/local/nginx/conf/nginx.conf server{ listen 8099 //埠號 location / { autoindex on; autoinde
Nginx+Tomcat+memcached負載均衡實現session共享
http://blog.csdn.net/bluejoe2000/article/details/24883967/ http://www.cnblogs.com/mouseIT/p/4176238.html