Nginx+Memcached+Tomcat集群配置实践(Sticky Session)
- 页面login.jsp
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>登录页面</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- </head>
- <body>
- <table bgcolor="#F8C3C3" border="1" borderColor="#FF0000"
- align="center" width="260" height="160">
- <tr>
- <td height="15" colspan="2" align="center"
- style="background-color: #000000; font-size: 28px; color: #FFFF00"><b>用户登录</b></td>
- </tr>
- <form method="post" action="login">
- <tr>
- <td width="80" align="center"><b>用户名</b></td>
- <td><input type="text" name="userName" value="" /></td>
- </tr>
- <tr>
- <td align="center"><b>密 码</b></td>
- <td><input type="password" name="password" value="" /></td>
- </tr>
- <tr>
- <td colspan="2" align="center" style="background-color: #000000;">
- <input type="submit" value="登 录" /> <input type="reset"
- value="重 置" />
- </td>
- </tr>
- </form>
- </table>
- </body>
- </html>
- 登录成功页面success.jsp
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>登录成功页面</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- </head>
- <%
- String userName = (String) request.getSession().getAttribute("userName");
- %>
- <body bgcolor="#000000">
- <table width="500" height="200" align="center" border="1"
- style="background-color: #000000;">
- <tr>
- <td align="center"
- style="background-color: #F8C3C3; color: #00FF00; font-weight: bold; font-size: 30px">
- 当前登录用户:<%=userName %>
- </td>
- </tr>
- </table>
- </body>
- </html>
- package org.shirdrn.cluster.tomcat.servlet;
- import java.io.IOException;
- import java.util.HashMap;
- import java.util.Map;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- public class LoginSevlet extends HttpServlet {
- private static final long serialVersionUID = 1L;
- private static final Log LOG = LogFactory.getLog(LoginSevlet.class);
- private static Map<String, String> accountDB = new HashMap<String, String>();
- static {
- accountDB.put("shirdrn", "123456");
- accountDB.put("admin", "999999");
- accountDB.put("dev", "000888");
- accountDB.put("007", "007007");
- }
- public LoginSevlet() {
- super();
- }
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- this.doPost(request, response);
- }
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- String userName = request.getParameter("userName");
- String password = request.getParameter("password");
- LOG.info("Raw input:userName=" + userName + ",password=" + password);
- if(userName!=null && password!=null
- && accountDB.containsKey(userName)
- && accountDB.get(userName).equals(password)) {
- LOG.info("Login;status=SUCCESS");
- request.getSession().setAttribute("userName", userName);
- request.getRequestDispatcher("success.jsp").forward(request, response);
- } else {
- LOG.info("Login;status=FAIL");
- request.getRequestDispatcher("login.jsp").forward(request, response);
- }
- }
- }
- sudo apt-get install memcached
- sudo /usr/bin/memcached -m 64 -d -u memcache -p 11211 -l 192.168.1.104 -c 5000 -P /tmp/n1-memcached.pid
- sudo /usr/bin/memcached -m 64 -d -u memcache -p 11311 -l 192.168.1.104 -c 5000 -P /tmp/n2-memcached.pid
- shirdrn@dev:~/servers/cluster/nginx_tomcat_memcached$ ps -ef | grep memcached
- memcache 1578 1 0 Jan25 ? 00:00:00 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1
- memcache 3300 1 0 Jan25 ? 00:00:00 /usr/bin/memcached -m 64 -d -u memcache -p 11211 -l 192.168.1.104 -c 5000 -P /tmp/n1-memcached.pid
- memcache 3308 1 0 00:00 ? 00:00:00 /usr/bin/memcached -m 64 -d -u memcache -p 11311 -l 192.168.1.104 -c 5000 -P /tmp/n2-memcached.pid
- shirdrn 3318 3095 0 00:00 pts/5 00:00:00 grep --color=auto memcached
Tomcat集群配置
集群配置,集群中各个结点通过共享存储在缓存Memcached中session来实现session的共享:如果有一台机器上的Tomcat服务停掉
了,对于其他对等服务器上的session数据仍然可以从Memcached缓存中读取,从而不会发生session丢失的问题。
- https://memcached-session-manager.googlecode.com/files/memcached-session-manager-1.6.3.jar
- https://memcached-session-manager.googlecode.com/files/memcached-session-manager-tc7-1.6.3.jar
- https://memcached-session-manager.googlecode.com/files/msm-javolution-serializer-1.6.2.jar
- http://memcached-session-manager.googlecode.com/svn/maven/javolution/javolution/5.4.3.1/javolution-5.4.3.1.jar
- https://spymemcached.googlecode.com/files/spymemcached-2.8.4.jar
- 如果在同一台机器上,要保证各个服务端口不相冲突
- <Manager>元素中failoverNodes属性值不同
- tomcat-1/conf/server.xml
- <?xml version='1.0' encoding='utf-8'?>
- <Server port="8085" shutdown="SHUTDOWN">
- <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">
- <Connector port="8088" protocol="HTTP/1.1" connectionTimeout="20000"
- redirectPort="8443" />
- <Connector port="8089" protocol="AJP/1.3" redirectPort="8443" />
- <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
- <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" deployOnStartup="true">
- <Context docBase="/home/shirdrn/servers/cluster/nginx_tomcat_memcached/webapps/session.war" path="/session" reloadable="true">
- <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
- memcachedNodes="n1:192.168.1.104:11211,n2:192.168.1.104:11311"
- failoverNodes="n1"
- requestUriIgnorePattern=".*\.(png|gif|jpg|css|js)$"
- sessionBackupAsync="false"
- sessionBackupTimeout="100"
- transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
- copyCollectionsForSerialization="false" />
- </Context>
- <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>
- tomcat-2/conf/server.xml
- <?xml version='1.0' encoding='utf-8'?>
- <Server port="9085" shutdown="SHUTDOWN">
- <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">
- <Connector port="9088" protocol="HTTP/1.1" connectionTimeout="20000"
- redirectPort="9443" />
- <Connector port="9089" protocol="AJP/1.3" redirectPort="9443" />
- <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
- <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" deployOnStartup="true">
- <Context docBase="/home/shirdrn/servers/cluster/nginx_tomcat_memcached/webapps/session.war" path="/session" reloadable="true">
- <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
- memcachedNodes="n1:192.168.1.104:11211,n2:192.168.1.104:11311"
- failoverNodes="n2"
- requestUriIgnorePattern=".*\.(png|gif|jpg|css|js)$"
- sessionBackupAsync="false"
- sessionBackupTimeout="100"
- transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
- copyCollectionsForSerialization="false" />
- </Context>
- <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>
类似,failover的Memcached结点是n2,也就是说,如果tomcat-2和n2在同一台机器上的话,Session数据会优先复制到存储
到Memcached结点n1,这样即使n2所在的结点宕机了,n1所在结点仍然存储了之前的Session数据。
配置实现了Tomcat集群中,通过Memcached实现了Sticky
Session(粘性Session),主要是通过配置failover结点来达到目的的。如果采用Non-sticky
Session方式,就不需要考虑failover的问题,详细配置扩参考文档(https://code.google.com/p/memcached-session-manager/wiki/SetupAndConfiguration),下面截取一个片段:
The following example shows a configuration for non-sticky sessions. In this case there's no need for failoverNodes, <Context> |
- shirdrn@dev:~/servers/cluster/nginx_tomcat_memcached$ tomcat-1/bin/catalina.sh start
- Using CATALINA_BASE: /home/shirdrn/servers/cluster/nginx_tomcat_memcached/tomcat-1
- Using CATALINA_HOME: /home/shirdrn/servers/cluster/nginx_tomcat_memcached/tomcat-1
- Using CATALINA_TMPDIR: /home/shirdrn/servers/cluster/nginx_tomcat_memcached/tomcat-1/temp
- Using JRE_HOME: /home/hadoop/installation/jdk1.6.0_30
- Using CLASSPATH: /home/shirdrn/servers/cluster/nginx_tomcat_memcached/tomcat-1/bin/bootstrap.jar:/home/shirdrn/servers/cluster/nginx_tomcat_memcached/tomcat-1/bin/tomcat-juli.jar
- shirdrn@dev:~/servers/cluster/nginx_tomcat_memcached$ tomcat-2/bin/catalina.sh start
- Using CATALINA_BASE: /home/shirdrn/servers/cluster/nginx_tomcat_memcached/tomcat-2
- Using CATALINA_HOME: /home/shirdrn/servers/cluster/nginx_tomcat_memcached/tomcat-2
- Using CATALINA_TMPDIR: /home/shirdrn/servers/cluster/nginx_tomcat_memcached/tomcat-2/temp
- Using JRE_HOME: /home/hadoop/installation/jdk1.6.0_30
- Using CLASSPATH: /home/shirdrn/servers/cluster/nginx_tomcat_memcached/tomcat-2/bin/bootstrap.jar:/home/shirdrn/servers/cluster/nginx_tomcat_memcached/tomcat-2/bin/tomcat-juli.jar
- tomcat-1的日志
- shirdrn@dev:~/servers/cluster/nginx_tomcat_memcached$ tail -100f tomcat-1/logs/catalina.out
- Jan 26, 2013 12:03:25 AM org.apache.catalina.core.AprLifecycleListener init
- INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /home/hadoop/installation/jdk1.6.0_30/jre/lib/i386/client:/home/hadoop/installation/jdk1.6.0_30/jre/lib/i386:/home/hadoop/installation/jdk1.6.0_30/jre/../lib/i386:/usr/java/packages/lib/i386:/lib:/usr/lib
- Jan 26, 2013 12:03:26 AM org.apache.coyote.AbstractProtocol init
- INFO: Initializing ProtocolHandler ["http-bio-8088"]
- Jan 26, 2013 12:03:26 AM org.apache.coyote.AbstractProtocol init
- INFO: Initializing ProtocolHandler ["ajp-bio-8089"]
- Jan 26, 2013 12:03:26 AM org.apache.catalina.startup.Catalina load
- INFO: Initialization processed in 1743 ms
- Jan 26, 2013 12:03:26 AM org.apache.catalina.core.StandardService startInternal
- INFO: Starting service Catalina
- Jan 26, 2013 12:03:26 AM org.apache.catalina.core.StandardEngine startInternal
- INFO: Starting Servlet Engine: Apache Tomcat/7.0.26
- Jan 26, 2013 12:03:27 AM de.javakaffee.web.msm.MemcachedSessionService startInternal
- INFO: MemcachedSessionService starts initialization... (configured nodes definition n1:192.168.1.104:11211,n2:192.168.1.104:11311, failover nodes n1)
- 2013-01-26 00:03:27.441 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=/192.168.1.104:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
- 2013-01-26 00:03:27.442 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=/192.168.1.104:11311, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
- Jan 26, 2013 12:03:27 AM de.javakaffee.web.msm.RequestTrackingHostValve <init>
- INFO: Setting ignorePattern to .*\.(png|gif|jpg|css|js)$
- 2013-01-26 00:03:27.459 INFO net.spy.memcached.MemcachedConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@5b0668
- 2013-01-26 00:03:27.460 INFO net.spy.memcached.MemcachedConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@1d5a0
- Jan 26, 2013 12:03:27 AM de.javakaffee.web.msm.MemcachedSessionService setLockingMode
- INFO: Setting lockingMode to null
- Jan 26, 2013 12:03:27 AM de.javakaffee.web.msm.MemcachedSessionService createTranscoderFactory
- INFO: Creating transcoder factory de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory
- Jan 26, 2013 12:03:27 AM de.javakaffee.web.msm.MemcachedSessionService startInternal
- INFO: MemcachedSessionService finished initialization, sticky true, operation timeout 1000, with node ids [n2] and failover node ids [n1]
- Jan 26, 2013 12:03:27 AM org.apache.catalina.startup.HostConfig deployDirectory
- INFO: Deploying web application directory /home/shirdrn/servers/cluster/nginx_tomcat_memcached/tomcat-1/webapps/host-manager
- Jan 26, 2013 12:03:27 AM org.apache.catalina.startup.HostConfig deployDirectory
- INFO: Deploying web application directory /home/shirdrn/servers/cluster/nginx_tomcat_memcached/tomcat-1/webapps/manager
- Jan 26, 2013 12:03:27 AM org.apache.catalina.startup.HostConfig deployDirectory
- INFO: Deploying web application directory /home/shirdrn/servers/cluster/nginx_tomcat_memcached/tomcat-1/webapps/docs
- Jan 26, 2013 12:03:27 AM org.apache.catalina.startup.HostConfig deployDirectory
- INFO: Deploying web application directory /home/shirdrn/servers/cluster/nginx_tomcat_memcached/tomcat-1/webapps/examples
- Jan 26, 2013 12:03:28 AM org.apache.catalina.startup.HostConfig deployDirectory
- INFO: Deploying web application directory /home/shirdrn/servers/cluster/nginx_tomcat_memcached/tomcat-1/webapps/ROOT
- Jan 26, 2013 12:03:28 AM org.apache.coyote.AbstractProtocol start
- INFO: Starting ProtocolHandler ["http-bio-8088"]
- Jan 26, 2013 12:03:28 AM org.apache.coyote.AbstractProtocol start
- INFO: Starting ProtocolHandler ["ajp-bio-8089"]
- Jan 26, 2013 12:03:28 AM org.apache.catalina.startup.Catalina start
- INFO: Server startup in 1487 ms
- tomcat-2的日志
- shirdrn@dev:~/servers/cluster/nginx_tomcat_memcached$ tail -100f tomcat-2/logs/catalina.out
- Jan 26, 2013 12:04:34 AM org.apache.catalina.core.AprLifecycleListener init
- INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /home/hadoop/installation/jdk1.6.0_30/jre/lib/i386/client:/home/hadoop/installation/jdk1.6.0_30/jre/lib/i386:/home/hadoop/installation/jdk1.6.0_30/jre/../lib/i386:/usr/java/packages/lib/i386:/lib:/usr/lib
- Jan 26, 2013 12:04:34 AM org.apache.coyote.AbstractProtocol init
- INFO: Initializing ProtocolHandler ["http-bio-9088"]
- Jan 26, 2013 12:04:34 AM org.apache.coyote.AbstractProtocol init
- INFO: Initializing ProtocolHandler ["ajp-bio-9089"]
- Jan 26, 2013 12:04:34 AM org.apache.catalina.startup.Catalina load
- INFO: Initialization processed in 742 ms
- Jan 26, 2013 12:04:34 AM org.apache.catalina.core.StandardService startInternal
- INFO: Starting service Catalina
- Jan 26, 2013 12:04:34 AM org.apache.catalina.core.StandardEngine startInternal
- INFO: Starting Servlet Engine: Apache Tomcat/7.0.26
- Jan 26, 2013 12:05:07 AM org.apache.catalina.util.SessionIdGenerator createSecureRandom
- INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [33,094] milliseconds.
- Jan 26, 2013 12:05:07 AM de.javakaffee.web.msm.MemcachedSessionService startInternal
- INFO: MemcachedSessionService starts initialization... (configured nodes definition n1:192.168.1.104:11211,n2:192.168.1.104:11311, failover nodes n2)
- 2013-01-26 00:05:07.873 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=/192.168.1.104:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
- 2013-01-26 00:05:07.876 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=/192.168.1.104:11311, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
- Jan 26, 2013 12:05:07 AM de.javakaffee.web.msm.RequestTrackingHostValve <init>
- INFO: Setting ignorePattern to .*\.(png|gif|jpg|css|js)$
- 2013-01-26 00:05:07.896 INFO net.spy.memcached.MemcachedConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@701a27
- 2013-01-26 00:05:07.897 INFO net.spy.memcached.MemcachedConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@1d5a0
- Jan 26, 2013 12:05:07 AM de.javakaffee.web.msm.MemcachedSessionService setLockingMode
- INFO: Setting lockingMode to null
- Jan 26, 2013 12:05:07 AM de.javakaffee.web.msm.MemcachedSessionService createTranscoderFactory
- INFO: Creating transcoder factory de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory
- Jan 26, 2013 12:05:07 AM de.javakaffee.web.msm.MemcachedSessionService startInternal
- INFO: MemcachedSessionService finished initialization, sticky true, operation timeout 1000, with node ids [n1] and failover node ids [n2]
- Jan 26, 2013 12:05:08 AM org.apache.catalina.startup.HostConfig deployDirectory
- INFO: Deploying web application directory /home/shirdrn/servers/cluster/nginx_tomcat_memcached/tomcat-2/webapps/host-manager
- Jan 26, 2013 12:05:08 AM org.apache.catalina.startup.HostConfig deployDirectory
- INFO: Deploying web application directory /home/shirdrn/servers/cluster/nginx_tomcat_memcached/tomcat-2/webapps/manager
- Jan 26, 2013 12:05:08 AM org.apache.catalina.startup.HostConfig deployDirectory
- INFO: Deploying web application directory /home/shirdrn/servers/cluster/nginx_tomcat_memcached/tomcat-2/webapps/docs
- Jan 26, 2013 12:05:08 AM org.apache.catalina.startup.HostConfig deployDirectory
- INFO: Deploying web application directory /home/shirdrn/servers/cluster/nginx_tomcat_memcached/tomcat-2/webapps/examples
- Jan 26, 2013 12:05:08 AM org.apache.catalina.startup.HostConfig deployDirectory
- INFO: Deploying web application directory /home/shirdrn/servers/cluster/nginx_tomcat_memcached/tomcat-2/webapps/ROOT
- Jan 26, 2013 12:05:08 AM org.apache.coyote.AbstractProtocol start
- INFO: Starting ProtocolHandler ["http-bio-9088"]
- Jan 26, 2013 12:05:08 AM org.apache.coyote.AbstractProtocol start
- INFO: Starting ProtocolHandler ["ajp-bio-9089"]
- Jan 26, 2013 12:05:08 AM org.apache.catalina.startup.Catalina start
- INFO: Server startup in 34163 ms
- sudo apt-get install nginx
- # You may add here your
- # server {
- # ...
- # }
- # statements for each of your virtual hosts to this file
- ##
- # You should look at the following URL's in order to grasp a solid understanding
- # of Nginx configuration files in order to fully unleash the power of Nginx.
- # http://wiki.nginx.org/Pitfalls
- # http://wiki.nginx.org/QuickStart
- # http://wiki.nginx.org/Configuration
- #
- # Generally, you will want to move this file somewhere, and start with a clean
- # file but keep this around for reference. Or just disable in sites-enabled.
- #
- # Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
- ##
- upstream dev.shirdrn.org {
- server 192.168.1.104:8088 weight=1;
- server 192.168.1.104:9088 weight=1;
- }
- server {
- #listen 80; ## listen for ipv4; this line is default and implied
- #listen [::]:80 default ipv6only=on; ## listen for ipv6
- root /usr/share/nginx/www/session; # session是我们配置的虚拟目录,实际直接指向Tomcat下名为session的web应用
- index index.html index.htm;
- # Make site accessible from http://localhost/
- ####### server_name localhost;
- server_name dev.shirdrn.org; # Nginx服务所在主机
- charset utf-8;
- location / {
- proxy_pass http://dev.shirdrn.org; #直接代理tomcat集群
- proxy_set_header X-Real-IP $remote_addr;
- client_max_body_size 100m;
- # First attempt to serve request as file, then
- # as directory, then fall back to index.html
- ####### try_files $uri $uri/ /index.html;
- # Uncomment to enable naxsi on this location
- # include /etc/nginx/naxsi.rules
- }
- location ~ ^/(WEB-INF)/ { # 禁止访问Tomcat下web应用的WEB-INF目录下的资源
- deny all;
- }
- location /doc/ {
- alias /usr/share/doc/;
- autoindex on;
- allow 127.0.0.1;
- deny all;
- }
- # Only for nginx-naxsi : process denied requests
- #location /RequestDenied {
- # For example, return an error code
- #return 418;
- #}
- #error_page 404 /404.html;
- # redirect server error pages to the static page /50x.html
- #
- #error_page 500 502 503 504 /50x.html;
- #location = /50x.html {
- # root /usr/share/nginx/www;
- #}
- # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
- #
- #location ~ \.php$ {
- # fastcgi_split_path_info ^(.+\.php)(/.+)$;
- # # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
- #
- # # With php5-cgi alone:
- # fastcgi_pass 127.0.0.1:9000;
- # # With php5-fpm:
- # fastcgi_pass unix:/var/run/php5-fpm.sock;
- # fastcgi_index index.php;
- # include fastcgi_params;
- #}
- # deny access to .htaccess files, if Apache's document root
- # concurs with nginx's one
- #
- #location ~ /\.ht {
- # deny all;
- #}
- }
- # another virtual host using mix of IP-, name-, and port-based configuration
- #
- #server {
- # listen 8000;
- # listen somename:8080;
- # server_name somename alias another.alias;
- # root html;
- # index index.html index.htm;
- #
- # location / {
- # try_files $uri $uri/ /index.html;
- # }
- #}
- # HTTPS server
- #
- #server {
- # listen 443;
- # server_name localhost;
- #
- # root html;
- # index index.html index.htm;
- #
- # ssl on;
- # ssl_certificate cert.pem;
- # ssl_certificate_key cert.key;
- #
- # ssl_session_timeout 5m;
- #
- # ssl_protocols SSLv3 TLSv1;
- # ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;
- # ssl_prefer_server_ciphers on;
- #
- # location / {
- # try_files $uri $uri/ /index.html;
- # }
- #}
- shirdrn@dev:~/servers/cluster/nginx_tomcat_memcached$ sudo mkdir /usr/share/nginx/www/session
- shirdrn@dev:~$ sudo /etc/init.d/nginx start
- shirdrn@dev:~/servers/cluster/nginx_tomcat_memcached$ sudo ps -ef | grep nginx
- root 3199 1 0 Jan25 ? 00:00:00 nginx: master process /usr/sbin/nginx
- www-data 3200 3199 0 Jan25 ? 00:00:00 nginx: worker process
- www-data 3201 3199 0 Jan25 ? 00:00:00 nginx: worker process
- www-data 3202 3199 0 Jan25 ? 00:00:00 nginx: worker process
- www-data 3203 3199 0 Jan25 ? 00:00:00 nginx: worker process
- shirdrn 3327 3095 0 00:01 pts/5 00:00:00 grep --color=auto nginx
- telnet 192.168.1.104 11211
- shirdrn@dev:~$ telnet 192.168.1.104 11211
- Trying 192.168.1.104...
- Connected to 192.168.1.104.
- Escape character is '^]'.
- ERROR
- stats
- STAT pid 3300
- STAT uptime 2148
- STAT time 1359131741
- STAT version 1.4.13
- STAT libevent 2.0.16-stable
- STAT pointer_size 32
- STAT rusage_user 0.052003
- STAT rusage_system 0.084005
- STAT curr_connections 7
- STAT total_connections 9
- STAT connection_structures 8
- STAT reserved_fds 20
- STAT cmd_get 23
- STAT cmd_set 6
- STAT cmd_flush 0
- STAT cmd_touch 0
- STAT get_hits 2
- STAT get_misses 21
- STAT delete_misses 0
- STAT delete_hits 0
- STAT incr_misses 0
- STAT incr_hits 0
- STAT decr_misses 0
- STAT decr_hits 0
- STAT cas_misses 0
- STAT cas_hits 0
- STAT cas_badval 0
- STAT touch_hits 0
- STAT touch_misses 0
- STAT auth_cmds 0
- STAT auth_errors 0
- STAT bytes_read 2675
- STAT bytes_written 2835
- STAT limit_maxbytes 67108864
- STAT accepting_conns 1
- STAT listen_disabled_num 0
- STAT threads 4
- STAT conn_yields 0
- STAT hash_power_level 16
- STAT hash_bytes 262144
- STAT hash_is_expanding 0
- STAT expired_unfetched 0
- STAT evicted_unfetched 0
- STAT bytes 858
- STAT curr_items 2
- STAT total_items 6
- STAT evictions 0
- STAT reclaimed 0
- END
- telnet 192.168.1.104 11311
- shirdrn@dev:~$ telnet 192.168.1.104 11311
- Trying 192.168.1.104...
- Connected to 192.168.1.104.
- Escape character is '^]'.
- stats
- STAT pid 3308
- STAT uptime 2398
- STAT time 1359132000
- STAT version 1.4.13
- STAT libevent 2.0.16-stable
- STAT pointer_size 32
- STAT rusage_user 0.004000
- STAT rusage_system 0.128008
- STAT curr_connections 7
- STAT total_connections 9
- STAT connection_structures 8
- STAT reserved_fds 20
- STAT cmd_get 1
- STAT cmd_set 2
- STAT cmd_flush 0
- STAT cmd_touch 0
- STAT get_hits 0
- STAT get_misses 1
- STAT delete_misses 1
- STAT delete_hits 0
- STAT incr_misses 0
- STAT incr_hits 0
- STAT decr_misses 0
- STAT decr_hits 0
- STAT cas_misses 0
- STAT cas_hits 0
- STAT cas_badval 0
- STAT touch_hits 0
- STAT touch_misses 0
- STAT auth_cmds 0
- STAT auth_errors 0
- STAT bytes_read 644
- STAT bytes_written 2109
- STAT limit_maxbytes 67108864
- STAT accepting_conns 1
- STAT listen_disabled_num 0
- STAT threads 4
- STAT conn_yields 0
- STAT hash_power_level 16
- STAT hash_bytes 262144
- STAT hash_is_expanding 0
- STAT expired_unfetched 1
- STAT evicted_unfetched 0
- STAT bytes 0
- STAT curr_items 0
- STAT total_items 2
- STAT evictions 0
- STAT reclaimed 1
- END
Nginx+Memcached+Tomcat集群配置实践(Sticky Session)的更多相关文章
- Nginx+Memcached+Tomcat集群配置(MSM--win7 64bit)
本次主要是在win7 64 上演示操作. web应用构建 Memcached安装配置启动 Tomcat配置 所需jar包 memcached-session-manager 序列化 contextxm ...
- 配置集群Nginx+Memcached+Tomcat集群配置
上班之余抽点时间出来写写博文,希望对新接触的朋友有帮助.今天在这里和大家一起学习一下配置集群 1. Nginx Nginx是通过将多个Web Server绑定到同一个IP地址下,以实现多个WebS ...
- Nginx+Memcached+Tomcat集群配置
1. Nginx Nginx是通过将多个Web Server绑定到同一个IP地址下,以实现多个WebServer间的负载均衡,降低单个Web Server的负荷,以提高整体的性能与稳定性. 安装和 ...
- nginx 、tomcat 集群配置、shiro Session 共享
一.nginx.config 配置 #user nobody; worker_processes ; #error_log logs/error.log; #error_log logs/error. ...
- 使用nginx搭建tomcat集群配置
软件准备: (1)jdk-8u73-linux-x64.tar.gz (2)apache-tomcat-7.0.57.tar.gz (3)nginx-1.7.7.tar.gz 准备3台Linux机器, ...
- nginx+tomcat集群配置(4)--rewrite规则和多应用根目录设定思路
前言: nginx中有一块很重要的概念, 就是rewrite规则. 它会对URL进行修改, 然后进行内部的重定向. rewrite授予了nginx更多的自由, 使得后级服务的接入更加地方便. 本文将简 ...
- nginx+tomcat集群配置(1)---根目录设定和多后端分发配置
前言: 对于javaer而言, nginx+tomcat集群配置, 已然成了web应用部署的主流. 大公司如此, 小公司亦然. 对于个人开发者而言, 资源有限, 往往多个web应用混部于一台服务器(云 ...
- Nginx+Tomcat集群配置
Nginx+Tomcat集群配置 一台虚拟机作为Nginx服务 两太虚拟机配置Tomcat+jdk环境 Nginx测试 启动: cd usr/local/nginx/sbin ./nginx ---& ...
- nginx整合tomcat集群并做session共享----测试案例
最近出于好奇心,研究了一下tomcat集群配置,并整合nginx,实现负载均衡,session共享,写篇记录,防止遗忘.---------菜鸡的自我修炼. 说明:博主采用一个web项目同时部署到两台t ...
随机推荐
- 标准C++中的String类的使用
要使用标准C++中的String类,必须包含#include<string>(注意不是<string.h>带.h的是C语言中的头文件) 作者:耑新新,发布于 博客园 转载请注 ...
- snmp常见操作
常用snmp OID说明下面这些值可以手动连接进行获取数据: 用zabbix监控交换机和路由器需要一款能够获取网络设备OID的工具,可用getif来获得OID 也可以使用snmpwalk 配置交换机的 ...
- spring boot之org.springframework.boot.context.TypeExcludeFilter
曾经碰到过这样一种情况,想让某个使用了spring 注解的类不被spring扫描注入到spring bean池中,比如下面的类使用了@Component和@ConfigurationPropertie ...
- 【LOJ】#2059. 「TJOI / HEOI2016」字符串
题解 我们冷静一下,先画一棵后缀树 然后发现我们要给c和d这一段区间在[a,b]这一段开头的串里找lcp 而lcp呢,就是c点的祖先的到根的一段,假如这个祖先的子树里有[a,b - dis[u] + ...
- 分享实用的JavaScript代码库
1 var keyCodeMap = { 2 8: 'Backspace', 3 9: 'Tab', 4 13: 'Enter', 5 16: 'Shift', 6 17: 'Ctrl', 7 18: ...
- 关于linux时间
时区确定cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 查看硬件时间hwclock --show 将系统时间写入硬件时间hwclock --sy ...
- 匹配合法IP地址的正则表达式
IPV4地址分为4段,以点号分隔.如192.168.26.13.要对IP地址进行匹配,首先要对其进行分析,分成如下部分,分别进行匹配: 第一步:地址分析,正则初判 0-9 \d 进行匹配 1 ...
- Spring单例 和 Scope注解
关键字 @Scope @Qualifier Singleton 单例 Spring是单例模式.结合Springboot的例子. Controller @Autowired private Tes ...
- JavaQuery
1.初识jQuery <!DOCTYPE html> <html> <head lang="en"> <meta charse ...
- python语法(一)
Python是一种面向对象.直译式电脑编程语言,也是一种功能强大的通用型语言,已经具有近二十年的发展历史,成熟且稳定.在近几年,大数据,人工智能火起来之后也是水涨船高,被越来越多的人知道,并且越来越多 ...