红联Linux门户
Linux帮助

CentOS 6.5系统下Nginx反向代理实现Tomcat负载均衡

发布时间:2015-03-27 21:59:23来源:linux网站作者:taokey_linux

1.查看当前系统的内核和系统参数以及版本。

[root@node1 ~]# uname -a
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.安装nginx。
1)安装gcc编译器及相关工具和依赖库。
[root@node1 ~]# yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers pcre

2)编译安装pcre库。
PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正规表达式。Pcre的作用主要是使nginx支持HTTP rewrite模块。
创建一个指定放置压缩包的目录。
[root@node1 ~]# mkdir -p /linux/tools
[root@node1 ~]# cd /linux/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

注意:安装成功启动nginx,有可能会遇到下面错误,
[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

解决办法:
[root@node1 ~]# cd /lib64/
[root@node1 lib64]# ln -s libpcre.so.0.0.1 libpcre.so.1

3)此事再启动nginx,查看下进程和端口。
[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  89911  0 16:43 ?00:00:00 nginx: master process /data/nginx/sbin/nginx
nobody8992  8991  0 16:43 ?00:00:00 nginx: worker process
root  8994  1907  0 16:44 pts/100:00:00 grep nginx
[root@node1 lib64]# netstat -anpt | grep nginx
tcp0  0 0.0.0.0:80  0.0.0.0:*  LISTEN  8991/nginx


3.配置nginx web反向代理,实现两个tomcat负载均衡:
nginx配置文件如下:
[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_nopushon;
keepalive_timeout  60;
server_tokens  off;
limit_rate_after 3m;
limit_rate 512k;
tcp_nodelay on;
client_header_buffer_size 256k;
large_client_header_buffers 4 256k;
 
# Define nginx proxy module
proxy_http_version 1.1;
proxy_connect_timeout 60;
proxy_read_timeout 60;
proxy_send_timeout 60;
proxy_buffer_size  16k;
proxy_buffers  4 64k;
proxy_busy_buffers_size128k;
proxy_temp_file_write_size 128k;
proxy_headers_hash_max_size 51200;
proxy_headers_hash_bucket_size 6400;
 
# Define Gzip compression module
gzip on;
gzip_vary on;
gzip_min_length  1k;
gzip_buffers4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types  text/plain application/x-javascript text/css application/xml;
 
# Define realserver pool
upstream  linux {
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  linux
 
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;
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 504  /50x.html;
location = /50x.html {
root  html;
}
}
}