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
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.10.tar.gz

Tar zxvf pcre-8.10.tar.gz
CD pcre-8.10/
./configure
Make && make install
CD.. /
Installing Nginx
wgetHttp://nginx.org/download/nginx-0.8.54.tar.gz
wgetHttp://nginx.org/download/nginx-1.0.2.tar.gz
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
mkdir-p/home/nginx/logs
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:
rm-f/usr/local/nginx/conf/nginx.conf
vi/usr/local/nginx/conf/nginx.conf
Enter the following:
user www www;
worker_processes 8;
Error_log/home/nginx/logs/nginx_error.log Crit;
Pid/usr/local/nginx/nginx.pid;
worker_rlimit_nofile 65535;
Events
{
Use epoll;
worker_connections 65535;
}
http
{
include mime.types;
Default_type Application/octet-stream;
#charset gb2312;
server_names_hash_bucket_size;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
sendfile on;
Tcp_nopush on;
keepalive_timeout;
tcp_nodelay on;
fastcgi_connect_timeout;
fastcgi_send_timeout;
fastcgi_read_timeout;
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;
#测试
Server
{
Listen;
server_namewww.test.com;
index index.htm index.html index.jsp;
root/home/htdocs/web/root/;
Location ~. *$
{
index index.jsp;
Proxy_passhttp://www.test.com:8080;
}
access_log Logs/sp.imichat.com.log combined;
error_page 404 =/404.html;
}
}
②, create the fcgi.conf file in the/usr/local/nginx/conf/directory:
vi/usr/local/nginx/conf/fcgi.conf
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
/usr/local/nginx/sbin/nginx
Configure boot automatically start Nginx
vi/etc/rc.local
Add the following at the end:
Ulimit-shn 65535
/usr/local/nginx/sbin/nginx
optimizing Linux Kernel parameters
vi/etc/sysctl.conf
Add the following at the end:
# ADD
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:
/sbin/sysctl-p
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:
/usr/local/nginx/sbin/nginx-t
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/cut_nginx_log.sh
vi/usr/local/nginx/sbin/cut_nginx_log.sh
Enter the following:
#!/bin/bash
# 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"
Logs_file=${logs_root_path}${logs_name}.log
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}
#重启nginx
/usr/local/nginx/sbin/nginx-s Reload
Assign execute permissions to this script
# chmod +x/usr/local/nginx/sbin/cut_nginx_log.sh
set crontab, cut nginx access log 00:00 every day
crontab-e
Enter the following:
XX * * * * /bin/bash/usr/local/nginx/sbin/cut_nginx_log.sh
2. Configure Nginx Load Balancing
Edit nginx.conf
vim/usr/local/nginx/conf/nginx.conf
increase on server
UpstreamWww.test.com {
Serverwww.test.com:8888;
Serverwww.test.com:8080;
#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
}
Www.test.com 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:
UpstreamWww.test.com {
Serverwww.test.com:8888;
Serverwww.test.com:8080;
}
#测试
Server
{
Listen;
server_namewww.test.com;
index index.htm index.html index.jsp;
root/home/htdocs/web/root/;
Location ~. *$
{
index index.jsp;
Proxy_passhttp://www.test.com; Note: Load balancing is done and the port cannot be added after this
}
access_log Logs/sp.imichat.com.log 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= "
c/>


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