Nginx+tomcat load Balancing and session replication

Source: Internet
Author: User
Tags documentation session id openssl xmlns server port tomcat tomcat server

This article describes the traditional Tomcat load balancing and session replication. Session replication is based on JVM memory, and of course, in today's internet big Data age, there are better alternatives, such as storing session data in Redis. 1. Installing Nginx

First download nginx, I downloaded the version is 1.11.4 version. Http://nginx.org/download/nginx-1.11.4.zip.

After downloading, unzip.

After the installation is complete, configure the nginx.conf file under the Conf directory.

The main additions include the following:

     #tomcat的三个服务
      upstream MySite {
      server localhost:18080 weight=5;
      Server localhost:28080 weight=5;
      Server localhost:38080 weight=5;
        }

Proxy_pass Http://mysite;  
            #添加如下3个配置后, when a server goes down, the switchover speed will be very fast, at this time the configuration is 1 seconds  
            proxy_connect_timeout   1;   
            Proxy_send_timeout      1;  
            Proxy_read_timeout      1;  

The

Complete file is as follows:

#user nobody;

Worker_processes 1;
#error_log 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; #log_format Main ' $remote _addr-$remote _user [$time _local] "$request" ' # ' $status $body _bytes_sen

    T "$http _referer" ' # ' "$http _user_agent" "$http _x_forwarded_for";

    #access_log Logs/access.log Main;
    Sendfile on;

    #tcp_nopush on;
    #keepalive_timeout 0;

    Keepalive_timeout 10;
    
    
    #gzip on;
      #tomcat的三个服务 upstream MySite {server localhost:18080 weight=5;
      Server localhost:28080 weight=5;
    Server localhost:38080 weight=5;
        } server {Listen 80;

        server_name localhost;

        #charset Koi8-r;

        #access_log Logs/host.access.log Main; LocatIon/{root HTML;
            Index index.html index.htm;
            Proxy_pass Http://mysite; 
            #添加如下3个配置后, when a server goes down, the switchover speed will be very fast, at this time the configuration is 1 seconds proxy_connect_timeout 1;
            Proxy_send_timeout 1;
        Proxy_read_timeout 1;

        } #error_page 404/404.html;
        # REDIRECT Server error pages to the static page/50x.html # Error_page 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 {# l
    Isten 8000;
    # Listen somename:8080;

    # server_name somename alias Another.alias;
    # location/{# root HTML;
    # index index.html index.htm;
    #} #} # HTTPS Server # #server {# listen 443 SSL;

    # server_name localhost;
    # ssl_certificate Cert.pem;

    # Ssl_certificate_key Cert.key;
    # Ssl_session_cache shared:ssl:1m;

    # ssl_session_timeout 5m; # ssl_ciphers high:!anull:!
    MD5;

    # ssl_prefer_server_ciphers on;
    # location/{# root HTML; # index Index.htmL index.htm;
 #    }
    #}

}

2. Tomcat Configuration

After you download tomcat, create a new three folder Tomcat1, Tomcat2, TOMCAT3, and then copy the extracted files into these three folders and change the Server.xml files in each tomcat directory.

Change the server and connector ports. The port of the server in TOMCAT1 is 18005,connector port for 18080,;TOMCAT2, respectively, the 28005,28080;TOMCAT3 is 38085, 38080 respectively.

For session replication, the following configuration is also required for Server.xml:

(1) Uncomment the cluster node. (2) Keep the value of each engine node Jvmroute is the same.

The following is the Server.xml configuration file in TOMCAT1:

<?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 are not a use of this file except in compliance with the License.  Obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 unless required by applicable Law or agreed into writing, software distributed under the License is distributed on a "as is" BASIS, without Warra
  Nties 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 if you are not define subcomponents such as "valves" at
     This level. Documentation At/docs/confiG/server.html--<server port= "18005" shutdown= "shutdown" > <listener classname= " Org.apache.catalina.startup.VersionLoggerListener "/> <!--Security Listener. Documentation at/docs/config/listeners.html <listener classname= "Org.apache.catalina.security.SecurityListener "/>-<!--APR Library loader. Documentation At/docs/apr.html--<listener classname= "Org.apache.catalina.core.AprLifecycleListener" Sslengine= "on"/> <!--Prevent memory leaks due to use of particular Java/javax apis--> <listener Classnam E= "Org.apache.catalina.core.JreMemoryLeakPreventionListener"/> <listener classname= " Org.apache.catalina.mbeans.GlobalResourcesLifecycleListener "/> <listener classname="  Org.apache.catalina.core.ThreadLocalLeakPreventionListener "/> <!--Global JNDI Resources documentation at /docs/jndi-resources-howto.html-<GlobalNamingResources> <!--Editable User database thatcan also be used by Userdatabaserealm to authenticate users--<resource name= "Userdatabase" auth= "Container" type= "Org.apache.catalina.UserDatabase" description= "User database that can be upd Ated and saved "factory=" Org.apache.catalina.users.MemoryUserDatabaseFactory "Pathname=" conf/t  Omcat-users.xml "/> </GlobalNamingResources> <!--a" Service "is a collection of one or more" connectors " That share a single "Container" Note:a "Service" was not itself a "Container" and so could not define SUBCO
       Mponents such as "valves" at the 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=" "minsparethreads=" 4 "/>--<! --A "Connector" represents an endpoint by which requests is received and responses is returned. Documentation At:java HTTP Connector:/docs/config/http.html Java AJP Connector:/docs/config/ajp.htm L APR (HTTP/AJP) Connector:/docs/apr.html Define a non-ssl/tls http/1.1 Connector on port 8080--&G
    T  <connector port= "18080" protocol= "http/1.1" connectiontimeout= "20000" redirectport= "8443" /> <!--A "Connector" using the shared thread pool--> <!--<connector executor= "Tomcatthreadpo Ol "port=" 8080 "protocol=" http/1.1 "connectiontimeout=" 20000 "redirectport=" 8 443 "/>-<!--Define A/TLS http/1.1 Connector on port 8443 This Connector uses the NIO IM Plementation with the JSSE engine.
    When using the JSSE engine, the JSSE configuration attributes must is used. -<!--&LT Connector port= "8443" protocol= "Org.apache.coyote.http11.Http11NioProtocol" maxthreads= "sslenabled=" tr
                         UE "> <SSLHostConfig> <certificate certificatekeystorefile=" Conf/localhost-rsa.jks " Type= "RSA"/> </SSLHostConfig> </Connector> <!--Define a S Sl/tls http/1.1 Connector on port 8443 with HTTP/2 This Connector uses the apr/native implementation. When using the apr/native implementation or the OpenSSL engine with NIO or NIO2 then the OpenSSL Configu
    Ration attributes must be used. -<!--<connector port= "8443" protocol= "Org.apache.coyote.http11.Http11AprProtocol" Maxt
        hreads= "sslenabled=" "true" > <upgradeprotocol classname= "Org.apache.coyote.http2.Http2Protocol"/>
               <SSLHostConfig> <certificate certificatekeyfile= "Conf/localhost-rsa-key.pem"          Certificatefile= "Conf/localhost-rsa-cert.pem" certificatechainfile= "conf/localhost-rsa-

    Chain.pem "type=" RSA "/> </SSLHostConfig> </Connector>-- <!--Define an AJP 1.3 Connector on port 8009--<connector port= "18009" protocol= "ajp/1.3" redirectport= "8  443 "/> <!--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-<!--should set Jvmroute to support load-balancing via AJP IE : <engine name= "Catalina" defaulthost= "localhost" jvmroute= "jvm1" >-<engine name= "Catalina" de faulthost= "localhost" jvmroute= "jvm1" > <!--for clustering, please take a look at documentation at:/docs/cluster-howto.html (easy to)/docs/config/cluster.html (ref
  

      Erence documentation)--<cluster classname= "Org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
      <!--use the Lockoutrealm to prevent attempts to guess user passwords via a brute-force attack- <realm classname= "Org.apache.catalina.realm.LockOutRealm" > <!--this Realm uses the Userdatabase config  Ured in the global JNDI resources under the key "Userdatabase". Any edits that is performed against this userdatabase is immediately available for use by the  Realm. --<realm classname= "Org.apache.catalina.realm.UserDatabaseRealm" Resourcename= "userdatabase "/> </Realm>  
3. Deploy Web Engineering 

Create a new Web project, I named Session_test here, the main is Web. XML and index.jsp files, the project is deployed into Tomcat1-3. In order to differentiate between each Tomcat server, index.jsp is slightly different.

Web. xml: Added <distributable/>

<?xml version= "1.0" encoding= "UTF-8"?> <web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee/http Java.sun.com/xml/ns/javaee/web-app_3_0.xsd "id=" webapp_id "version=" 3.0 "> <display-name>session_test< /display-name> <distributable/> <welcome-file-list> <welcome-file>index.html</welcome-fi Le> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> & Lt;welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> < welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <description>th  IS is the description of my EE component</description> <display-name>this is the display name of my EE Component</display-name> </web-app>
index.jsp

<% @page language= "java"%>


At this point, all configurations and deployments are complete. 4. Start Nginx and Tomcat service (1) Start nginx Service open command line cmd, switch to nginx installation directory, run start Nginx start service.

Note: The command to stop the Nginx service is nginx-s stop

(2) Start TOMCAT1, TOMCAT2 and TOMCAT3 services respectively.

Simply double-click the Startup.bat file in the bin directory under Tomcat to launch the corresponding Tomcat service. 5, test Load balancer and session copy open the browser input http://localhost/session_test, the effect is as follows, indicating that access is TOMCAT3 server
Refresh the browser, the interface transformation is as follows, indicating access to the TOMCAT1 server.

As can be seen from the above two pictures, Nginx will randomly assign each request to a different Tomcat server, this is: load balancing. The SessionID value of each request does not change, indicating that the session replication was successful.







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.