Nginx与Tomcat6集群

Nginx与Tomcat6集群
Tomcat6集群配置
  参考文档 http://tomcat.apache.org/tomcat-6.0-doc/
  Tomcat1 /home/web/tomcat1 Server_port:8005 Conn_port:7518 Conn_AJP13_port:8009 Conn_redirectPort:8443
  <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
  Tomcat2 /home/web/tomcat2 Server_port:8006 Conn_port:7519 Conn_AJP13_port:8010 Conn_redirectPort:8444
  <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">
 
  Tomcat1的server.xml文件如下:(Tomcat2类似)
    <?xml version='1.0' encoding='utf-8'?>
    <Server port="8005" shutdown="SHUTDOWN">   
      ...
        <Connector port="7518" protocol="HTTP/1.1"
                   maxHttpHeaderSize="8192" connectionTimeout="20000"
                   disableUploadTimeout="true" enableLookups="false"
                   redirectPort="8443" maxThreads="600"
                   minSpareThreads="25" maxSpareThreads="75" acceptCount="100" />
        <Connector port="8009" protocol="AJP/1.3"
                   maxHttpHeaderSize="8192" connectionTimeout="20000"
                   disableUploadTimeout="true" maxThreads="600"
                   minSpareThreads="25" maxSpareThreads="75"
                   enableLookups="false" redirectPort="8443" />   
        <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">   
          ... 
            <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
                     channelSendOptions="6">   
              <Manager className="org.apache.catalina.ha.session.BackupManager"
                       expireSessionsOnShutdown="false"
                       notifyListenersOnReplication="true"
                       mapSendOptions="6"/>
              <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="auto"
                          port="5000"
                          selectorTimeout="100"
                          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></Host></Engine></Service></Server>
  Tomcat6的集群主要作用是session的复制,如果Nginx用ip_hash方式固定某IP访问,则Tomcat的集群无需配置。
  但若某IP固定访问的一后端服务器宕机,则此IP下所有访问session丢失。
           
Nginx转发设置
  Nginx的Nginx.conf设置如下:
    user  nobody;
    worker_processes  2;   
    error_log  logs/error.log  notice;
    pid        logs/nginx.pid;  
    worker_rlimit_nofile 65535;
    events {
        use epoll;
        worker_connections  10240;
    }   
    http {
        include       mime.types;
        default_type  application/octet-stream;   
        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;   
        client_header_timeout 3m;
        client_body_timeout 3m;
        send_timeout 3m;   
        sendfile        on;
        tcp_nodelay on;  
        keepalive_timeout  65;   
        upstream servers1 {
            server 192.168.1.190:7518 weight=1;
            server 192.168.1.190:7519 weight=1;
        }   
        server {
            listen       80;
            server_name  localhost;   
            charset utf-8;   
            access_log  logs/host.access.log  main;       
            location / {
                root   html;
                index  index.jsp index.html index.htm;
            }   
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }   
            location /mygold {
                    proxy_pass http://servers1;
                    proxy_set_header X-Real-IP $remote_addr;
                    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                    proxy_set_header Host $http_host;
                    client_max_body_size    100m;
                    proxy_connect_timeout   60;
                    #proxy_busy_buffers_size 2048k;
            } } }
  Nginx转发会自动识别后端服务器的存活状态。一旦发现其中一台宕机,会把请求转发给其它服务器。
           
应用程序的session同步
  修改程序目录WEB-INF下的web.xml:在</web-app>前加如下行:
  <distributable/>即可实现session同步
  session固定,ip_hash方式.
    ip_hash方式仅能用ip这个因子来分配后端,即针对同一IP固定访问后端一服务器.
    配置在upstream内加一行 ip_hash; 不过以下两种方式中慎用:
      nginx不是最前端的服务器。
      nginx的后端还有其它方式的负载均衡。
  另一种方式是用模块upstream_hash. 能用作url_hash.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值