Nginx+Tomcat for load balancing under Windows

Nginx+Tomcat for load balancing under Windows

1. Why do you need to load balance the Tomcat server:

As a web server, the Tomcat server has a concurrent number between 300 and 500. If the concurrent number exceeds 500, Tomcat will not be able to respond to new requests, which will seriously affect the operation of the website.

In addition, in the case of a large number of visits, the number of threads of Tomcat will continue to increase.
Since Tomcat itself has control over the memory usage, when the memory usage reaches the maximum value, there will be memory overflow, and the access to the website will be severely timed out. At this time, it is necessary to restart Tomcat to release the occupied memory. will block the website from running.

Therefore, it is necessary to do load balancing for Tomcat. At present, the mainstream server that can do load balancing with Tomcat is Apache, but Nginx has gradually become the first choice for many load balancing servers due to its multiple functions and simple configuration. The concurrent number of Nginx can reach 50,000, so in theory, it can be configured with Tomcat at a ratio of 1:100, which can well solve the problem of website concurrency bottleneck.

2. Nginx+Tomcat load balancing configuration method under Windows:

The load balancing of Nginx+Tomcat under Windows is much simpler than that under Linux, because both Nginx and Tomcat only need to download the installation package under Windows and unzip it to a certain directory, and then configure it.

The Nginx I choose is the version of nginx-0.8.49, and the version of Tomcat is apache-tomcat-6.0.26.

Here is the configuration process:

1. Unzip the nginx-0.8.49.rar compressed package directly to a directory (such as D:/load balancing/nginx).

2. Decompress the apache-tomcat-6.0.26.rar compressed package. Because it is for load balancing, at least two should be decompressed. (For example, extract them to D:/Load Balance/tomcat_1 and D:/Load Balance/tomcat_2 respectively).

3. Publish the project to be published to the webapps under the two Tomcat root directories, and ensure that the project names under the two Tomcats are the same.

4. Modify one of the Tomcat configuration files. The configuration file is located in /conf/ in the Tomcat directory. The file name is server.xml. Modify the

<Server port="8005" shutdown="SHUTDOWN"><Server port="8006" shutdown="SHUTDOWN">

modify the

<Connector port="8080" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8443" />

for

<Connector port="8088" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8443" />

The modified file is as follows:

<?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 may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES 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 you may not
     define subcomponents such as "Valves" at this level.
     Documentation at /docs/config/server.html
 -->
<Server port="8006" shutdown="SHUTDOWN">

  <!--APR library loader. Documentation at /docs/apr.html -->
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
  <Listener className="org.apache.catalina.core.JasperListener" />
  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <!-- JMX Support for the Tomcat server. Documentation at /docs/non-existent.html -->
  <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />

  <!-- Global JNDI resources
       Documentation at /docs/jndi-resources-howto.html
  -->
  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>

  <!-- A "Service" is a collection of one or more "Connectors" that share
       a single "Container" Note:  A "Service" is not itself a "Container", 
       so you may not define subcomponents such as "Valves" at 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="150" minSpareThreads="4"/>
    -->


    <!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL HTTP/1.1 Connector on port 8080
    -->
    <Connector port="8088" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8443" />
    <!-- A "Connector" using the shared thread pool-->
    <!--
    <Connector executor="tomcatThreadPool"
               port="8080" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8443" />
    -->           
    <!-- Define a SSL HTTP/1.1 Connector on port 8443
         This connector uses the JSSE configuration, when using APR, the 
         connector should be using the OpenSSL style configuration
         described in the APR documentation -->
    <!--
    <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
               maxThreads="150" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />
    -->

    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />


    <!-- 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 -->

    <!-- You should set jvmRoute to support load-balancing via AJP ie :
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">         
    --> 
    <Engine name="Catalina" defaultHost="localhost">

      <!--For clustering, please take a look at documentation at:
          /docs/cluster-howto.html  (simple how to)
          /docs/config/cluster.html (reference documentation) -->
      <!--
      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
      -->        

      <!-- The request dumper valve dumps useful debugging information about
           the request and response data received and sent by Tomcat.
           Documentation at: /docs/config/valve.html -->
      <!--
      <Valve className="org.apache.catalina.valves.RequestDumperValve"/>
      -->

      <!-- This Realm uses the UserDatabase configured in the global JNDI
           resources under the key "UserDatabase".  Any edits
           that are performed against this UserDatabase are immediately
           available for use by the Realm.  -->
      <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
             resourceName="UserDatabase"/>

      <!-- Define the default virtual host
           Note: XML Schema validation will not work with Xerces 2.2.
       -->
      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"  
               prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/>
        -->
      </Host>
    </Engine>
  </Service>
</Server>

5. Modify the Nginx configuration file nginx.conf, which is located under /conf in the Nginx root directory.

① Add the following configuration after #gzip on;:

upstream backend {
    server localhost:8080;
    server localhost:8088;
    ip_hash;
}

Among them, server localhost:8080 is the startup address of the first Tomcat, server localhost:8088 is the startup address of the second Tomcat, and ip_hash is used for session synchronization.

② Modify the listen 80 in the first server{} configuration; to a new port number, because my local port 80 is occupied by IIS, so change it to listen 800;. and will

location / {
    root   html;
    index  index.html index.htm;
}

Change it to:

location / {
    root   html;
    index  index.html index.htm;
proxy_pass                  http://backend;  
   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; 
}

The proxy_pass parameter corresponds to upstream backend{}.

After the above steps, the load balancing configuration is completed. Start two tomcats before the following, then double-click the nginx.exe file in the nginx root directory or start nginx with start nginx, open the browser, and enter the address: http://localhost: 800 to see the following screen:

write picture description here

This shows that it has successfully jumped to Tomcat. Let's take the specific project of the call show as an example. Enter the address: http://localhost:800/ldxwebpersonal/personal/index/index.action to see the home page of the call show below:

write picture description here

Let's take a look at the log display below the two Tomcats:

Display with port number 8080:

write picture description here

Display with port number 8088:

write picture description here

This shows that the two tomcats have achieved load balancing.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325769202&siteId=291194637