天天看點

用Nginx反向代理多個Tomcat實作負載和session共享

 用Nginx反向代理多個Tomcat實作負載和session共享

之前的一篇博文寫了用Apache做反向代理實作Tomcat的負載和session,下面來用Nginx來實作。

實作系統:CentOS 5.5 32bit

一、準備工作:

1、安裝服務所需的工具,把開發庫和開發工具兩個包用yum安裝上就行了。

# yum groupinstall "Development Libraries" "Development Tools" 

2、安裝nginx之前還要安裝pcre-devel包

# yum install pcre-devel 

二、安裝Nginx

1、Nginx下載下傳位址:www.nginx.org這個是nginx的官方網站。

為了實作session共享還得下載下傳nginx的擴充子產品nginx-upstream-jvm-route-master,下載下傳位址:https://github.com/tbje/nginx-upstream-jvm-route

要想實作後面Tomcat的健康檢測,還得下載下傳子產品nginx_upstream_check_module-master 下載下傳位址:https://nodeload.github.com/yaoweibin/nginx_upstream_check_module/zip/master

在這裡所用的軟體預設下載下傳到/usr/local/src目錄中

2、安裝之前要給nginx添加個使用者:

# groupadd nginx 

# useradd -g nginx nginx 

3、安裝Nignx,并加入所需的擴充子產品

# cd /usr/local/src 

解壓那兩個擴充子產品包 

# unzip nginx-upstream-jvm-route-master.zip 

# unzip nginx_upstream_check_module-master.zip 

之後再來安裝nginx 

# tar zxvf nginx-1.2.5.tar.gz 

# cd nginx-1.2.5 

下面對nginx加入擴充子產品 

# patch -p0 < ../nginx-upstream-jvm-route-master/jvm_route.patch  

# patch -p1 < ../nginx_upstream_check_module-master/check1.2.2+.patch (在nginx 1.2以上的版本都要用這個patch不然後會報錯的) 

# ./configure --prefix=/usr/local/nginx \

> --user=nginx --group=nginx --with-http_ssl_module \

> --with-http_flv_module --with-http_stub_status_module \

> --with-http_gzip_static_module --http-proxy-temp-path=/var/tmp/nginx/proxy/ \

> --with-pcre \

> --add-module=/usr/local/src/nginx_upstream_check_module-master \

> --add-module=/usr/local/src/nginx-upstream-jvm-route-master 

如果沒有什麼報錯的話在結尾處會看如下所示的内容: 

configuring additional modules 

adding module in /usr/local/src/nginx_upstream_check_module-master 

checking for ngx_http_upstream_check_module ... found 

 + ngx_http_upstream_check_module was configured 

adding module in /usr/local/src/nginx-upstream-jvm-route-master 

 + ngx_http_upstream_jvm_route_module was configured 

checking for PCRE library ... found 

checking for PCRE JIT support ... not found 

checking for OpenSSL library ... found 

checking for zlib library ... found 

creating objs/Makefile 

之後就可以make和make install了 

# make 

# make install 

這樣Nginx就安裝好了,下面給nginx制作一個啟動腳本

# vim /etc/init.d/nginx 

内容如下: 

#!/bin/sh  

# nginx - this script starts and stops the nginx daemon  

# chkconfig:     - 85 15     

# description:    Nginx is an HTTP(S) server, HTTP(S) reverse \  

#                             proxy and IMAP/POP3 proxy server  

# processname: nginx  

# config:            /etc/nginx/nginx.conf  

# config:            /etc/sysconfig/nginx  

# pidfile:         /var/run/nginx.pid  

# Source function library.  

. /etc/rc.d/init.d/functions 

# Source networking configuration.  

. /etc/sysconfig/network 

# Check that networking is up.  

[ "$NETWORKING" = "no" ] && exit 0 

nginx="/usr/local/nginx/sbin/nginx" 

prog=$(basename $nginx) 

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" 

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx 

lockfile=/var/lock/subsys/nginx 

make_dirs() { 

     # make required directories  

     user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` 

     options=`$nginx -V 2>&1 | grep 'configure arguments:'` 

     for opt in $options; do 

             if [ `echo $opt | grep '.*-temp-path'` ]; then 

                     value=`echo $opt | cut -d "=" -f 2` 

                     if [ ! -d "$value" ]; then 

                             # echo "creating" $value  

                             mkdir -p $value && chown -R $user $value 

                     fi 

             fi 

     done 

start() { 

        [ -x $nginx ] || exit 5 

        [ -f $NGINX_CONF_FILE ] || exit 6 

        make_dirs 

        echo -n $"Starting $prog: "  

        daemon $nginx -c $NGINX_CONF_FILE 

        retval=$? 

        echo  

        [ $retval -eq 0 ] && touch $lockfile 

        return $retval 

stop() { 

        echo -n $"Stopping $prog: "  

        killproc $prog -QUIT 

        [ $retval -eq 0 ] && rm -f $lockfile 

restart() { 

        configtest || return $? 

        stop 

        sleep 1 

        start 

reload() { 

        echo -n $"Reloading $prog: "  

        killproc $nginx -HUP 

        RETVAL=$? 

force_reload() { 

        restart 

configtest() { 

    $nginx -t -c $NGINX_CONF_FILE 

rh_status() { 

        status $prog 

rh_status_q() { 

        rh_status >/dev/null 2>&1 

case "$1" in 

        start) 

                rh_status_q && exit 0 

                $1 

                ;; 

        stop) 

                rh_status_q || exit 0 

        restart|configtest) 

        reload) 

                rh_status_q || exit 7 

        force-reload) 

                force_reload 

        status) 

                rh_status 

        condrestart|try-restart) 

                        ;; 

        *) 

                echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"  

                exit 2 

esac 

儲存後退出,并給它一個執行的權限。 

# chmod +x /etc/init.d/nginx 

下面就可以啟動了,

# service nginx start 

Starting nginx:                                            [  OK  ] 

對于JDK與Tomcat的安裝配置前面的博文中有詳細的說明,在這裡就不重複了,下面來配置一下Ngixn代理tomcat并實作session共享,及後端tomcat的健康檢測。

對nginx的配置檔案如下所示:

#user  nobody;   //運作nginx的使用者,預設是nobody 

worker_processes  1; //這個是設定nginx開啟的程序數,每個程序資料平均消耗10MB-20MB記憶體,一般一個就夠了,如果是多核cpu建議指定和cpu的數量一樣多的程序即可。 

#error_log  logs/error.log;  //設定錯誤日志 

#error_log  logs/error.log  notice; 

#error_log  logs/error.log  info; 

#pid        logs/nginx.pid; //設定程序的id的存儲檔案位置 

events {    //events指令用來設定Nginx的工作模式及其連接配接數的上限。 

    use epoll;  //設定epoll為Nginx的工作模式。 

    worker_connections  1024; 

http { 

    include       mime.types; 

    default_type  application/octet-stream; 

    sendfile        on; 

    tcp_nopush     on; 

    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; 

    upstream tomcat { 

        ip_hash;  //采用ip_hash排程算法

        server 192.168.1.222:8080; 

        server 192.168.1.222:9080; 

        check interval=3000 rise=2 fall=5 timeout=1000; \\這個是背景的健康檢測 

        } 

    server { 

        listen       80; 

        server_name  localhost; 

        index index.jsp index.action; 

        root /usr/local/tomcat/app1/apps/fis; 

        location ~ .*\.(jsp|action|js)$ { 

                proxy_pass      http://tomcat; 

                proxy_redirect  off; 

                proxy_set_header        Host    $http_host; 

                proxy_set_header        X-Real-IP       $remote_addr; 

                proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for; 

                chunked_transfer_encoding       off; 

        location /status {          //在網頁上檢視背景tomcat和狀态 

                check_status; 

                access_log off; 

        error_page   500 502 503 504  /50x.html; 

        location = /50x.html { 

            root   html; 

    } 

在Nginx端設定完成了,在這裡要說明一下這個配置檔案中采用了ip_hash排程算法,還有其他工作模式,在這裡就不一一介紹了,說一下ip_hash工作模式,對于每個請求按通路ip的hash結果配置設定,這樣來自同一個ip的通路使用者固定通路一個背景的伺服器,這個能有效解決動态網頁session共享問題。

下面來配置tomcat,主要在sever.xml檔案中配置,所需配置和在Apache代理時相同,請參考:

http://zhou123.blog.51cto.com/4355617/981560

配置完成後測試結果如下:

<a href="http://blog.51cto.com/attachment/201303/184808762.jpg" target="_blank"></a>

這個圖檔說明了,使用者通路了jvm1這個伺服器,然後把jvm1服務停止後結果如下:

<a href="http://blog.51cto.com/attachment/201303/184906664.jpg" target="_blank"></a>

由此可知session已經共享成功

下面來看一下,背景的健康檢測

在浏覽器中輸入:http://192.168.1.222/status 結果如下:

<a href="http://blog.51cto.com/attachment/201303/185109324.jpg" target="_blank"></a>

當把一台服務停止後:

<a href="http://blog.51cto.com/attachment/201303/185135656.jpg" target="_blank"></a>

這個會很清楚的看到那個服務當機了。

關于Nginx的内容簡單的說到這裡了。

本文轉自 ZhouLS 51CTO部落格,原文連結:http://blog.51cto.com/zhou123/1163090