Nginx負載均衡Tomcat、Resin實現動靜分離

說明:前端202.207.178.7安裝Nginx,提供負載均衡器及靜態頁面;後端202.207.178.6安裝Tomcat、Resin,提供多實例。有關tomcat多實例的配置詳見本人上一篇博客:Tomcat安裝配置

http://10927734.blog.51cto.com/10917734/1874112

一、編譯安裝並配置Nginx

1、首先添加用戶nginx,實現以之運行nginx服務進程

# groupadd -r -g 108 nginx

# useradd -r -g 108 -u 108 nginx

2、將下載好的軟件包解壓並安裝(我這裏是nginx-1.4.7.tar.gz)

# tar xf nginx-1.4.7.tar.gz

# cd nginx-1.4.7

接着開始編譯和安裝:

# ./configure \

--prefix=/usr \

--sbin-path=/usr/sbin/nginx \

--conf-path=/etc/nginx/nginx.conf \

--error-log-path=/var/log/nginx/error.log \

--http-log-path=/var/log/nginx/access.log \

--pid-path=/var/run/nginx/nginx.pid  \

--lock-path=/var/lock/nginx.lock \

--user=nginx \

--group=nginx \

--with-http_ssl_module \

--with-http_flv_module \

--with-http_stub_status_module \

--with-http_gzip_static_module \

--http-client-body-temp-path=/var/tmp/nginx/client/ \

--http-proxy-temp-path=/var/tmp/nginx/proxy/ \

--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \

--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \

--http-scgi-temp-path=/var/tmp/nginx/scgi \

--with-pcre \

--with-file-aio

# make && make install

報錯時可能要求安裝如下包,按需安裝即可!

# yum -y install pcre-devel

# yum -y install gcc

# yum -y install openssl-devel

3、爲nginx提供SysV init腳本:


新建文件/etc/rc.d/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/sbin/nginx"

prog=$(basename $nginx)

 

NGINX_CONF_FILE="/etc/nginx/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=$?

echo

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

return $retval

}

 

restart() {

configtest || return $?

stop

sleep 1

start

}

 

reload() {

configtest || return $?

echo -n $"Reloading $prog: "

killproc $nginx -HUP

RETVAL=$?

echo

}

 

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

$1

;;

restart|configtest)

$1

;;

reload)

rh_status_q || exit 7

$1

;;

force-reload)

force_reload

;;

status)

rh_status

;;

condrestart|try-restart)

rh_status_q || exit 0

;;

*)

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

exit 2

esac


4、而後爲此腳本賦予執行權限:

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


5、添加至服務管理列表,並讓其開機自動啓動:

# chkconfig --add nginx

# chkconfig nginx on


6、而後就可以啓動服務並測試了:

# service nginx start

此時訪問202.207.178.7可以訪問到nginx的默認頁面

二、nginx負載均衡Tomcat,並實現動靜分離(在202.207.178.7上。在此之前,應該先在後端安裝了Tomcat多實例)

1、編輯nginx配置文件,添加以下內容

# vim /etc/nginx/nginx.conf

upstream tomcat{


           server 202.207.178.6:8080 weight=1 max_fails=2 fail_timeout=30s;

           server 202.207.178.6:8081 weight=1 max_fails=2 fail_timeout=30s;

           server 202.207.178.6:8082 weight=1 max_fails=2 fail_timeout=30s;


}

server

 {

listen       80;

server_name  www.wuguangke.cn;

index index.jsp index.html index.htm;

#配置發佈目錄爲/data/www;

root  /data/webapps/www;

#動態頁面交給http://tomcat,也即我們之前在nginx.conf定義的                 upstream tomcat 均衡

location /

 {

#出錯後跳到下一臺

proxy_next_upstream http_502 http_504 error                         timeout invalid_header;

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://tomcat;

expires      3d;

}

#配置Nginx動靜分離,定義的靜態頁面直接從Nginx發佈目錄讀取

location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$

{

 root /data/webapps/www;

 #expires定義用戶瀏覽器緩存的時間爲3天,如果靜態頁面不常

                   更新,可以設置更長,這樣可以節省帶寬和緩解服務器的壓力

 expires      3d;

}

 }

2、創建靜態頁面目錄和文件

# mkdir -p /data/webapps/www

# vim /data/webapps/www/index.html

添加如下內容:

This is nginx static

3、配置本地域名解析(在進行訪問測試的真機上)

修改C:\Windows\System32\drivers\etc\hosts文件,添加如下一行

202.207.178.7 www.fsy.cn

3、重啓nginx服務,進行訪問測試

# service nginx restart

此時,訪問http://www.fsy.cn/,會現後端3個tomcat實例負載均衡;訪問http://www.fsy.cn/index.html會發現訪問到前端nginx上的測試頁,實現了動靜分離!

三、安裝配置resin

說明:

Resin是CAUCHO公司的產品,是一個非常流行的application server,對servlet和JSP提供了良好的支持,性能也比較優良,resin自身採用JAVA語言開發。

resin 普通版本和pro版本主要區別是 pro支持緩存和負載均衡。pro因爲有強大的cache功能,獨立作爲web服務器處理靜態頁面性能都可以和apache有一比。但普通版本獨立作爲web服務器性能就要差一些。當然可以使用apache+resin的方案藉助apache的緩存功能提高性能。

1、編譯安裝軟件包

1)安裝所需環境:

#yum -y install gcc

# yum -y install openssl-devel

2)安裝Resin

# tar xf resin-4.0.33.tar.gz

# ./configure --prefix=/usr/local/resin --with-resin-log=/data/logs/resin/           --with-java-home=/usr/java/jdk1.6.0_21/

# make && make install

2、修改配置文件

# cd /usr/local/resin/conf

# vim resin.xml

<web-app id="/" root-directory="/data/webapps/www3"/>

# vim resin.properties 

修改如下兩項:

app.http          : 8081

web.http          : 8081

3、提供主訪問測試頁面

# vim /data/webapps/www3/index.jsp

如下內容:

<h1> RESIN_3 JSP Test Page</h1>

<%=new java.util.Date()%>

</body>

</html>

4、停止tomcat3,啓動resin進行訪問測試

# /usr/local/tomcat3/bin/shutdown.sh

# /usr/local/resin/bin/resin.sh start

此時訪問http://202.207.178.6:8082/,會訪問到我們自定義的resin,訪問http://www.fsy.cn/,會實現2個Tomcat實例和一個resin負載均衡,訪問http://www.fsy.cn/index.html,會看到自定義的靜態頁面


                            歡迎批評指正!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章