Nginx + Tomcat server Load balancer Configuration

Source: Internet
Author: User

Nginx + Tomcat server Load balancer Configuration
GuideNginx + tomcat is the mainstream Java Web architecture of Internet companies. nginx + tomcat can work stably at the same time. How can I use nginx to reverse proxy tomcat backend balancing? Directly install the configuration as follows:Install JAVA JDK

# Download the appropriate jdk software package, and then unzip the installation, here I package name: 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/

# Configure the environment variable so that jdk can be referenced anywhere. The configuration is as follows:
# Add the following statement to the end of vi/etc/profile:

export JAVA_HOME=/usr/java/jdk1.7.0_25export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/libexport PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH:$HOMR/bin

# Source/etc/profile # make the environment variable take effect immediately
# Java -- version # Check the java version. If jdk 1.7.0 _ 25 is displayed, the java jdk is successfully installed.

Nginx Installation
wget http://nginx.org/download/nginx-1.2.6.tar.gz useradd wwwtar zxvf nginx-1.2.6.tar.gzcd nginx-1.2.6/./configure --user=www --group=www --prefix=/usr/local/nginx \--with-http_stub_status_module --with-http_ssl_modulemake && make install

# After Nginx is installed, run the command:/usr/local/nginx/sbin/nginx-t to test OK, which indicates that nginx is successfully installed.
/Usr/local/nginx/sbin/nginx press enter to start nginx. You can access http: // ip/to view the default nginx page.

Tomcat installation

# Download tomcat 6.0.30 or another version from the official website:

cd /usr/src  && tar xzf  apache-tomcat-6.0.30.tar.gz

# Directly decompress the package and run the package. Copy two tomcat servers and name them 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. The three ports must be modified as follows:
Shutdown port: 8005 is mainly used to start and close.
Ajp port: 8009 is mainly used for ajp balancing (usually used for apache and tomcat integration)
Http port: 8080 can be directly accessed through web pages (nginx + tomcata integration)
# Note * If the three port numbers of tomcat1 are 8005 8009 8080, port 8006 + 1, that is, port 8010 8081
# On a server, the ports cannot be repeated; otherwise, an error is reported.
# After modifying the port, start two tomcat servers and run the following command:
# How to prompt whether the file does not exist or the permission is insufficient? The bin directory of tomcat needs to grant the execution permission to the sh file: chmod

o+x   *.sh /usr/local/tomcat1/bin/startup.sh/usr/local/tomcat2/bin/startup.sh

# After the instance is started, use netstat-tnl to view the six ports, that is, tomcat1 tomcat2 is successfully started. You can use http: // ip: 8080 http: // ip: 8081 to access the default tomcat page.

Customize directory Publishing

Edit vi/usr/local/tomcat1/conf/server. xml and add the following content to the last line:

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

2. Edit vi/usr/local/tomcat2/conf/server. xml and add the following content to the last line:

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

3. The release directory of tomcat1 is as follows:

4. tomcat2 release directory content:

Nginx + tomcat Integration
User www; worker_processes 8; pid/usr/local/nginx. pid; worker_rlimit_nofile 102400; events {use epoll; worker_connections 102400;} http {include mime. types; default_type application/octet-stream; requests on; charset UTF-8; Limit 128; limit 4 k; limit 4 32 k; client_max_body_size 300 m; sendfile on; tcp_nopush on; keepalive_timeout 60; tcp_nodelay on; Limit 512 k; proxy_connect_timeout 5; proxy_read_timeout 60; limit 5; Limit 16 k; proxy_buffers 4 64 k; Limit 128 k; Limit 128 k; gzip on; gzip_min_length 1 k; gzip_buffers 4 16 k; 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 logslog_format main '$ http_x_forwarded_for-$ remote_user [$ time_local] "$ request" ''$ status $ response" $ 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 = 30 s; server 127.0.0.1: 8081 weight = 1 max_fails = 2 fail_timeout = 30 s; }### chinaapp. sinaapp. comserver {listen 80; server_name chinaapp.sinaapp.com; 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 ;}}}

# Note * The web_app defined in the server segment proxy_pass must be consistent with the web_app defined in upstream. Otherwise, the server cannot find a balance.
# In the preceding configuration, the nginx + tomcat Reverse Proxy Server Load balancer configuration is complete. To separate Dynamic and Static traffic, you only need to add the following configuration in nginx.

# Configure Nginx dynamic/static Separation

Location ~ . *\. (Html | htm | gif | jpg | jpeg | bmp | png | ico | txt | js | css) $ {root/data/www; # expires defines that the user's browser cache time is three days. If the static page is not updated frequently, you can set a longer period to save bandwidth and relieve the pressure on the server. expires 3d ;}

From: http://wgkgood.blog.51cto.com/1192594/1529618

Address: http://www.linuxprobe.com/centos-directory-color/


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.