Nginx+tomcat load Balancing configuration method under Linux _nginx

Source: Internet
Author: User
Tags mkdir java web

Nginx+tomcat is the current mainstream Java Web architecture, how to let nginx+tomcat work at the same time, can also say how to use Nginx to reverse proxy tomcat back-end equalization? The direct installation configuration is as follows:

1. JAVA JDK Installation:

#下载相应的jdk软件包, and then unpack the installation, I have the package name here: jdk-7u25-linux-x64.tar.gz  
  
tar-xzf jdk-7u25-linux-x64.tar.gz; mkdir-p/usr/java/; MV jdk1.7.0_25//usr/java/.  
  
#然后配置环境变量, so that you can refer to JDK anywhere, as follows:  
  
#vi/etc/profile The last face add the following statement:  
  
export java_home=/usr/java/jdk1.7.0_25  
  
Export classpath= $CLASSPATH: $JAVA _home/lib: $JAVA _home/jre/lib 
  
export path= $JAVA _home/bin: $JAVA _home/jre/bin: $PATH: $HOMR/bin 
  
#source/etc/profile  #使环境变量马上生效  
  
#java--version  

2, Nginx Installation:

wget http://nginx.org/download/nginx-1.2.6.tar.gz  
  
useradd www  
  
tar zxvf nginx-1.2.6.tar.gz  
  
CD nginx-1.2.6/  
  
./configure--user=www--group=www--prefix=/usr/local/nginx 
 \--with-http_stub_status_module --with-http_ssl_module make  
  
&& make install 
  
#Nginx安装完毕, and then use the command:/usr/local/nginx/sbin/nginx-t test OK, Represents Nginx installation success.  
  

3, Tomcat installation:

#官方网站下载tomcat 6.0.30 or other version:  
  
cd/usr/src && tar xzf apache-tomcat-6.0.30.tar.gz  
  
#直接解压就可以使用, decompression complete execution, Simultaneously copies two Tomcat, named TOMCAT1 tomcat2  
  
mv APACHE-TOMCAT-6.0.30/USR/LOCAL/TOMCAT1 
  
cp/usr/local/tomcat1/usr/local /tomcat2-r  
  
#分别修改tomcat1和tomcat2 Port, there are three ports that need to be modified, as follows:  
  
shutdown Port: 8005 main responsible for startup shutdown.  
  
AJP Port: 8009 is primarily responsible for using AJP equalization (commonly used for Apache and Tomcat consolidation)  
  
HTTP ports: 8080 can be accessed directly via Web pages (nginx+tomcata integration)  
  
#注 * If the TOMCAT1 three ports are: 8005 8009 8080, then TOMCAT2 port on this basis are +1, that is: 8006 8010 8081  
  
#一台服务器上, the port can not be repeated, otherwise it will be an error.  
  
#修改完端口后, and then start two Tomcat, the launch command is:  
  
#如何提示没有这个文件或者权限不足, requires Tomcat's Bin directory to give execution permission to the sh file: chmod o+x  *.sh  
  
/ usr/local/tomcat1/bin/startup.sh  
  
/usr/local/tomcat2/bin/startup.sh  
  

#如果需要修改tomcat发布目录为自己制定的目录, you need to make the following adjustments to create two publishing directories:

Copy Code code as follows:

Mkdir-p/USR/WEBAPPS/{WWW1,WWW2}

Edit Vi/usr/local/tomcat1/conf/server.xml The last </Host> previous line add the content:
Copy Code code as follows:

<context path= "" docbase= "/USR/WEBAPPS/WWW1" reloadable= "false"/>

2. Edit Vi/usr/local/tomcat2/conf/server.xml The last </Host> the previous line add the content:
Copy Code code as follows:

<context path= "" docbase= "/USR/WEBAPPS/WWW2" reloadable= "false"/>

3.TOMCAT1 Publish Directory content:

 
 

4.tomcat2 Publish Directory content:

 
 

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

5, Nginx+tomcat Integration:

Integration is mainly to modify the nginx.conf configuration, to a complete nginx.conf online configuration, some of the parameters can be modified according to the actual requirements:

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 _byte  
         
S_sent "$http _referer" "$http _user_agent" $request _time $remote _addr ';  
 Upstream Web_app {server 127.0.0.1:8080 weight=1 max_fails=2 fail_timeout=30s;  
Server 127.0.0.1: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;  
    
  #发布目录/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;  }  
   
 }  
  
}

#注 * Server segment Proxy_pass defined Web_app need to be consistent with the Web_app defined in upstream, otherwise the server cannot find a balance.

#如上配置, Nginx+tomcat reverse proxy load balanced configuration is complete, if you want to do static and dynamic separation, just add the following configuration in Nginx OK.

#配置Nginx动静分离  
  
Location ~ *\. ( HTML|HTM|GIF|JPG|JPEG|BMP|PNG|ICO|TXT|JS|CSS) $  
  
{  
  
root/data/www;  
  
#expires定义用户浏览器缓存的时间为3天, if the static page is not often updated, can be set longer, this can save bandwidth and ease the pressure of the server  
  
expires   3d;  
  

This article comes from "Wu Guangko-Jing Feng Linux operation training" blog

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.