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的测试页面,需清除记录