这几天研究了一下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包:
<?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>
<?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>
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:localhost:11211"<!-- Memcached只有一个节点,现在这么写就OK --> requestUriIgnorePattern=".*/.(png|gif|jpg|css|js)$" sessionBackupAsync="false" sessionBackupTimeout="100" transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory" copyCollectionsForSerialization="false"/>
#设定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; } }
<%@ 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请求:
Nginx1.7.4+Tomcat7+memcached集群环境搭建
原文地址:http://blog.csdn.net/xq328220454/article/details/39257105