Implementation of session sticky LNMT based on cookie

Source: Internet
Author: User


Architecture diagram

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/77/13/wKioL1ZipZjxfgppAABGRleQs2g582.png "title=" Lnmt.png "alt=" Wkiol1zipzjxfgppaabgrleqs2g582.png "/>

Environment description

  • all hosts based on CentOS 6.5

  • back-end host : ha111 ip:192.168.61.139 installing Tomcat

  • back-end host : ha222 ip:192.168.61.140 installing Tomcat

  • Load Balancer Scheduling host:rs1 ip:192.168.61.131 installation Nginx

ha111 Host Configuration

1. JDK installation, can be used Sun or openjdk, here with Sun 's

    • , download the linuxx64 rpm installation package to
      Http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

    • Installation
      RPM-IVH jdk-8u65-linux-x64.rpm

    • environment variable Settings
      vim/etc/profile.d/java.sh
      Export Java_home=/usr/java/latest
      Export path= $JAVA _home/bin: $PATH

    • Effect
      source/etc/profile.d/java.sh

2.tomcat Installation

    • , here with 8.0 , apache-tomcat-8.0.28.tar.gz.
      http://tomcat.apache.org/download-80.cgi

    • Installation
      Tar xvzf apache-tomcat-8.0.28.tar.gz-c/usr/local/
      ln-sv/usr/local/apache-tomcat-8.0.28//usr/local/tomcat

    • environment variable Settings
      vim/etc/profile.d/tomcat.sh
      Export Catalina_home=/usr/local/tomcat
      Export path= $CATALINA _home/bin: $PATH

    • Immediate effect
      source/etc/profile.d/tomcat.sh

  • Adding system Services
    Vim/etc/init.d/tomcat
    #!/bin/sh
    # Tomcat Init scriptfor Linux.
    #
    # chkconfig:2345 96 14
    # Description:theapache Tomcat servlet/jsp container.
    # java_opts= '-xms64m-xmx128m '
    Java_home=/usr/java/latest#Note that these paths are correct
    Catalina_home=/usr/local/tomcat#Note that these paths are correct
    Exportjava_home Catalina_home#This sentence can not add, the front environment is set up on the line

    Case $ in
    Start
    exec $CATALINA _home/bin/catalina.sh start;;
    Stop
    exec $CATALINA _home/bin/catalina.sh stop;;
    Restart
    $CATALINA _home/bin/catalina.sh Stop
    Sleep 2
    exec $CATALINA _home/bin/catalina.sh start;;
    *)
    echo "Usage: ' basename $ ' {Start|stop|restart}"
    Exit 1
    ;;
    Esac

  • Modify the default listener's 8080 port to 80
    Vim/usr/local/tomcat/conf/server.xml
    <connector port= "8080" protocol= "http/1.1" to
    <connector port= "protocol=" http/1.1 "

    chmod +x/etc/init.d/tomcat
    Chkconfig--add Tomcat
    Service Tomcat start Services

  • Build a test Site
    Cd/usr/local/tomcat/webapp
    MKDIR-PV Testapp/{web-inf,classes,meta-inf,lib}

  • Set up homepage
    Vim testapp/index.jsp
    <%@ page language= "java"%>
    <body>
    <tableborder= "1" >
    <tr>
    <td>session id</td>
    <%session.setattribute ("dx.com", "dx.com"); %>
    <td><%= Session.getid ()%></td>
    </tr>
    <tr>
    <td>created on</td>
    <td><%=session.getcreationtime ()%></td>
    </tr>
    </table>
    </body>

  • Test Curl http://localhost:8080/testapp/index.jsp

3. ha222 Host configuration is the same as ha111

4. Nginx Load Balancer Scheduler rs1 Configuration

    • Install package
      Download the latest stable version of nginx-1.8.0
      Http://nginx.org/en/download.html

    • Installation dependencies
      Yum-y Install Pcre-devel Openssl-devel
      Tar xvzf nginx-1.8.0.tar-c/root/

    • Sticky Module
      wget https://nginx-sticky-module.googlecode.com/files/nginx-sticky-module-1.1.tar.gz
      Tar xvzf nginx-sticky-module-1.1.tar.gz-c/root/

  • Compile and install after decompression

  • ./configure--prefix=/usr/local/nginx--conf-path=/etc/nginx/nginx.conf--user=nginx--group=nginx--error-log-path =/var/log/nginx/error.log--http-log-path=/var/log/nginx/access.log--pid-path=/var/run/nginx/ Nginx.pid--lock-path=/var/lock/nginx.lock--with-http_ssl_module--with-http_stub_status_module--with-http_gzip_ Static_module--with-http_flv_module--with-http_mp4_module--http-client-body-temp-path=/var/tmp/nginx/ client--http-proxy-temp-path=/var/tmp/nginx/proxy--http-fastcgi-temp-path=/var/tmp/nginx/ Fastcgi--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi--add-module=. /nginx-sticky-module-1.1

    Make && make install
    Note: If you need to turn onSticky sessionfunction, need to add the above red this sentence,nginx-sticky-module-1.1where the module files are located, hereNginxthe source file andStickymodule source files in the same directory

    Create the directory specified above, or you will get an error when booting
    MKDIR-PV/VAR/TMP/NGINX/{CLIENT,PROXY,FASTCGI,UWSGI}

  • Modifying a configuration file
    Vim/etc/nginx/nginx.conf
    Add the following content:
    Upstream TestApp {
    Sticky #OpenSession Sticky
    Server 192.168.61.139:80;
    Server 192.168.61.140:80;
    }

    server {
    Listen 80;
    server_name 192.168.61.131;
    Location/{
    Proxy_pass Http://testapp;
    Index index.jsp index.html index.htm;
    }

    Error_page 502 503 504/50x.html;
    Location =/50x.html {
    root HTML;
    }
    }

at this point all configuration is complete, as long as access http://192.168.61.131/testapp/index.jsp can implement a cookie-based session sticky LNMT


Implementation of session sticky LNMT based on cookie

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.