Nginx + tomcat cluster for load balancing

Source: Internet
Author: User
Tags nginx load balancing
Nginx + tomcat cluster for load Balancing
2012-09-21 14:15 1338 People read review (1) Favorite Report
nginxtomcat Cluster Server load Balancing file
1. Installing Nginx
The required Prce library

Tar zxvf pcre-8.10.tar.gz
CD pcre-8.10/
Make && make install
CD.. /
Installing Nginx
Tar zxvf nginx-1.0.2.tar.gz
CD nginx-1.0.2/
./configure--user=www--group=www--prefix=/usr/local/nginx--with-http_stub_status_module--with-http_ssl_ Module
Make && make install
CD.. /
Create nginx Log directory
chmod +w/home/nginx/logs
chown-r www:www/home/nginx/logs
Create nginx configuration file
①, create the nginx.conf file in the/usr/local/nginx/conf/directory:
Enter the following:
user www www;
worker_processes 8;
Error_log/home/nginx/logs/nginx_error.log Crit;
worker_rlimit_nofile 65535;
Use epoll;
worker_connections 65535;
include mime.types;
Default_type Application/octet-stream;
#charset gb2312;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
sendfile on;
Tcp_nopush on;
tcp_nodelay on;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
Gzip_min_length 1k;
gzip_buffers 4 16k;
Gzip_http_version 1.0;
Gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#limit_zone crawler$binary_remote_addr10m;
index index.htm index.html index.jsp;
Location ~. *$
index index.jsp;
access_log Logs/ combined;
error_page 404 =/404.html;
②, create the fcgi.conf file in the/usr/local/nginx/conf/directory:
Enter the following:
fastcgi_param gateway_interface cgi/1.1;
fastcgi_param server_software nginx;
Fastcgi_param query_string $query _string;
fastcgi_param request_method $request _method;
fastcgi_param content_type $content _type;
Fastcgi_param content_length $content _length;
Fastcgi_param script_filename $document _root$fastcgi_script_name;
fastcgi_param script_name $fastcgi _script_name;
fastcgi_param Request_uri $request _uri;
Fastcgi_param Document_uri $document _uri;
Fastcgi_param document_root $document _root;
fastcgi_param server_protocol $server _protocol;
Fastcgi_param remote_addr $remote _addr;
Fastcgi_param remote_port $remote _port;
Fastcgi_param server_addr $server _addr;
Fastcgi_param server_port $server _port;
Fastcgi_param server_name $server _name;
# PHP only, required if PHP is built with--enable-force-cgi-redirect
Fastcgi_param redirect_status;
start Nginx
Ulimit-shn 65535
Configure boot automatically start Nginx
Add the following at the end:
Ulimit-shn 65535
optimizing Linux Kernel parameters
Add the following at the end:
Net.ipv4.tcp_max_syn_backlog = 65536
Net.core.netdev_max_backlog = 32768
net.core.somaxconn = 32768
Net.core.wmem_default = 8388608
Net.core.rmem_default = 8388608
Net.core.rmem_max = 16777216
Net.core.wmem_max = 16777216
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_tw_recycle = 1
#net. Ipv4.tcp_tw_len = 1
net.ipv4.tcp_tw_reuse = 1
Net.ipv4.tcp_mem = 94500000 915000000 927000000
Net.ipv4.tcp_max_orphans = 3276800
#net. ipv4.tcp_fin_timeout =
#net. ipv4.tcp_keepalive_time =
Net.ipv4.ip_local_port_range = 1024x768 65535
make the configuration effective immediately:
change the Nginx configuration without stopping the Nginx service
After you modify the/usr/local/nginx/conf/nginx.conf configuration file, execute the following command to check that the configuration file is correct:
If the screen displays the following two lines of information, the configuration file is correct:
The configuration file/usr/local/nginx/conf/nginx.conf syntax is OK
The configuration file/usr/local/nginx/conf/nginx.conf was tested successfully
Restart nginx:
/usr/local/nginx/sbin/nginx-s Reload
write a script that periodically cuts Nginx logs every day
Create Script/usr/local/nginx/sbin/
Enter the following:
# This script run at 00:00
# The Nginx logs path
logs_root_path= "/home/nginx/logs/"
logs_path=${logs_root_path}$ (date-d "Yesterday" + "%Y")/$ (date-d "Yesterday" + "%m")/
mkdir-p ${logs_path}
#日志文件名 ====================================================
logs_name= "Weblogs"
cut_logs_file=${logs_path}${logs_name}_$ (date-d "Yesterday" + "%y%m%d"). Log
mv ${logs_file} ${cut_logs_file}
tar czf ${cut_logs_file}.tar.gz ${cut_logs_file}
rm-f ${cut_logs_file}
/usr/local/nginx/sbin/nginx-s Reload
Assign execute permissions to this script
# chmod +x/usr/local/nginx/sbin/
set crontab, cut nginx access log 00:00 every day
Enter the following:
XX * * * * /bin/bash/usr/local/nginx/sbin/
2. Configure Nginx Load Balancing
Edit nginx.conf
increase on server {;;
#ip_hash; Note: If you join Ip_hash, this can only be applied when a user connected to a server, after he logged on all the operations will only be in that one server will not jump to another server, if the server over the drop, it will automatically exit, Connect to another server
} is your own server, preferably with a domain name
upstream after the name to be the same name as Proxy_pass, suggest upstream behind the name, Proxy_pass behind the name, server_name after the name of the same
Example: {;;
index index.htm index.html index.jsp;
Location ~. *$
index index.jsp;
Proxy_pass; Note: Load balancing is done and the port cannot be added after this
access_log Logs/ combined;
error_page 404 =/404.html;
Note: If load balancing is unsuccessful, check that the nginx,80 port is not being forwarded directly to the port of the Tomcat service
3. Configuring the Tomcat cluster
optimize tomcat max concurrency, edit server.xml

maxthreads=" 2048 "minsparethreads=" M Axsparethreads= "$"
enablelookups= "false" redirectport= "8443" acceptcount= "

Modify Web. xml
add tag:
Just add it before you can, this is join Tom Cat session replication, do tomcat cluster must need this step, otherwise the user's session will not work properly.

Above describes the Nginx + Tomcat cluster load balancing, including aspects of the content, I hope the PHP tutorial interested in a friend helpful.

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