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

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

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 /linuxidc/tools 
[root@node1 ~]# cd /linuxidc/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      8991    1  0 16:43 ?        00:00:00 nginx: master process /data/nginx/sbin/nginx
nobody    8992  8991  0 16:43 ?        00:00:00 nginx: worker process  
root      8994  1907  0 16:44 pts/1    00:00:00 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.配置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_nopush    on; 
    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_size    128k; 
        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_buffers    4 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  linuxidc.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  linuxidc.net www.linuxidc.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://linuxidc.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 504  /50x.html; 
        location = /50x.html { 
            root  html; 
        } 
    } 
}

Linux下Apache与多个Tomcat 集群负载均衡 http://www.linuxidc.com/Linux/2012-01/51731.htm

Nginx Tomcat 集群负载均衡解决笔记 http://www.linuxidc.com/Linux/2013-07/86827.htm

实例详解Tomcat组件安装+Nginx反向代理Tomcat+Apache使用mod_jk和mod_proxy反向代理和负载均衡 http://www.linuxidc.com/Linux/2013-06/85290.htm

CentOS 6.5下利用Rsyslog+LogAnalyzer+MySQL部署日志服务器 http://www.linuxidc.com/Linux/2014-06/103836.htm

Apache+Tomcat 环境搭建(JK部署过程) http://www.linuxidc.com/Linux/2012-11/74474.htm

猜你喜欢

转载自zengshaotao.iteye.com/blog/2376780