linux Nginx+Tomcat load balancing configuration method

  • 2020-05-12 06:51:37
  • OfStack

Nginx+tomcat is the current mainstream java web architecture, how to make nginx+tomcat work at the same time, or how to use nginx to reverse proxy tomcat backend equilibrium? The direct installation configuration is as follows:

1. Installation of JAVA JDK:


# Download the corresponding jdk The package is then unpacked and installed, and 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/  Under the .  
  
# Then configure the environment variables so they can be referenced anywhere jdk , as follows:   
  
#vi /etc/profile  Add the following statement at the end:   
  
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  # Puts environment variables into effect immediately   
  
#java --version  # To view java Version, see jdk1.7.0_25 Version is representative java jdk Installation was successful.  

2. Installation of Nginx:


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 Once installed, use the command :/usr/local/nginx/sbin/nginx -t  test OK On behalf of nginx Installation was successful.   
  
/usr/local/nginx/sbin/nginx  Press enter to start nginx Can be accessed http://ip/ see nginx Default page.  

3. Installation of Tomcat:


# Official website download tomcat 6.0.30 Or another version:   
  
cd /usr/src && tar xzf apache-tomcat-6.0.30.tar.gz  
  
# Directly unzip can be used, unzip complete execution, copy two at the same time tomcat , named tomcat1 tomcat2  
  
mv apache-tomcat-6.0.30 /usr/local/tomcat1 
  
cp /usr/local/tomcat1 /usr/local/tomcat2 -r  
  
# Modified respectively tomcat1 and tomcat2  Port, we have it here 3 Three ports need to be modified, as follows:   
  
shutdown  Port: 8005  Mainly responsible for starting and closing .  
  
ajp Port: 8009  Mainly responsible for passing ajp Equilibrium n apache and tomcat Integration)   
  
http Port: 8080  Can be achieved by web Direct page access ( nginx+tomcata Integration)   
  
# note *  if tomcat13 The ports are: 8005 8009 8080 , then tomcat2 The ports are based on this +1 To: 8006 8010 8081  
  
#1 The port cannot be repeated on the server, otherwise an error will be reported.   
  
# After modifying the ports, start both tomcat , the startup command is:   
  
# How to prompt no file or insufficient permissions required tomcat  the bin Directory of sh File gives execution permission: chmod o+x  *.sh  
  
/usr/local/tomcat1/bin/startup.sh  
  
/usr/local/tomcat2/bin/startup.sh  
  
# After startup, use netstat -tnl  You can see 6 Port, that is tomcat1 tomcat2 Successful startup. You can use http://ip:8080 http://ip:8081 access tomcat Default page.  

If you need to modify the tomcat publishing directory to create your own directory, you need to make the following adjustments to create two publishing directories:


mkdir -p /usr/webapps/{www1,www2}

Edit vi usr/local tomcat1 / conf/server xml at the bottom < /Host > Add the following content to the first line:

<Context path="" docBase="/usr/webapps/www1" reloadable="false"/>

2. Edit vi usr/local tomcat2 / conf/server xml at the bottom < /Host > Add the following content to the first line:

<Context path="" docBase="/usr/webapps/www2" reloadable="false"/>

3. Contents of tomcat1 publishing directory:


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

4. Contents of tomcat2 published directory:


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

5. Integration of Nginx+tomcat:

The integration is mainly to modify the nginx.conf configuration and give a complete nginx.conf online configuration. Some 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_bytes_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;  
  # Release 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;  
  }  
   
 }  
  
} 

web_app defined in server paragraph needs to correspond to web_app1 defined in upstream, otherwise server cannot find an equilibrium.

The above configuration, nginx+tomcat reverse agent load balancing configuration is completed, if you want to do static and static separation, you only need to add the following configuration in nginx to OK.


# configuration Nginx Dynamic and static separation   
  
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$  
  
{  
  
root /data/www;  
  
#expires The time to define the user's browser cache is 3 Day, if the static page is not often updated, can be set longer, which can save bandwidth and relieve the pressure on the server   
  
expires   3d;  
  
} 

This article is from the blog "wu guangke - jingfeng Linux operation and maintenance training"


Related articles: