天天看點

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編譯安裝

[[email protected] ~]#cd /usr/src/

[[email protected] src]#tar xf pcre-8.00.tar.gz

[[email protected] src]#cd pcre-8.00/

[[email protected]]# ./configure ;make;make install

2:nginx-1.0.8編譯安裝

[[email protected] src]#tar xf nginx-upstream-jvm-route-0.1.tar.gz

[[email protected] src]#tar xf nginx-1.0.8.tar.gz

[[email protected] src]#cd nginx-1.0.8/

[[email protected]]#patch  -p0 </usr/src/nginx_upstream_jvm_route/jvm_route.patch

[[email protected]]# ./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

# --with-pcre路徑是指向pcre的源碼檔案目錄

3:修改Nginx配置檔案

[[email protected] ~]#cd /usr/local/nginx/conf/

[[email protected]]# cpnginx.confnginx.conf.back

[[email protected]]# vinginx.conf

user nginxnginx;

worker_processes2;以下是以伺服器上有多少個cpu定的數字。(2cpu就定為2)

worker_cpu_affinity 0010 0100;cpu的負載配置設定      

error_log  logs/error.log;

       #error_log  logs/error.log  notice;

       #error_log  logs/error.log  info;

pid        logs/nginx.pid;

worker_rlimit_nofile65535;

events {

useepoll;

worker_connections  10240;

      }

http {

includemime.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_size128;

client_header_buffer_size32k;

large_client_header_buffers4 32k;

client_max_body_size8m;

          #keepalive_timeout  0;

keepalive_timeout  60;

gzip  on;

gzip_min_length1k;

gzip_buffers 416k;

gzip_http_version1.0;

gzip_comp_level 2;

gzip_types   text/plain application/x-javascript text/cssapplication/xml;

gzip_vary on;

server_tokens off;

upstream   backend 

        {   

server192.168.1.21:8080 srun_id=tomcat1; 

server192.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;

charsetutf-8;

              #access_log  logs/host.access.log  main;

location / {

root   html;

index  index.html index.htm index.jsp;

proxy_redirectoff;

proxy_set_headerHost $host;

proxy_set_headerX-Real-IP $remote_addr;

proxy_set_headerX-Forwarded-For $proxy_add_x_forwarded_for;

proxy_passhttp://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 tothe static page /50x.html

              #

error_page   500 502 503 504  /50x.html;

location =/50x.html {

root   html;

              }

              # proxy the PHP scripts to Apachelistening on 127.0.0.1:80

              #

              #location ~ \.php$ {

              #    proxy_pass  http://127.0.0.1;

              #}

              # pass the PHP scripts to FastCGIserver listening on 127.0.0.1:9000

              #

              #location ~ \.php$ {

              #    root           html;

              #    fastcgi_pass   127.0.0.1:9000;

              #    fastcgi_indexindex.php;

              #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

              #    includefastcgi_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 ofIP-, name-, and port-based configuration

         #

         #server {

         #   listen       8000;

         #   listen       somename:8080;

         #   server_namesomename  alias  another.alias;

         #   location / {

         #        root  html;

         #        index index.html index.htm;

         #   }

         #}

         # HTTPS server

         #

         #server {

         #   listen       443;

         #   server_namelocalhost;

         #   ssl                  on;

         #   ssl_certificatecert.pem;

         #   ssl_certificate_keycert.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;  

         #}

     }

添加使用者:

[[email protected]]# groupaddnginx

[[email protected]]# useradd -g nginx -s /sbin/nologinnginx

添加檢視Nginx服務狀态的使用者:

[[email protected] ~]# yum -y install httpd安裝此服務需要借助一個指令

[[email protected] ~]#htpasswd -c /usr/local/nginx/htpasswdnginx

New password:

Re-type newpassword:

檢查配置檔案:

[[email protected] conf]# /usr/local/nginx/sbin/nginx -t

nginx: theconfiguration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx:configuration file /usr/local/nginx/conf/nginx.conf test is successful

看到以上兩個結果就算配置是正确的。

啟動服務:

[[email protected]]# /usr/local/nginx/sbin/nginx

停止服務:

[[email protected]]# /usr/local/nginx/sbin/nginx–s stop

也可以使用這個啟動和停止的腳本:

[[email protected] conf]#vi /etc/init.d/nginx

#!/bin/sh  

#  

# description: Starts, stopsnginx  

#  

#chkconfig: 2345 20 80 

#dscription: Startup scriptfor 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="nginxdaemon" 

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 tostop,  

    # if this happens, d_start will fail (and dpkg will breakif  

    # the package is being upgraded). Change the timeout ifneeded  

    # be, or change d_stop to have start-stop-daemon use--retry.  

    # Notice that using --retry slows down the shutdown processsomewhat.  

    sleep 3 

    d_start  

    echo "done." 

;;  

'list')  

    psauxf | 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 

[[email protected]]#chmod 755 /etc/init.d/nginx

[[email protected]]# /etc/init.d/nginx start

[[email protected]]# /etc/init.d/nginx stop

[[email protected]]#chkconfig --add nginx      #開機自啟動

檢查伺服器端口是否啟動:

[[email protected] conf]#lsof–i:80 

檢視服務狀态:

http://192.168.1.20/nginx

Apache-tomcat伺服器配置

1:jdk-6u27-linux-i586.bin安裝

[[email protected]]# cd /usr/src/

[[email protected]]# ./jdk-6u27-linux-i586.bin

[[email protected]]# mvjdk1.6.0_27//usr/local/

[[email protected]]# 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

使配置生效:

[[email protected]]# source /etc/profile

檢視目前java版本号:

[[email protected]]# java -version

java version "1.6.0_27"

2:apache-tomcat-6.0.18安裝

[[email protected]]#tar xf apache-tomcat-6.0.18.tar.gz

[[email protected]]# mv apache-tomcat-6.0.18/usr/local/tomcat1

[[email protected] tomcat]# cd/usr/local/tomcat/conf/

修改配置檔案:

[[email protected]]#vi server.xml

<Engine name="Catalina"defaultHost="localhost" jvmRoute="tomcat1">

<Connector port="8009"maxThreads="500" minSpareThreads="30"maxSpareThreads="70" acceptCount="100"connectionTimeout="60000"protocol="AJP/1.3"redirectPort="8443" />

[[email protected]]# vi tomcat-users.xml

<?xml version='1.0'encoding='utf-8'?>

<tomcat-users>

<role rolename="manager"/>

<role rolename="admin"/>

<user username="admin"password="admin" roles="admin,manager"/>

</tomcat-users>

[[email protected]]# cd /usr/local/tomcat/bin/

[[email protected] bin]# vi catalina.sh

74 JAVA_HOME=/usr/local/ jdk1.6.0_27

JAVA_OPTS="-server-Xms256m -Xmx512m"

啟動服務:

[[email protected] bin]#/usr/local/tomcat/bin/startup.sh

關閉服務:

[[email protected] bin]#/usr/local/tomcat/bin/shutdown.sh

檢查服務端口:

[[email protected] bin]# lsof -i:8080

COMMAND PID USER   FD   TYPE DEVICE SIZE NODE NAME

java   6414 root   29u  IPv4 24953       TCP *:webcache (LISTEN)

在目錄下建立測試頁

[[email protected] ~]# mkdir/usr/local/tomcat/webapps/opps

[[email protected] ~]# vi/usr/local/tomcat/webapps/opps/mem.jsp

<%

Runtime lRuntime = Runtime.getRuntime();

out.println("*** BEGINMEMORY STATISTICS ***<br/>");

out.println("Free Memory:"+lRuntime.freeMemory()+"<br/>");

out.println("Max   Memory:"+lRuntime.maxMemory()+"<br/>");

out.println("TotalMemory: "+lRuntime.totalMemory()+"<br/>");

out.println("AvailableProcessors : "+lRuntime.availableProcessors()+"<br/>");

out.println("*** ENDMEMORY STATISTICS ***");

%>

通路測試:

http://192.168.1.20/mem.jsp

能通路,證明配置成功。OK

在TOMCAT另一台伺服器的配置步驟重複第一台的步驟就好了,在這裡就不寫出來了。

系統核心優化參數:(在三台上面添加)

[[email protected] bin]# /etc/sysctl.conf

net.ipv4.tcp_syncookies = 1

net.ipv4.tcp_window_scaling: = 1

net.core.rmem_max = 16777216

net.core.wmem_max = 16777216

net.ipv4.tcp_rmem = 4096 87380 16777216

net.ipv4.tcp_wmem = 4096 65536 16777216

net.ipv4.ip_local_port_range = 1024 65000

net.ipv4.icmp_echo_ignore_all = 1

net.ipv4.tcp_max_syn_backlog = 2048

net.ipv4.tcp_synack_retries = 3

net.ipv4.tcp_fin_timeout = 120

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_tw_recycle = 1

net.ipv4.tcp_keepalive_time = 1200

使配置立即生效:

[[email protected] bin]# /sbin/sysctl -p

繼續閱讀