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/>属性。
完成,现在可以测试了。