nginx+tomcat的會話同步(2)

Nginx+tomcat session會話同步(2)

Nginx的安裝(在安裝nginx前需要安裝prce

首先安裝pcre

tar zxvf pcre-7.8.tar.gz

cd zxvf pcre-7.8

./configure

make

make install

默認路徑是/usr/local

 

 

然後安裝nginx

tar zxvf nginx-0.8.17.tar.gz

cd nginx-0.8.17

./configure --prefix=/usr/local/nginx

make

make install

 

三負載均衡的實現以及session會話的同步。

在這裏我們要注意三個配置文件,兩個是tomcat server.xml(主配置文件)和web.xml,默認路徑在:/usr/local/apache-tomcat-6.0.29/conf下面。另一個是nginx的配置文件nginx.conf ,默認路徑在:/usr/local/nginx/conf下面。另外還得注意/usr/local/apache-tomcat-6.0.29/webapps裏面建一個文件夾aa(看你個人愛好)再在aa中寫一個index.jsp的測試頁(兩臺機器的webapps都要這麼做哦)

現在我們要做的就是修改配置文件了

  修改server.xml

在兩臺服務器的tomcat的配置文件中分別找到:

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

分別修改爲:

Tomcat01:

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

Tomcat02:

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

 

再在其後加上以下內容

<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.2"

 

                        bind="192.168.0.51" //(tomcat2裏下tomcat2ip)

 

 

                        port="45564"

 

                        frequency="500"

 

                        dropTime="3000"/>

 

<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"

 

                      address="192.168.0.51" //(tomcat2裏下tomcat2ip)

 

                        autoBind="100"

 

                      port="5001" //tomcat2的不一樣啊我弄的是5002

 

                      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>

Tomcat2一樣的就不說。

Tomcat的測試(我是先把測試頁放了進去的)

  修改nginx.conf

user  nobody;

worker_processes  1;

 

#error_liog  logs/error.log;

error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

 

pid        logs/nginx.pid;

 

events {

    worker_connections  1024;

}

 

http {

    include       mime.types;

    default_type  application/octet-stream;

    server_tokens off;

    #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;

 

    sendfile        on;

    tcp_nopush     on;

    tcp_nodelay    on;

    #keepalive_timeout  0;

    keepalive_timeout  65;

 

    gzip  on;

    gzip_min_length  10;

    gzip_buffers     4 8k;

    gzip_http_version 1.1;

    gzip_types       text/plain application/x-javascript text/css  application/xml;

    #sendfile         on;  

    upstream localhost

        {

        server 192.168.0.51:8080  weight=1;

        server 192.168.0.52:8080  weight=1; //設置訪問的機率數值越大訪問的機率就越大

#       ip_hash;

}

 

    server {

        listen       80; //監聽的端口

        server_name  localhost; //服務器的地址可以是ip

 

        charset utf-8; //編碼格式

 

       # access_log  logs/host.access.log  main;

 

        location / {

        root /usr/local/apache-tomcat-6.0.29/webapps/aa; //要訪問頁面的路徑

        index index.jsp  index.html index.htm;

         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;

         proxy_pass  http://localhost;

 

}

 

        #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   html;

        }

 

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80

        #

        #location ~ \.php$ {

        #    proxy_pass   http://127.0.0.1;

        #}

 

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

        #

        #location ~ \.php$ {

        #    root           html;

        #    fastcgi_pass   127.0.0.1:9000;

        #    fastcgi_index  index.php;

        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

        #    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;

 

    #    location / {

    #        root   html;

    #        index  index.html index.htm;

    #    }

    #}

 

 

    # HTTPS server

    #

    #server {

    #    listen       443;

    #    server_name  localhost;

 

    #    ssl                  on;

    #    ssl_certificate      cert.pem;

    #    ssl_certificate_key  cert.key;

 

    #    ssl_session_timeout  5m;

 

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;

    #    ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;

    #    ssl_prefer_server_ciphers   on;

 

    #    location / {

    #        root   html;

    #        index  index.html index.htm;

    #    }

    #}

}

  修改web.xml

<web-app> </web-app>之間加上<distributable/>

  /usr/local/apache-tomcat-6.0.29/webapps下創建aa文件夾並在aa裏編寫一段index.jsp的測試頁。

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%

%>

<html>

<head>

    </head>

      <body>

       51

       <!--server1 這裏爲 51 -->

        <br />

       <%out.print(request.getSession()) ;%>

       <!--輸出session-->

        <br />

        <%out.println(request.getHeader("Cookie")); %>

      <!--輸出Cookie-->

      </body>

</html>

 

 

測試哈結果

點擊刷新數字會在5152之間跳動就證明成功了。

 

 

http://bbs.linuxtone.org/forum-viewthread-tid-1195-highlight-tomcat%2Bsession%2B%E5%90%8C%E6%AD%A5.html

 

本人初學Linux希望大家多多指教不對之處還請大家指正。在這裏還得感謝summer,遊弋の亡靈。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章