Windows下Nginx+Tomcat負載均衡、Session複製
一、 下載Nginx、Tomcat
Nginx下載地址:http://nginx.org/en/download.html,建議使用穩定版,現在使用nginx-1.12.1。
Tomcat下載地址:http://tomcat.apache.org/download-80.cgi#8.0.45,可根據需要下載其他版本。根據Tomcat版本不同,session複製配置可能不同。
二、 配置Nginx
將nginx解壓,並在conf中的nginx.conf進行相關配置。
具體配置:
#user nobody;
worker_processes 1;#工作進程的個數,一般與計算機的cpu核數一致
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;#單個進程最大連接數(最大連接數=連接數*進程數)
}
http {
include mime.types;#文件擴展名與文件類型映射表
default_type application/octet-stream;#默認文件類型
client_max_body_size 8m;#客戶端提交信息最大值
client_body_buffer_size 128k;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
#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;
sendfile on;#開啓高效文件傳輸模式,sendfile指令指定nginx是否調用sendfile函數來輸出文件,對於普通應用設爲 on,如果用來進行下載等應用磁盤IO重負載應用,可設置爲off,以平衡磁盤與網絡I/O處理速度,降低系統的負載。注意:如果圖片顯示不正常把這個改成off。
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;#長連接超時時間,單位是秒
#開啓zip網頁壓縮
gzip on;#啓用Gizp壓縮
gzip_min_length 1k;
gzip_buffers 4 8k;
gzip_http_version 1.1;
gzip_types text/plainapplication/x-javascript text/css application/xml;
#服務器的集羣,該部分是被代理的服務器。
upstream chipSeal { #服務器集羣名字
server 192.168.14.242:8211 weight=1;#服務器配置 weight是權重的意思,權重越大,分配的概率越大。
#server 192.168.3.180:8212 weight=1;
server 192.168.14.233:8213 weight=1;
#ip_hash; #每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個後端服務器,可以解決session的問題
#server192.168.3.180:8201 weight=1 max_fails=2 fail_timeout=30s;
#server192.168.2.4:8190 weight=2 max_fails=2 fail_timeout=30s;
}
#當前的Nginx的配置,每一個server相當於一個代理服務器;該部分是外部訪問的IP、端口號
server {
listen 8099;#監聽80端口,可以改成其他端口
server_name 192.168.14.242;#當前服務的域名,可以有多個,用空格分隔
charset utf-8;
#access_log logs/host.access.log main;
#表示匹配的路徑,這時配置了/表示所有請求都被匹配到這裏
location / {
index index.jspindex.html;#當沒有指定主頁時,默認會選擇這個指定的文件,可多個,空格分隔
proxy_passhttp://chipSeal; #請求轉向自定義的服務器列表
proxy_redirectdefault;
#添加如下3個配置後,當一臺server宕機,切換速度會很快,此時配置是1秒
#proxy_connect_timeout 1;
#proxy_send_timeout 1;
#proxy_read_timeout 1;
proxy_set_headerHost $http_host;
proxy_set_headerCookie $http_cookie;
proxy_set_headerX-Real-IP $remote_addr;
proxy_set_headerX-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_headerX-Forwarded-Proto $scheme;
#deny192.168.2.3; #是訪問控制設置,禁止某個IP或者某個IP段訪問。也可以指定unix,允許socket的訪問。
#allow 192.168.2.0/24; #是訪問控制設置,允許某個IP或者某個IP段訪問。也可以指定unix,允許socket的訪問。
}
#error_page 404 /404.html;
# redirect servererror pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location =/50x.html {
root html;
}
# proxy the PHPscripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php${
# proxy_pass http://127.0.0.1;
#}
# pass the PHPscripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php${
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to.htaccess files, if Apache's document root
# concurs withnginx's one
#
#location ~ /\.ht{
# deny all;
#}
}
# another virtual hostusing mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
啓動腳本:
@echo off
e:
cd E:\nginx-1.12.1\
echo "nginx is starting on port 8099"
start "" "nginx.exe"
pause
關閉腳本:
@echo off
e:
cd E:\nginx-1.12.1\
tasklist | findstr /i "nginx.exe"
echo "nginx is running, stopping..."
rem nginx -s stop
TASKKILL /F /IM nginx.exe /T
echo "stop ok"
pause
重啓腳本:
@echo off
e:
cd E:\nginx-1.12.1\
tasklist | findstr /i "nginx.exe"
echo "nginx is running, stopping..."
rem nginx -s stop
TASKKILL /F /IM nginx.exe /T
echo "stop ok"
start "" "nginx.exe"
pause
Nginx常用命令:
在nginx.exe目錄,打開命令行工具,用命令 啓動/關閉/重啓nginx
start nginx : 啓動nginx
nginx -s reload :修改配置後重新加載生效,重新加載配置文件
nginx -s reopen :重新打開日誌文件
nginx -t -c /path/to/nginx.conf 測試nginx配置文件是否正確
關閉nginx:
nginx -s stop :快速停止nginx
nginx -s quit :完整有序的停止nginx
三、 配置Tomcat
可在多個服務器中配置Tomcat,服務器需要在一個網段中,若不在一個網段內,可能需要設置路由。
共同設置
在Tomcat下conf/ server.xml中,找到<Engine name="Catalina"defaultHost="localhost">,更改爲<Enginename="Catalina" defaultHost="localhost"jvmRoute="jvm4111">,其中jvmRoute在各個Tomcat中不相同。
在<Engine name="Catalina"defaultHost="localhost" jvmRoute="jvm4111">增加如下:
配置方式1
<ClusterclassName="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="6">
<ManagerclassName="org.apache.catalina.ha.session.BackupManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"
mapSendOptions="6"/>
<!--
<ManagerclassName="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"/>
-->
<ChannelclassName="org.apache.catalina.tribes.group.GroupChannel">
<MembershipclassName="org.apache.catalina.tribes.membership.McastService"
address="228.0.0.4"
port="45564"
frequency="500"
dropTime="3000"/>
<ReceiverclassName="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="auto"
port="5000"
selectorTimeout="100"
maxThreads="6"/>
<SenderclassName="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<TransportclassName="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>
<InterceptorclassName="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<InterceptorclassName="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>
</Channel>
<ValveclassName="org.apache.catalina.ha.tcp.ReplicationValve"
filter=".*\.gif|.*\.js|.*\.jpeg|.*\.jpg|.*\.png|.*\.htm|.*\.html|.*\.css|.*\.txt"/>
<DeployerclassName="org.apache.catalina.ha.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/>
<ClusterListenerclassName="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
配置方式2
<ClusterclassName="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="8">
<Manager className="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"/>
<ChannelclassName="org.apache.catalina.tribes.group.GroupChannel">
<MembershipclassName="org.apache.catalina.tribes.membership.McastService"
address="228.0.0.4"
port="45564"
frequency="500"
dropTime="3000"/>
<ReceiverclassName="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="auto"
port="4000"
autoBind="100"
selectorTimeout="5000"
maxThreads="6"/>
<SenderclassName="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<TransportclassName="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>
<InterceptorclassName="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<InterceptorclassName="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
</Channel>
<ValveclassName="org.apache.catalina.ha.tcp.ReplicationValve"
filter=""/>
<ValveclassName="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
<DeployerclassName="org.apache.catalina.ha.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/>
<ClusterListenerclassName="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
添加項目
在<Host name="localhost" appBase="webapps" unpackWARs="true"autoDeploy="true">下增加<Context path=""reloadable="true" docBase="E:/temp/WebTest1"/>,docBase爲項目位置。
四、 項目配置
在項目的web.xml中增加<distributable/>屬性。
完成,現在可以測試了。