Tomcat Load Balancing using Nginx reverse proxy in CentOS 6.5
Tomcat Load Balancing using Nginx reverse proxy in CentOS 6.5
1. view the kernel, system parameters, and version of the current system.
[Root @ node1 ~] # Uname-
Linux node1 2.6.32-431. el6.x86 _ 64 #1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
[Root @ node1 ~] # Cat/etc/issue
CentOS release 6.5 (Final)
Kernel \ r on an \ m
2. Install nginx.
1) install the gcc compiler and related tools and dependent libraries.
[Root @ node1 ~] # Yum-y install gcc-c ++ autoconf libjpeg-devel libpng-devel freetype-devel libxml2 libxml2-devel zlib-devel glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses -devel curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn-devel openssl-devel openldap-devel nss_ldap openldap-clients openldap-servers pcre
2) Compile and install the pcre Library.
PCRE (Perl Compatible Regular Expressions) is a Perl library that includes Regular Expressions Compatible with perl. Pcre is mainly used to enable nginx to support the HTTP rewrite module.
Create a directory for storing compressed packages.
[Root @ node1 ~] # Mkdir-p/bkjia/tools
[Root @ node1 ~] # Cd/bkjia/tools/
[Root @ node1 tools] # tar-zxf pcre-8.33.tar.gz
[Root @ node1 pcre-8.33] #./configure
[Root @ node1 pcre-8.33] # make & make install
[Root @ node1 nginx-1.5.8] #./configure -- with-http_stub_status_module -- with-http_ssl_module -- prefix =/data/nginx
[Root @ node1 nginx-1.5.8] # make & make install
NOTE: If nginx is successfully started after installation, the following error may occur,
[Root @ node1 nginx-1.5.8] #/data/nginx/sbin/nginx-t
/Data/nginx/sbin/nginx: error while loading shared libraries: libpcre. so.1: cannot open shared object file: No such file or directory
Solution:
[Root @ node1 ~] # Cd/lib64/
[Root @ node1 lib64] # ln-s libpcre. so.0.0.1 libpcre. so.1
3) Start nginx and check the processes and ports.
[Root @ node1 lib64] #/data/nginx/sbin/nginx-t
Nginx: the configuration file/data/nginx/conf/nginx. conf syntax is OK
Nginx: configuration file/data/nginx/conf/nginx. conf test is successful
[Root @ node1 lib64] #/data/nginx/sbin/nginx
[Root @ node1 lib64] # ps-ef | grep nginx
Root 8991 1 0? 00:00:00 nginx: master process/data/nginx/sbin/nginx
Nobody 8992 8991 0? 00:00:00 nginx: worker process
Root 8994 1907 0 00:00:00 pts/1 grep nginx
[Root @ node1 lib64] # netstat-anpt | grep nginx
Tcp 0 0 0.0.0.0: 80 0.0.0.0: * LISTEN 8991/nginx
3. Configure nginx web reverse proxy to achieve two tomcat load balancing:
The nginx configuration file is as follows:
[Root @ node1 ~] # Cat/data/nginx/conf/nginx. conf
User root;
Worker_processes 1;
# Error_log logs/error. log info;
Pid/data/nginx/logs/nginx. pid;
Worker_rlimit_nofile 65535;
Events {
Use epoll;
Worker_connections 65535;
Multi_accept on;
}
Http {
Include mime. types;
Default_type application/octet-stream;
Charset UTF-8;
Log_format main '$ remote_addr-$ remote_user [$ time_local] "$ request "'
'$ Status $ body_bytes_sent "$ http_referer "'
'"$ Http_user_agent" "$ http_x_forwarded_for "';
Access_log logs/access. log main;
Sendfile on;
Tcp_nopush on;
Keepalive_timeout 60;
Server_tokens off;
Limit_rate_after 3 m;
Limit_rate 512 k;
Tcp_nodelay on;
Client_header_buffer_size 256 k;
Large_client_header_buffers 4 256 k;
# Define nginx proxy module
Proxy_http_version 1.1;
Proxy_connect_timeout 60;
Proxy_read_timeout 60;
Proxy_send_timeout 60;
Proxy_buffer_size 16 k;
Proxy_buffers 4 64 k;
Proxy_busy_buffers_size 128 k;
Proxy_temp_file_write_size 128 k;
Proxy_headers_hash_max_size 51200;
Proxy_headers_hash_bucket_size 6400;
# Define Gzip compression module
Gzip on;
Gzip_vary on;
Gzip_min_length 1 k;
Gzip_buffers 4 16 k;
Gzip_http_version 1.0;
Gzip_comp_level 2;
Gzip_types text/plain application/x-javascript text/css application/xml;
# Define realserver pool
Upstream bkjia.com {
Ip_hash;
Server 192.168.1.15: 8080 max_fails = 0 weight = 5;
Server 192.168.1.19: 8080 max_fails = 0 weight = 5;
}
Server {
Listen 80;
Server_name bkjia.net www.bkjia.net;
Location /{
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://bkjia.com;
Expires 1d;
Access_log logs/host. access. log main;
}
# Define 404 502 503 504 error page
Error_page 404/404 .html;
Error_page 500 502 503 x.html;
Location =/50x.html {
Root html;
}
}
}
Load Balancing between Apache and multiple Tomcat clusters in Linux
Nginx Tomcat Cluster load balancing solution notes
Instance details Tomcat component installation + Nginx reverse proxy Tomcat + Apache use mod_jk and mod_proxy Reverse Proxy and load balancing
Deploy a log server using Rsyslog + LogAnalyzer + MySQL in CentOS 6.5
Build an Apache + Tomcat environment (JK deployment process)
Tomcat details: click here
Tomcat: click here
This article permanently updates the link address: