Nginx+Tomcat+Memcached共享session集群配置

标签: nginx tomcat memcached | 发表时间:2011-08-03 13:15 | 作者:(author unknown) ENOCH
出处:http://www.iteye.com
1、采用Nginx负载均衡
2、memcached共享session
3、tomcat集群配置(3台CentOS 6)

(172.18.188.64): 操作系统CentOS 6; 安装nginx、memcached和tomcat 6
(172.18.188.76): 操作系统CentOS 6; 安装tomcat 6
(172.18.188.78): 操作系统CentOS 6; 安装tomcat 6

nginx、memcached、tomcat 6安装省略.

nginx配置如下:

nginx.conf如下:
#运行nginx所在的用户名和用户组
#user  root root;

#启动进程数
worker_processes 8;
#全局错误日志及PID文件
error_log /usr/local/webserver/nginx/logs/nginx_error.log crit;

pid /usr/local/webserver/nginx/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.

worker_rlimit_nofile 65535;
#工作模式及连接数上限
events
{
  use epoll;
  worker_connections 65535;
}
#设定http服务器,利用它的反向代理功能提供负载均衡支持
http
{
  #设定mime类型
  include       mime.types;
  default_type  application/octet-stream;
  include /usr/local/webserver/nginx/conf/proxy.conf;
  #charset  gb2312;
  #设定请求缓冲  
  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  #client_max_body_size 8m;
    
  sendfile on;
  tcp_nopush     on;

  keepalive_timeout 60;

  tcp_nodelay on;

#  fastcgi_connect_timeout 300;
#  fastcgi_send_timeout 300;
#  fastcgi_read_timeout 300;
#  fastcgi_buffer_size 64k;
#  fastcgi_buffers 4 64k;
#  fastcgi_busy_buffers_size 128k;
#  fastcgi_temp_file_write_size 128k;

  gzip on;
  gzip_min_length  1k;
  gzip_buffers     4 16k;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_types       text/plain application/x-javascript text/css application/xml;
  gzip_vary on;

  #limit_zone  crawler  $binary_remote_addr  10m;
###禁止通过ip访问站点
#  server{
#       server_name _;
#       return 404;
#       }

upstream tserver {
    server 172.18.188.64:8080 weight=1;
    server 172.18.188.76:8080 weight=1;
    server 172.18.188.78:8080 weight=1;
}

  server
  {
    listen       80;
    server_name  vmwarehost;
    index index.html index.htm index.jsp;
    root  /home/www/web/ROOT;

    #limit_conn   crawler  20;  
   
    location /
    {
    proxy_pass http://tserver;
    } 
   
    location /NginxStatus
    {
      stub_status on;
      access_log off;
    }   
  
    location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ico)$
    {
      expires      30d;
    }

    location ~ .*\.(js|css)?$
    {
      expires      1h;
    }  

#定义访问日志的写入格式
     log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
              '$status $body_bytes_sent "$http_referer" '
              '"$http_user_agent" $http_x_forwarded_for';
              access_log  /usr/local/webserver/nginx/logs/localhost.log access;

      }

}

proxy.confi配置如下
#!nginx (-)
# proxy.conf
proxy_redirect          off;
proxy_set_header        Host $host;
proxy_set_header        X-Real-IP $remote_addr;
proxy_set_header       X-Forwarded-For   $proxy_add_x_forwarded_for;
client_max_body_size    10m;
client_body_buffer_size 128k;
proxy_connect_timeout   90;
proxy_send_timeout      90;
proxy_read_timeout      90;
proxy_buffer_size       4k;
proxy_buffers           4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;

tomcat 6 server.xml配置如下:
<?xml version='1.0' encoding='utf-8'?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- Note:  A "Server" is not itself a "Container", so you may not
     define subcomponents such as "Valves" at this level.
     Documentation at /docs/config/server.html
-->
<Server port="8005" shutdown="SHUTDOWN">

  <!--APR library loader. Documentation at /docs/apr.html -->
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
  <Listener className="org.apache.catalina.core.JasperListener" />
  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <!-- JMX Support for the Tomcat server. Documentation at /docs/non-existent.html -->
  <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />

  <!-- Global JNDI resources
       Documentation at /docs/jndi-resources-howto.html
  -->
  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <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>

  <!-- A "Service" is a collection of one or more "Connectors" that share
       a single "Container" Note:  A "Service" is not itself a "Container",
       so you may not define subcomponents such as "Valves" at this level.
       Documentation at /docs/config/service.html
   -->
  <Service name="Catalina">
 
    <!--The connectors can use a shared executor, you can define one or more named thread pools-->
    <!--
    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
        maxThreads="150" minSpareThreads="4"/>
    -->
   
   
    <!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL HTTP/1.1 Connector on port 8080
    -->
    <Connector port="8080" protocol="HTTP/1.1"
               maxHttpHeaderSize="8192" connectionTimeout="20000"
               disableUploadTimeout="true" enableLookups="false"
               redirectPort="8443" maxThreads="600"
               minSpareThreads="25" maxSpareThreads="75" acceptCount="100" />
    <!-- A "Connector" using the shared thread pool-->
    <!--
    <Connector executor="tomcatThreadPool"
               port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    -->          
    <!-- Define a SSL HTTP/1.1 Connector on port 8443
         This connector uses the JSSE configuration, when using APR, the
         connector should be using the OpenSSL style configuration
         described in the APR documentation -->
    <!--
    <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
               maxThreads="150" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />
    -->

    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8009" protocol="AJP/1.3"
    maxHttpHeaderSize="8192" connectionTimeout="20000"
                  disableUploadTimeout="true" maxThreads="600"
                  minSpareThreads="25" maxSpareThreads="75"
                  enableLookups="false" redirectPort="8443" />


    <!-- An Engine represents the entry point (within Catalina) that processes
         every request.  The Engine implementation for Tomcat stand alone
         analyzes the HTTP headers included with the request, and passes them
         on to the appropriate Host (virtual host).
         Documentation at /docs/config/engine.html -->

    <!-- You should set jvmRoute to support load-balancing via AJP ie :
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">        
    -->
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">

      <!--For clustering, please take a look at documentation at:
          /docs/cluster-howto.html  (simple how to)
          /docs/config/cluster.html (reference documentation) -->
     
         <!--
      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
      -->
        <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
                 channelSendOptions="6">
<!--
<Manager className="org.apache.catalina.ha.session.BackupManager"
                       expireSessionsOnShutdown="false"
                       notifyListenersOnReplication="true"
                       mapSendOptions="6"/>
           -->
           <!--           
          <Manager className="org.apache.catalina.ha.session.DeltaManager"
                   expireSessionsOnShutdown="false"
                   notifyListenersOnReplication="true"/>
-->
          <Channel className="org.apache.catalina.tribes.group.GroupChannel">
            <Membership className="org.apache.catalina.tribes.membership.McastService"
                        address="228.0.0.4"
                        port="45564"
                        frequency="500"
                        dropTime="3000"/>
            <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
                      address="172.18.188.64"
                      port="4001"
                      autoBind="100"
                      selectorTimeout="5000"
                      maxThreads="6"/>

            <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
              <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
            </Sender>        
            <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
            <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
            <Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>         
          </Channel>

          <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
          filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;" />

          <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
                        tempDir="/tmp/war-temp/"
                        deployDir="/tmp/war-deploy/"
                        watchDir="/tmp/war-listen/"
                        watchEnabled="false"/>
          <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
        </Cluster>
            

      <!-- The request dumper valve dumps useful debugging information about
           the request and response data received and sent by Tomcat.
           Documentation at: /docs/config/valve.html -->
      <!--
      <Valve className="org.apache.catalina.valves.RequestDumperValve"/>
      -->

      <!-- This Realm uses the UserDatabase configured in the global JNDI
           resources under the key "UserDatabase".  Any edits
           that are performed against this UserDatabase are immediately
           available for use by the Realm.  -->
      <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
             resourceName="UserDatabase"/>

      <!-- Define the default virtual host
           Note: XML Schema validation will not work with Xerces 2.2.
       -->
      <Host name="localhost"  appBase="/home/www/web"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" 
               prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/>
        -->
      </Host>
    </Engine>
  </Service>
</Server>

content.xml配置如下
<Context>

    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>

    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->

    <!-- Uncomment this to enable Comet connection tacking (provides events
         on session expiration as well as webapp lifecycle) -->
    <!--
    <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
    -->
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:172.18.188.64:11211"
requestUriIgnorePattern=".*\.(png|gif|jpg|css|js)$"
sessionBackupAsync="false"
sessionBackupTimeout="100"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
copyCollectionsForSerialization="false"/>
</Context>
memcached启动命令:
useradd -s /sbin/nologin memcached
memcached -d -m 2048 -l 172.18.188.64 -p 11211 -u memcached
ngingx启动命令
sbin/nginx
tomcat启动命令(startup.sh)
分别启动memcached nginx tomcat能实现session的简单共享.
按照上面配置Nginx Memcached Tomcat启动运行都没有问题,并且可以实现Session的共享.但是有2个问题
一、Session的共享是基于访问IP的,即在同一台电脑上开2个IE窗口时,获取到Session中内容是相同的,也就是sessionid除了最后面的jvmrout不一样,其他都一样,内容也一样,这样就造成如果2个用户先后在同一台电脑上登录形成session混乱,有没有可能配置成同一ip在不同的ie窗口中不共享session,一个ie窗口对应一个session,而不是一个ip共享一个session.后台的访问依然是由nginx根据weight做分发而不是固定到一台固定的tomcat机器?
二、Memcached 启动是用-m 512发现会有数据丢失而且丢失几率很大,在一个页面上连续不断的刷新时就会发现session中的内容会清空.

    本文附件下载:
  • 1.rar (4.5 KB)


作者: wtaoh_tm 
声明: 本文系ITeye网站发布的原创文章,未经作者书面许可,严禁任何网站转载本文,否则必将追究法律责任!

已有 1 人发表回复,猛击->>这里<<-参与讨论


ITeye推荐



相关 [nginx tomcat memcached] 推荐:

Nginx+Tomcat+Memcached共享session集群配置

- ENOCH - ITeye论坛最新讨论
2、memcached共享session. 3、tomcat集群配置(3台CentOS 6). (172.18.188.64): 操作系统CentOS 6; 安装nginx、memcached和tomcat 6. (172.18.188.76): 操作系统CentOS 6; 安装tomcat 6. (172.18.188.78): 操作系统CentOS 6; 安装tomcat 6.

电子商务网站基础架构 nginx + memcached + tomcat + squid 集群

- - 企业架构 - ITeye博客
本文中 包含 nginx memcached tomcat 集群 的安装和配置. wget url 为下载所需的资源文件方式,也可以通过 VMware Tools 工具载入,. 如何安装 VMwareTools 参考本博客或者其他资料. 后面最后一章节是安装配置squid 在项目第一期不作要求. 第一章 安装配置nginx.

nginx+tomcat+memcached (msm)实现 session同步复制

- - 企业架构 - ITeye博客
tomcat + memcached + nginx 实现session共享.     这里重点强调如何实现linux服务器上 服务器session共享,软件安装不再赘述. 首先我们需要对 cookie 和session的工作机制非常了解,如果不了解其中的原理,就算配置成功,也毫无意义. 换了工作换了环境,重新配置起来.

nginx + memcached session 同步

- - 企业架构 - ITeye博客
squid 缓存疑问 问题归纳:. 1 squid 缓存的数据,何时过期,如何判断缓存的数据已经过期,如何把最新的数据缓入squid 并且替换掉旧的内容. 2 如何判断数据是否应该被缓存. 3 校验失败时,是否给出缓存中旧的内容. 上面的几个问题  都可以通过 squid中的 refresh_pattern 配置项 找到答案.

使用 Nginx 的 keepalive patch,nginx+memcached的TPS提升7倍

- 2sin18 - Linux@SOHU
编者按:本月初 Maxim Dounin,Nginx 最活跃的开发者之一,提交了 upstream keepalive patch,支持 http/fastcgi/memcached,除了减少和 upstream 的网络开销外,也意味着能反向代理 http chunked 响应了. 搜狐技术部CMS组的同学进行了一个简单的测试:.

nginx+tomcat+redis完成session共享

- - 企业架构 - ITeye博客
本文记录nginx+redis+tomcat实现session共享的过程. nginx安装: http://blog.csdn.net/grhlove123/article/details/47834673. redis安装: http://blog.csdn.net/grhlove123/article/details/47783471.

Apache+Tomcat+Memcached共享Session的构架设计

- - CSDN博客架构设计推荐文章
一、       方案目标. 实现互动留言系统、后台发布系统的高可用性,有效解决高并发量对单台应用服务器的冲击,确保应用服务器单点故障不影响系统正常运行. 二、       部署架构. 采取Tomcat集群的部署方式,Apache通过proxy_module代理方式对用户的请求进行负载均衡,转发至tomcat集群中的某一实例进行处理,tomcat集群之间通过Memcached高性能缓存集群共享持久Session.

Nginx + Tomcat + HTTPS 配置原来不需要在 Tomcat 上启用 SSL 支持

- -
之前在网上搜索到的很多文章在描述. Nginx + Tomcat启用 HTTPS 支持的时候,都必须在 Nginx 和 Tomcat 两边同时配置 SSL 支持. 但我一直在想为什么就不能按照下面的方式来配置呢. 就是 Nginx 上启用了 HTTPS,而 Nginx 和 Tomcat 之间走的却是普通的 HTTP 连接.

centos下搭建nginx+tomcat实现集群负载与session复制

- - CSDN博客系统运维推荐文章
系统均选用最小化安装的centos 5.7. 客户端通过访问nginx做的负载均衡层去访问后端的web运行层(tomcat),如下图:. 另外,关于session复制原理,简单来说如下图:. 负载层:192.168.254.200. 安装:pcre、nginx、nginx-upstream-jvm-route-0.1.

Nginx+keepalived做双机热备加tomcat负载均衡

- - 开心平淡对待每一天。热爱生活
   Nginx+keepalived做双机热备加tomcat负载均衡. 一.Nginx配置. 1.安装Nginx所需pcre库. 2.安装Nginx. ./configure: error: SSL modules require the OpenSSL library.Centos需要安装openssl-devel Ubuntu则需要安装:sudo apt-get install libssl-dev.