Nginx+tomcat負載平衡session問題解決

來源:互聯網
上載者:User

tomcat通過配置也可以實現叢集session共用,但是不建議這樣做,有更多更好的方案可以解決,如:Redis之類

分別下載

tomcat http://archive.apache.org/dist/tomcat/tomcat-7/v7.0.22/bin/apache-tomcat-7.0.22.tar.gz

nginx http://nginx.org/download/nginx-1.1.14.tar.gz

 

準備兩個虛擬機器:

server1 192.168.1.112

server2 192.168.1.64

 

Tomcat直接解壓,運行,使用預設的8080連接埠

tar zxvf apache-tomcat-7.0.22.tar.gz
cd apache-tomcat-7.0.22/bin
./startup.sh

訪問http://192.168.1.112:8080和http://192.168.1.64:8080出現Tomcat首頁即可

 

接下來安裝nginx, nginx我就直接安裝在server1上

nginx_upstream_jvm_route是一個Nginx的擴充模組,用來實現基於Cookie的SessionSticky的功能, 去SVN下載最新版

svn checkout http://nginx-upstream-jvm-route.googlecode.com/svn/trunk/ /root/dev/nginx-upstream-jvm-route-read-only

解壓nginx

tar zxvf nginx-1.1.14.tar.gzcd nginx-1.1.14

 運行

patch -p0 < /root/dev/nginx-upstream-jvm-route-read-only/jvm_route.patch
./configure --prefix=/etc/nginx --with-http_stub_status_module --add-module=/root/dev/nginx-upstream-jvm-route-read-only/
make
make install

在nginx安裝目錄下的conf/目錄建立一個檔案proxy.conf(/etc/nginx/conf/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;

修改nginx設定檔/etc/nginx/conf/nginx.conf

完整配置如下

http {    include       mime.types;    #反向 Proxy配置    include      proxy.conf;    default_type  application/octet-stream;    sendfile        on;
    keepalive_timeout  65;    upstream server1{    server 192.168.1.112:8080 srun_id=tomcat1;    server 192.168.1.64:8080 srun_id=tomcat2;    jvm_route $cookie_JSESSIONID|sessionid reverse;    }    server {        listen       80;        server_name  localhost;    access_log    /var/log/nginx/access.log;    location ~ ^/NginxStatus/ {         stub_status on; #Nginx 狀態監控配置        access_log off;     }    location ~ ^/(WEB-INF)/ {         deny all;    }        location / {            root   html;            index  index.html index.htm;        proxy_pass http://server1;        }    location /doc {        root   /usr/share;        autoindex on;        allow 127.0.0.1;        deny all;    }    location /images {        root   /usr/share;        autoindex on;    }

具體配置說明請參考《輕量級WEB伺服器Nginx》

然後運行nginx

/etc/nginx/sbin/nginx

訪問http://192.168.1.112/NginxStatus,可以看到nginx狀態

然後修改tomcat設定檔, 開啟apache-tomcat-7.0.22/conf/server.xml, 找到最下面的<Engine name="Catalina" defaultHost="localhost">節點, 修改為

server1

<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">

並插入如下配置

<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"                  channelSendOptions="8">           <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="224.0.0.4"                         port="45564"                         frequency="500"                         dropTime="3000"/>             <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"                       address="192.168.1.112"                      port="4000"                      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=""/>           <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>           <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>           <ClusterListener className

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.