nginx+tomcat實現負載均衡

Nginx處理靜態web頁面,tomcat處理動態頁面,動靜結合。

Nginx+tomcat實現負載均衡1

負載均衡服務器:192.168.0.179

負載均衡主機:192.168.0.179

192.168.0.34

179主機配置如下:

[root@server79 lnmp]# scp tomcat/ jdk/ 192.168.0.34:/usr/local/

[root@server79 conf]# vim nginx.conf

# location ~ \.jsp$ {

       #    proxy_pass   http://127.0.0.1:8080;

       #}

http {

       upstream westos{

       server 192.168.0.34:8080;

       server 192.168.0.179:8080;

       }

   include       mime.types;

server {

listen          80;

server_name     www.westos.org;

location ~ \.jsp$ {

               proxy_pass http://westos;

}

[root@server79 ROOT]# vim test.jsp

server79 time is: <%=new java.util.Date() %>

[root@server79 ROOT]# scp test.jsp  192.168.0.34:/usr/local/tomcat/webapps/ROOT

[email protected]'s password:

test.jsp  

Nginx -t,nginx -s reload

34測試主機:

[root@server34 bin]# vim /etc/profile

export JAVA_HOME=/usr/local/jdk

export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib

export PATH=$PATH:$JAVA_HOME/bin

激活

[root@server34 bin]# source /etc/profile

[root@server34 ROOT]# vim test.jsp

server34 time is: <%=new java.util.Date() %>

[root@server34 bin]# ./startup.sh

Using CATALINA_BASE:   /usr/local/tomcat

Using CATALINA_HOME:   /usr/local/tomcat

Using CATALINA_TMPDIR: /usr/local/tomcat/temp

Using JRE_HOME:        /usr/local/jdk

Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar

測試:

http://www.westos.org/test.jsp

多次刷新會看到:server79 time is: Tue Apr 22:03:42:52 CST 2014

               Server34 time is: Tue Apr 22:03:42:54 CST 2014

Nginx+tomcat實現負載均衡2

Nginx+tomcat實現負載均衡1已經通過nginx+tomcat實現了負載均衡,但出現一個問題,當刷新頁面時,界面會跳動,這樣的場景我們不奔潰,客戶早都淚奔。爲了解決界面跳動的問題,我們用nginx-sticky粘住界面,只有當客戶清除歷史記錄再次刷新時纔有可能跳轉到另一界面。以下就是實現此功能的過程:

負載均衡服務器:192.168.0.179

測試主機:192.168.0.34

192.168.0.179

下載包: nginx-sticky-module-1.0.tar.gz

nginx-1.4.2.tar.gz

解壓

[root@server79 ~]# tar zxf nginx-sticky-module-1.0 nginx-1.4.2.tar.gz

爲了安全,刪除版本

[root@server79 nginx-1.4.2]# vim src/core/nginx.h  

#define NGINX_VER          "nginx"

註釋掉debug,使得編譯更純淨

[root@server79 nginx-1.4.2]# vim auto/cc/gcc

#CFLAGS="$CFLAGS -g"

編譯,加https加密模塊,http的狀態模塊,nginx-sticky模塊

[[email protected]]#./configure --prefix=/usr/local/lnmp/nginx/ --with-http_ssl_module--with-http_stub_status_module --add-module=/root/nginx-sticky-module-1.0

make之前,stop掉nginx

[root@server79 nginx-1.4.2]# nginx -s stop

修改nginx的配置文件,加入sticky參數

[root@server79 nginx-1.4.2]# vim /usr/local/lnmp/nginx/conf/nginx.conf

http {

       upstream westos{

       sticky;

       server 192.168.0.34:8080;

       server 192.168.0.179:8080;

       }

檢測nginx的配置文件時顯示檢測失敗,需make

[root@server79 nginx-1.4.2]# nginx -t

nginx:[emerg]unknowndirective "sticky" in /usr/local/lnmp/nginx/conf/nginx.conf:20

nginx: configuration file /usr/local/lnmp/nginx/conf/nginx.conf test failed

[root@server79 nginx-1.4.2]# make && make install

再次檢測成功

[root@server79 nginx-1.4.2]# nginx -t

nginx: the configuration file /usr/local/lnmp/nginx//conf/nginx.conf syntax is ok

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

啓動nginx

[root@server79 nginx-1.4.2]# nginx

測試:

[root@server79 bin]# ./startup.sh

[root@server79 bin]# pwd

/usr/local/lnmp/tomcat/bin

[root@server34 bin]# ./startup.sh

http://www.westos.org/test.jsp

出現:server79 time is:Sun Apr 20 09:28:17 CST 2014,當持續刷新時。頁面的server79一直沒變即成功,要想出現192.168.0.34的測試頁面,需清除記錄


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