文章来源:http://blog.csdn.net/xq328220454/article/details/39257105
这几天研究了一下Nginx和tomcat的集群环境搭建,同时支持HTTP请求和HTTPS请求,研究了一下,整理一下,分享给大家。还是有很多不足的地方,大家发现问题,希望多多指正。话不多说,上代码~
因为研究的条件有限,我是在本机上搭建两个Tomcat进行模拟的。
一、环境准备
1.Tomcat7:http://tomcat.apache.org/download-70.cgi
2.Nginx1.7.4:http://nginx.org/en/download.html
3.Memcached:http://www.newasp.net/soft/63735.html 实在没有找到别的太好的下载地址~
4.必备的JAR包:
- memcached-session-manager-1.6.5.jar、memcached-session-manager-tc7-1.6.5(tomcat6 请选tc6)、javolution-5.4.3.1.jar、msm-javolution-serializer-1.6.5.jar、spymemcached-2.10.3.jar
这些JAR都可以在:http://code.google.com/p/memcached-session-manager/downloads/list 这里下载到,网速慢,FQ吧~
- couchbase-client-1.4.4.jar
下载地址:http://packages.couchbase.com/clients/java/1.4.4/Couchbase-Java-Client-1.4.4.zip 这个JAR在这个zip文件中
二、Tomcat配置
- 解压并复制Tomcat,分别为apache-tomcat-7.0.55-01和apache-tomcat-7.0.55-02
- 修改server.xml文件,同时添加HTTPS支持和Tomcat双开的配置
apache-tomcat-7.0.55-01配置
- <?xml version='1.0' encoding='utf-8'?>
- <span style="color:#ff0000;"><Server port="8005" shutdown="SHUTDOWN"></span>
- <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
- <Listener className="org.apache.catalina.core.JasperListener" />
- <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
- <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
- <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
-
- <GlobalNamingResources>
- <Resource name="UserDatabase" auth="Container"
- type="org.apache.catalina.UserDatabase"
- description="User database that can be updated and saved"
- factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
- pathname="conf/tomcat-users.xml" />
- </GlobalNamingResources>
- <Service name="Catalina">
- <span style="color:#ff0000;"><Connector port="8080" protocol="HTTP/1.1"
- connectionTimeout="20000"
- redirectPort="8443" /></span>
- <span style="color:#ff0000;"> <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true" SSLCertificateFile="E:/tomcat.crt" SSLCertificateKeyFile="E:/tomcat.key"
- maxThreads="150" scheme="https" secure="true"
- clientAuth="false" keystoreFile="E:/tomcat.keystore" keystorePass="123456" sslProtocol="TLS" />
- <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /></span>
- <Engine name="Catalina" jvmRoute="<span style="color:#ff0000;">tomcat1</span>" defaultHost="localhost">
- <Realm className="org.apache.catalina.realm.LockOutRealm">
- <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
- resourceName="UserDatabase"/>
- </Realm>
-
- <Host name="localhost" appBase="webapps"
- unpackWARs="true" autoDeploy="true">
- <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
- prefix="localhost_access_log." suffix=".txt"
- pattern="%h %l %u %t "%r" %s %b" />
-
- </Host>
- </Engine>
- </Service>
- </Server>
apache-tomcat-7.0.55-02配置
- <?xml version='1.0' encoding='utf-8'?>
- <span style="color:#ff0000;"><Server port="8006" shutdown="SHUTDOWN"></span>
- <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
- <Listener className="org.apache.catalina.core.JasperListener" />
- <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
- <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
- <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
-
- <GlobalNamingResources>
- <Resource name="UserDatabase" auth="Container"
- type="org.apache.catalina.UserDatabase"
- description="User database that can be updated and saved"
- factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
- pathname="conf/tomcat-users.xml" />
- </GlobalNamingResources>
- <Service name="Catalina">
- <span style="color:#ff0000;"> <Connector port="</span><span style="color:#ff0000;background-color: rgb(255, 153, 0);">11080</span><span style="color:#ff0000;">" protocol="HTTP/1.1"
- connectionTimeout="20000"
- redirectPort="<span style="background-color: rgb(255, 153, 0);">11443</span>" />
- <Connector port="<span style="background-color: rgb(255, 153, 0);">11443</span>" protocol="HTTP/1.1" SSLEnabled="true" SSLCertificateFile="E:/tomcat.crt" SSLCertificateKeyFile="E:/tomcat.key"
- maxThreads="150" scheme="https" secure="true"
- clientAuth="false" keystoreFile="E:/tomcat.keystore" keystorePass="123456" sslProtocol="TLS" />
- <Connector port="<span style="background-color: rgb(255, 153, 0);">11009</span>" protocol="AJP/1.3" redirectPort="11443" /></span>
-
- <Engine name="Catalina" jvmRoute="<span style="color:#ff0000;background-color: rgb(255, 153, 0);">tomcat2</span>" defaultHost="localhost">
- <Realm className="org.apache.catalina.realm.LockOutRealm">
- <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
- resourceName="UserDatabase"/>
- </Realm>
-
- <Host name="localhost" appBase="webapps"
- unpackWARs="true" autoDeploy="true">
- <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
- prefix="localhost_access_log." suffix=".txt"
- pattern="%h %l %u %t "%r" %s %b" />
- </Host>
- </Engine>
- </Service>
- </Server>
三、Memcached配置
- 将Memcached的安装包解压到目录下,文件夹重命名为X:/memcached
- 以管理员身份进入X:/memcached,执行命令memcached.exe –d install,现在memcached已经作为系统服务可以随机启动,默认端口:11211
- 下面介绍几个常用的Memcached命令:
查看memcached的服务是否已经启动:tasklist /fi "imagename eq memcached.exe"
memcached参数使用介绍
-p 监听的端口
-l 连接的IP地址, 默认是本机
-d start 启动memcached服务
-d restart 重起memcached服务
-d stop|shutdown 关闭正在运行的memcached服务
-d install 安装memcached服务
-d uninstall 卸载memcached服务
-u 以的身份运行 (仅在以root运行的时候有效)
-m 最大内存使用,单位MB。默认64MB
-M 内存耗尽时返回错误,而不是删除项
-c 最大同时连接数,默认是1024
-f 块大小增长因子,默认是1.25
-n 最小分配空间,key+value+flags默认是48
-h 显示帮助
4.Memcached的Tomcat配置【
处理Session同步的问题】
首先,将之前下载的JAR包全部拷贝到两个Tomcat的lib下。
接着,配置两个Tomcat的context.xml的文件:在Context标签内加入如下内容
- <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
- memcachedNodes="n1:localhost:11211"
- requestUriIgnorePattern=".*/.(png|gif|jpg|css|js)$"
- sessionBackupAsync="false"
- sessionBackupTimeout="100"
- transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
- copyCollectionsForSerialization="false"/>
四、Nginx的配置
1.将下载的Nginx压缩包解压到X:/Nginx目录下
2.启动Nginx,以管理员身份进入X:/Nginx目录,执行命令:start nginx.exe,屏幕会一闪而过
3.接着输入命令:tasklist /fi "imagename eq nginx.exe",出现下面的内容就算是启动成功:
Nginx常用命令:
停止Nginx:nginx.exe -s quit
重启Nginx:nginx.exe -s reload
查看Nginx版本:nginx.exe -v
4.修改conf/nginx.conf文件:
在http{}中,添加如下内容:
- #设定HTTP负载均衡的服务器列表
- upstream <span style="color:#3333ff;">tomcat_server</span>{
- server localhost:11080 weight=1;#第一台机器
- server localhost:8080 weight=1;#第二台机器
- }
- server {
- listen 80;
- server_name localhost;
-
-
- #charset koi8-r;
-
-
- #access_log logs/host.access.log main;
-
-
- location / {
- root html;
- index index.html index.htm;
- <span style="white-space:pre"> </span> proxy_pass http://<span style="color:#3333ff;">tomcat_server</span>;
- }
- #设定HTTPS负载均衡的服务器列表
- upstream tomcat_ssl_server{
- server localhost:11443 weight=1;#第一台机器
- server localhost:8443 weight=1;#第二台机器
- }
- server {
- listen 443 ssl;
- server_name localhost;
-
-
- ssl_certificate E:\\tomcat.crt;
- ssl_certificate_key E:\\tomcat.key;
-
-
- <span style="white-space:pre"> </span>#The cache and other modules which require shared memory support do not work
- <span style="white-space:pre"> </span>#on Windows Vista and later versions due to address space layout randomization being enabled in these Windows versions.
- #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;
- <span style="white-space:pre"> </span>proxy_pass https://tomcat_ssl_server;
- }
- }
proxy_pass表示代理主机,upstream tomcat_server{……}配置中配置了代理主机处理请求的两个服务。
上述配置表示:Nginx监听到http://locahost:80 https://localhost:443
的请求之后,转发到代理tomcat_server中的两个服务中的一个,有它们来处理请求。weight表示被分配到权重,weight值越大,处理请求的机会越多。
五、测试
在这里我们主要测试的是,Tomcat的Session同步问题,编写JSP页面,获取到Session的ID
- <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
- <html>
- <body>
-
- SessionID:<%=session.getId()%>
- <BR>
- SessionIP:<%=request.getServerName()%>
- <BR>
- SessionPort:<%=request.getServerPort()%>
- <%
- out.println("apache-tomcat-7.0.55-02");
- %>
-
- <%=(String)session.getAttribute("user")%>
- </body>
- </html>
HTTP请求:
HTTPS请求: