NGINX_TOMCAT 负载均衡配置
此配置已在生产环境中运行。
软件:
Nginx-1.0.8
下载地址: http://nginx.org/download/nginx-1.0.8.tar.gz
apache-tomcat-6.0.18
下载地址: http://archive.apache.org/dist/tomcat/tomcat-6/v6.0.18/bin/apache-tomcat-6.0.18.tar.gz
jdk-6u27-linux-i586.bin
下载地址: http://download.oracle.com/auth/otn-pub/java/jdk/6u27-b07/jdk-6u27-linux-i586.bin?e=131891933
Pcre-8.00
下载地址: http://nchc.dl.sourceforge.net/project/pcre/pcre/8.00/pcre-8.00.tar.gz
Nginx-upstream-jvm-route-0.2
下载地址: http://nginx-upstream-jvm-route.googlecode.com/files/nginx-upstream-jvm-route-0.2.tar.gz
 
配置环境:
Nginx服务器地址:192.168.1.20
Apache_tomcat1服务器:192.168.1.21
Apache_tomcat2服务器:192.168.1.22
 
一:软件安装步骤
将所有软件包下载到:/usr/src路径
配置nginx 服务器:
1:pcre-8.00编译安装
[root@mysql2 ~]# cd /usr/src/
[root@mysql2 src]# tar xf pcre-8.00.tar.gz
[root@mysql2 src]# cd pcre-8.00/
[root@mysql2 pcre-8.00]# ./configure ;make;make install
2:nginx-1.0.8编译安装
[root@mysql2 src]# tar xf nginx-upstream-jvm-route-0.1.tar.gz
[root@mysql2 src]# tar xf nginx-1.0.8.tar.gz
[root@mysql2 src]# cd nginx-1.0.8/
[root@mysql2 nginx-1.0.8]# patch -p0 < /usr/src/nginx_upstream_jvm_route/jvm_route.patch
[root@mysql2 nginx-1.0.8]# ./configure --prefix=/usr/local/nginx --with-pcre=/usr/src/pcre-8.00/ --with-http_stub_status_module --with-http_sub_module --add-module=/usr/src/nginx_upstream_jvm_route
3:修改Nginx 配置文件
[root@mysql2 ~]# cd /usr/local/nginx/conf/
[root@mysql2 conf]# cp nginx.conf nginx.conf.back
[root@mysql2 conf]# vi nginx.conf
user nginx nginx;
       worker_processes 8;
    
       error_log logs/error.log;
       #error_log logs/error.log notice;
       #error_log logs/error.log info;
    
       pid        logs/nginx.pid;
   
       worker_rlimit_nofile 65535;
   
      events {
          use epoll;
          worker_connections 10240;
      }
   
      http {
          include       mime.types;
          default_type application/octet-stream;
         
          #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;
          server_names_hash_bucket_size 128;
          client_header_buffer_size 32k;
          large_client_header_buffers 4 32k;
          client_max_body_size 8m;
          #keepalive_timeout 0;
          keepalive_timeout 60;
   
          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;
   
          server_tokens off;
                upstream   backend 
        {   
        server 192.168.1.21:8080 srun_id=tomcat1; 
        server 192.168.1.22:8080 srun_id=tomcat2; 
        jvm_route $cookie_JSESSIONID|sessionid reverse;
        }
 
          server {
              listen       192.168.1.20:80;
              server_name web.cl.com;
   
              charset utf-8;
   
              #access_log logs/host.access.log main;
   
              location / {
                  root   html;
                  index index.html index.htm index.jsp;
                  proxy_redirect off;
                  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://backend;
                     }
              location /status {   
                                                        stub_status   on;
                                                        access_log   on; 
                                                        auth_basic   "status"; 
                                                        auth_basic_user_file   /usr/local/nginx/htpasswd; 
                                                        }
   
              #error_page 404              /404.html;
   
              # redirect server error pages to the static page /50x.html
              #
              error_page   500 502 503 504 /50x.html;
              location = /50x.html {
                 root   html;
              }
   
              # proxy the PHP scripts to Apache listening on 127.0.0.1:80
              #
              #location ~ \.php$ {
              #    proxy_pass   http://127.0.0.1;
              #}
   
              # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
              #
              #location ~ \.php$ {
              #    root           html;
              #    fastcgi_pass   127.0.0.1:9000;
              #    fastcgi_index index.php;
              #    fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
              #    include        fastcgi_params;
              #}
   
              # deny access to .htaccess files, if Apache's document root
             # concurs with nginx's one
             #
             #location ~ /\.ht {
             #    deny all;
             #}
         }
  
  
         # another virtual host using mix of IP-, name-, and port-based configuration
         #
         #server {
         #    listen       8000;
         #    listen       somename:8080;
         #    server_name somename alias another.alias;
  
         #    location / {
         #        root   html;
         #        index index.html index.htm;
         #    }
         #}
  
         # HTTPS server
         #
         #server {
         #    listen       443;
         #    server_name localhost;
  
         #    ssl                  on;
         #    ssl_certificate      cert.pem;
         #    ssl_certificate_key cert.key;
 
         #    ssl_session_timeout 5m;
  
         #    ssl_protocols SSLv2 SSLv3 TLSv1;
         #    ssl_ciphers HIGH:!aNULL:!MD5;
         #    ssl_prefer_server_ciphers   on;
  
         #    location / {
         #        root   html;
         #        index index.html index.htm;
         #    }
                                          log_format   access   '$remote_addr - $remote_user [$time_local] "$request" ' 
                                          '$status $body_bytes_sent "$http_referer" ' 
                                          '"$http_user_agent" $http_x_forwarded_for'; 
                                          access_log   /var/log/access.log   access;  
 
         #}
  
     }
添加用户:
[root@mysql2 conf]# groupadd nginx
[root@mysql2 conf]# useradd -g nginx -s /sbin/nologin nginx
 
添加查看Nginx服务状态的用户:
[root@mysql2 ~]# yum -y install httpd        安装此服务需要借助一个命令
[root@mysql2 ~]# htpasswd -c /usr/local/nginx/htpasswd nginx
New password:
Re-type new password:
 
检查配置文件:
[root@mysql2 conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
看到以上两个结果就算配置是正确的。
 
启动服务
[root@mysql2 conf]# /usr/local/nginx/sbin/nginx
 
停止服务:
[root@mysql2 conf]# /usr/local/nginx/sbin/nginx –s stop
 
也可以使用这个启动和停止的脚本:
[root@mysql2 conf]# vi /etc/init.d/nginx
#!/bin/sh  
#  
# description: Starts, stops nginx  
#  
#chkconfig: 2345 20 80 
#dscription: Startup script for nginx webserver on CentOS. Place in /etc/init.d   
#  
# Author: Touya  
set -e  
 
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/nginx  
DESC="nginx daemon" 
NAME=nginx  
DAEMON=/usr/local/nginx/sbin/$NAME   (定义nginx的安装目录)
CONFIGFILE=/usr/local/nginx/conf/nginx.conf  
PIDFILE=/usr/local/nginx/logs/$NAME.pid  
SCRIPTNAME=/etc/init.d/$NAME
 
# Gracefully exit if the package has been removed.  
test -x $DAEMON || exit 0 
 
d_start() {  
echo "Starting $DESC: $NAME" 
$DAEMON -c $CONFIGFILE || echo "already running" 
}  
 
d_stop() {  
echo "Stopping $DESC: $NAME" 
test -f $PIDFILE && kill -QUIT `cat $PIDFILE`  
}  
 
d_reload() {  
echo "Reloading $DESC configuration…" 
kill -HUP `cat $PIDFILE` || echo "can’t reload" 
}  
case "$1" in  
'start')  
    d_start  
    echo "started." 
;;  
'stop')  
    d_stop  
    echo "stoped." 
;;  
'reload')  
    d_reload  
    echo "reloaded." 
;;  
'restart')  
    echo "Restarting $DESC: $NAME ..." 
    d_stop  
    # One second might not be time enough for a daemon to stop,  
    # if this happens, d_start will fail (and dpkg will break if  
    # the package is being upgraded). Change the timeout if needed  
    # be, or change d_stop to have start-stop-daemon use --retry.  
    # Notice that using --retry slows down the shutdown process somewhat.  
    sleep 3 
    d_start  
    echo "done." 
;;  
'list')  
    ps auxf | egrep '(PID|nginx)' | grep -v grep  
;;  
'test')  
    $DAEMON -t -c $CONFIGFILE  
;;  
*)  
echo "Usage: $SCRIPTNAME {reload|list|test|start|stop|restart}" >&2 
exit 3 
;;  
esac  
exit 0 
 
[root@mysql2 conf]# chmod 755 /etc/init.d/nginx
[root@mysql2 conf]# /etc/init.d/nginx start
[root@mysql2 conf]# /etc/init.d/nginx stop
[root@mysql2 conf]# chkconfig --add nginx       #开机自启动
 
检查服务器端口是否启动:
[root@mysql2 conf]# lsof –i:80 
 
查看服务状态:
http://192.168.1.20/nginx
 
Apache-tomcat服务器配置
1:jdk-6u27-linux-i586.bin安装
[root@web usr]# cd /usr/src/
[root@web src]# ./jdk-6u27-linux-i586.bin
[root@web src]# mv jdk1.6.0_27/ /usr/local/
[root@web src]# vi /etc/profile
JAVA_HOME=/usr/local/ jdk1.6.0_27
CLASSPATH=".:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar"
PATH=$JAVA_HOME/bin:$PATH
TOMCAT_HOME=/usr/local/tomcat
export JAVA_HOME CLASSPATH PATH TOMCAT_HOME
 
使配置生效:
[root@web src]# source /etc/profile
 
查看当前java版本号:
[root@web src]# java -version
java version "1.6.0_27"
 
2:apache-tomcat-6.0.18安装
[root@web src]# tar xf apache-tomcat-6.0.18.tar.gz
[root@web src]# mv apache-tomcat-6.0.18 /usr/local/tomcat1
[root@web tomcat]# cd /usr/local/tomcat/conf/
 
修改配置文件:
[root@web conf]# vi server.xml
 
 
[root@web conf]# vi tomcat-users.xml
 
 
 
 
[root@web conf]# cd /usr/local/tomcat/bin/
[root@web bin]# vi catalina.sh
74  JAVA_HOME=/usr/local/ jdk1.6.0_27
JAVA_OPTS="-server -Xms256m -Xmx512m"
 
启动服务:
[root@web bin]# /usr/local/tomcat/bin/startup.sh
关闭服务:
[root@web bin]# /usr/local/tomcat/bin/shutdown.sh
检查服务端口:
[root@web bin]# lsof -i:8080
COMMAND PID USER    FD   TYPE DEVICE SIZE NODE NAME
java     6414 root   29u IPv4 24953       TCP *:webcache (LISTEN)
 
在目录下建立测试页
[root@web ~]# mkdir /usr/local/tomcat/webapps/opps
[root@web ~]# vi /usr/local/tomcat/webapps/opps/mem.jsp
<%
Runtime lRuntime = Runtime.getRuntime();
out.println("*** BEGIN MEMORY STATISTICS ***
");
out.println("Free Memory: "+lRuntime.freeMemory()+"
");
out.println("Max    Memory: "+lRuntime.maxMemory()+"
");
out.println("Total Memory: "+lRuntime.totalMemory()+"
");
out.println("Available Processors : "+lRuntime.availableProcessors()+"
");
out.println("*** END MEMORY STATISTICS ***");
%>
 
 
访问测试:
http://192.168.1.20/mem.jsp
 
能访问,证明配置成功。OK
 
在TOMCAT另一台服务器的配置步骤重复第一台的步骤就好了,在这里就不写出来了。
 
系统内核优化参数:(在三台上面添加)
[root@web bin]# /etc/sysctl.conf
# Use TCP syncookies when needed
net.ipv4.tcp_syncookies = 1
# Enable TCP window scaling
net.ipv4.tcp_window_scaling: = 1
# Increase TCP max buffer size
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
# Increase Linux autotuning TCP buffer limits
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
# Increase number of ports available
net.ipv4.ip_local_port_range = 1024 65000
 
使配置立即生效:
[root@web bin]# /sbin/sysctl -p