Nginx+Tomcat load balancing configuration

reference document:

Nginx+Tomcat load balancing configuration: http://blog.csdn.net/wave_1102/article/details/44475093
Reference: http://blog.csdn.net/wave_1102/article/details/44475093

 

theory:

[What are the advantages and disadvantages of Nginx and Apache? ] You
can refer to some articles:
http://www.cnblogs.com/huangye-dream/p/3550328.html
https://www.zhihu.com/question/19571087
The advantages of nginx over apache:
(1) Lightweight , the same as a web service, it occupies less memory and resources than apache and is anti-concurrency.
(2) nginx processes requests asynchronously and non-blocking, while apache is blocking. Under high concurrency, nginx can maintain low resources and low consumption. The performance is highly modular design, the writing module is relatively simple, the community is active, and various high-performance modules are produced quickly
. The advantages of apache relative to nginx:
rewrite (URL rewriting), there are more powerful modules than nginx's rewrite, and you can find everything you can think of. Less bugs, nginx has relatively more bugs and is super stable.
Generally speaking,
(1) if you need high-performance web services, use nginx
(2) if you don't need performance and only want stability, then apache. Various functional modules of apache are implemented better than the former, for example, the ssl module is better than the former, and there are many configurable items.
Note: The network IO model of epoll (kqueue on freebsd) is the fundamental reason for the high processing performance of nginx, but not all cases are epoll's victory. If it provides static services, there are only a few files. apache's select model may be more performant than epoll. Of course, this is just an assumption based on the principle of the network IO model, and the real application still needs to be measured.
1. Apache has advantages in dealing with dynamics. Nginx has better concurrency and low CPU memory usage. If rewrite is frequent, then Apache is
better. 2. Ginx handles static files well and consumes less memory. But there is no doubt that apache is still the current mainstream. There are many rich features. So it needs to be matched. Of course, if you can determine that nginx is suitable for your needs, then using nginx will be a more economical way. 3. nginx is
suitable for static, simple and efficient, and apache is suitable for dynamic, stable, Strong function
4....


[How to choose the architecture of Nginx/Apache + Tomcat]
(1) Nginx: an HTTP server that can withstand high concurrency, asynchronous, multiple connections (10,000 levels) can correspond to a process, respond. Based on an event-driven model.
Nginx advantages: load balancing, reverse proxy, processing static files advantages.
(2) Apache: Compared with Nginx, synchronous multi-process model, one connection handles requests to one process. In terms of speed and consumption, Apache cannot withstand high concurrency and will cause downtime.
Advantages of Apache: Compared with the Tomcat server, processing static files is its advantage, and it is fast. Apache is static analysis, suitable for static HTML, pictures, etc. Compared with Nginx, Nginx configuration is simple and simple to configure load balancing, while Apache configuration is complex. The performance of Nginx in processing static files is also better than that of Apache. Apache provides more components than Nginx.
(3) Tomcat: Dynamic parsing container, processing dynamic requests, and compiling JSP\Servlet container.
Tomcat advantage: handle dynamic requests and process requests in a threaded manner.

Nginx has a dynamic separation mechanism. Static requests can be processed directly through Nginx, and dynamic requests are forwarded to the background for Tomcat to process.

The architecture combinations commonly used for dynamic and static separation are:
(1) It was used earlier, apache+tomcat
(2) nginx+tomcat
(3) nginx+apache+tomcat
Obviously scheme 1 is relatively old, but some systems are in use due to historical reasons; scheme 3, many people use pache to Be an intermediate layer to detect requests, do some verification, and attack protection, plus CSRF verification, anti-slow HTTP denial. These only need to add a module to apache, and the real background Tomcat only needs to pay attention Logical processing is enough, more because it is more familiar with apache or pays more attention to the stability of apache, although nginx can also achieve the same function

 

Nginx+tomcat is the current mainstream Java web architecture. How to make nginx+tomcat work at the same time? It can also be said that how to use nginx to reverse proxy tomcat backend balance!!!

Tomcat cluster planning

192.168.1.111:8080
192.168.1.112:8080


installation steps

 

Java JDK installation: refer to jdk installation practice

Nginx installation: refer to ngixn.txt

Tomcat installation: refer to tomcat installation
#Official website to download tomcat 6.0.30 or other versions:  

Note: The following measures are used when deploying multiple Tomcats on one

 

#It can be used directly after decompression, the decompression is completed and executed, and two tomcats are copied at the same time, named tomcat1 tomcat2   
mv apache-tomcat-6.0.30  /usr/local/tomcat1  
cp /usr/local/tomcat1  /usr/local/tomcat2 -r  
#Modify the tomcat1 and tomcat2 ports respectively, there are three ports that need to be modified, as follows:  
shutdown port: 8005 is mainly responsible for starting shutdown.  
Ajp port: 8009 is mainly responsible for balancing through ajp (commonly used for apache and tomcat integration)  
http port: 8080 can be accessed directly through the web page (nginx+tomcata integration)  
#Note* If the three ports of tomcat1 are: 8005 8009 8080, then the ports of tomcat2 are +1 on this basis, that is: 8006 8010 8081  

#On a server, the port cannot be repeated, otherwise an error will be reported.   
#After modifying the port, then start two tomcats, the startup command is:  
#How to prompt that there is no such file or insufficient permissions, you need to give execution permission to the sh file in the bin directory of tomcat: chmod o+x *.sh  
/usr/local/tomcat1/bin/startup.sh  
/usr/local/tomcat2/bin/startup.sh  
#After startup, you can see 6 ports using netstat -tnl, which means tomcat1 and tomcat2 are successfully started. You can use http://ip:8080 http://ip:8081 to access the tomcat default page.
 

 

Regardless of whether it is single or multiple, if you need to modify the tomcat release directory for your own directory, you need to make the following adjustments to create two release directories:
(1) mkdir -p /usr/webapps/{www1,www2} 
(2) Edit vi /usr/local/tomcat1/conf/server.xml Add the following line before the last </Host>: <Context path="" docBase="/usr/webapps/www1" reloadable="false"/>
(3 ) Edit vi /usr/local/tomcat2/conf/server.xml and add the following line before the last </Host>: <Context path="" docBase="/usr/webapps/www2" reloadable="false"/>
(4) Contents of the tomcat1 release directory:

    <html>  
    <body>  
    <h1>TOMCAT_1 JSP Test Page</h1>  
    <%=new java.util.Date()%>  
    </body>  
    </html>
(5) tomcat2 Publish directory content:

    <html>  
    <body>  
    <h1>   TOMCAT_2 JSP Test Page</h1>  
    <%=new java.util.Date()%>  
    </body>  
    </html>

Then visit http://ip:8080, 8081 to view the test content.

Note: Since I use 2 machines, I set the content in the 2 webapps

(1) mkdir -p /usr/webapps/www

(2) Edit vi /usr/local/src/tomcat7/conf/server.xml and add the following line before the last </Host>: <Context path="" docBase="/usr/webapps/www" reloadable=" false"/>

(3)  tomcat1发布目录内容:test.jsp
    <html>  
    <body>  
    <h1>TOMCAT_1 JSP Test Page</h1>  
    <%=new java.util.Date()%>  
    </body>  
    </html>
(4) tomcat2发布目录内容:test.jsp

    <html>  
    <body>  
    <h1>TOMCAT_2 JSP Test Page</h1>  
    <%=new java.util.Date()%>  
    </body>  
    </html>

 

Start command:

/usr/local/src/tomcat7/bin/startup.sh & tail -f /usr/local/src/tomcat7/logs/catalina.out

 

test:

http://192.168.1.111:8081/test.jsp

http://192.168.1.112:8081/test.jsp

 


Nginx+tomcat integration: The integration of load balancing and dynamic and static separation
is mainly to modify the nginx.conf configuration to give a complete nginx.conf online configuration. Some parameters can be modified according to actual needs:

# vi /usr/local/nginx/conf/nginx.conf

 

   user www www;  
    worker_processes 8;  
    pid /usr/local/nginx/nginx.pid;  
    worker_rlimit_nofile 102400;  
    events  
    {  
    use epoll;  
    worker_connections 102400;  
    }  
    http  
    {  
      include       mime.types;  
      default_type  application/octet-stream;  
      fastcgi_intercept_errors on;  
      charset  utf-8;  
      server_names_hash_bucket_size 128;  
      client_header_buffer_size 4k;  
      large_client_header_buffers 4 32k;  
      client_max_body_size 300m;  
      sendfile on;  
      tcp_nopush     on;  

      keepalive_timeout 60;  

      tcp_nodelay on;  
      client_body_buffer_size  512k;  

      proxy_connect_timeout    5;  
      proxy_read_timeout       60;  
      proxy_send_timeout       5;  
      proxy_buffer_size        16k;  
      proxy_buffers            4 64k;  
      proxy_busy_buffers_size 128k;  
      proxy_temp_file_write_size 128k;  

      gzip on;  
      gzip_min_length  1k;  
      gzip_buffers     4 16k;  
      gzip_http_version 1.1;  
      gzip_comp_level 2;  
      gzip_types       text/plain application/x-javascript text/css application/xml;  
      gzip_vary on;  

    ###2012-12-19 change nginx logs  
    log_format  main  '$http_x_forwarded_for - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent"  $request_time $remote_addr';  

    upstream web_app {  
     server 192.168.1.111:8081 weight=1 max_fails=2 fail_timeout=30s;  
     server 192.168.1.112:8081 weight=1 max_fails=2 fail_timeout=30s;  
    }  

    #### chinaapp.sinaapp.com  
    server {  
        listen 80;  
        server_name  chinaapp.sinaapp.com;  
        index index.jsp index.html index.htm;  
        #Publish directory /data/www  
        root  /data/www;  

        location /  
        {  
        proxy_next_upstream http_502 http_504 error timeout invalid_header;  
        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://web_app;  
        expires      3d;  
        }  

      }  

    }

 

Test: http://192.168.1.100:88/test.jsp


Note* The web_app defined by proxy_pass in the server segment needs to be consistent with the web_app defined in the upstream, otherwise the server cannot find a balance.
Best practice: It is achieved by replacing the apache address with the TOMCAT address. The relevant parameters need to be studied
. #Configuration as above, nginx+tomcat reverse proxy load balancing configuration is completed, if you want to separate static and dynamic, you only need to add the following configuration to nginx and it will be OK To configure Nginx dynamic and

static separation 

# vi /usr/local/nginx/conf/nginx.conf

 

location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$  
{  
    root /data/www;  
    #expires defines the cache time of the user's browser as 3 days. If the static page is not updated frequently, it can be set longer, which can save bandwidth and relieve the pressure on the server  
    expires      3d;  
}

 


Best Practices:

(1) This /data/www is built under the nginx file, not under tomcat
(2) The same application folder needs to be created under /data/www, such as /data/www/edu-demo/..

mkdir -p /data/www/edu-demo

(3) Exceptions when dynamic and static separation occurs: dwz Error loading XML document: dwz.frag.xml
Analysis: dwz.frag.xml is a core file that needs to be loaded before it can be used normally.
This file cannot be separated and can be listed separately in the container under

(4) Big pit: http://192.168.1.100:88/edu-demo No, it must be written here http://192.168.1.100:88/edu-demo/index.jsp


在web.xml中加入
    <mime-mapping> 
      <extension>xml</extension> 
      <mime-type>text/xml</mime-type> 
    </mime-mapping>

Guess you like

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