Nginx reverse proxy under CentOS 6.5 system to achieve Tomcat load balancing

Nginx reverse proxy under CentOS  6.5 system to achieve Tomcat load balancing

1. View the kernel and system parameters and version of the current system.

[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) Compile and install the pcre library.
PCRE (Perl Compatible Regular Expressions) is a Perl library that includes Perl Compatible Regular Expressions. The role of Pcre is mainly to enable nginx to support the HTTP rewrite module.
Create a directory that specifies where to put the tarball.
[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

Note: The installation is successful and nginx is started, you may encounter the following error,
[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)此事再启动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. 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 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; 
        } 
    } 
}

Load balancing between Apache and multiple Tomcat clusters under Linuxhttp  ://www.linuxidc.com/Linux/2012-01/51731.htm

Nginx Tomcat Cluster Load Balancing Solution Noteshttp  ://www.linuxidc.com/Linux/2013-07/86827.htm

Example detailed Tomcat component installation + Nginx reverse proxy Tomcat + Apache uses mod_jk and mod_proxy reverse proxy and load balancinghttp  ://www.linuxidc.com/Linux/2013-06/85290.htm

Using Rsyslog+LogAnalyzer+MySQL to Deploy Log Server under CentOS 6.5http:  //www.linuxidc.com/Linux/2014-06/103836.htm

Apache+Tomcat environment setup (JK deployment process)  http://www.linuxidc.com/Linux/2012-11/74474.htm

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326326659&siteId=291194637