1. 下载最新nginx, pcre
http://www.nginx.com.cn/download/nginx-0.8.20.tar.gz
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.02.tar.gz
2. 安装nginx, pcre到服务器192.168.1.57
tar zxvf pcre-8.02
cd pcre-8.02
./configure
make
make install
tar zxvf nginx-0.8.20
cd nginx-0.8.20
./configure --with-pcre=../pcre-8.02
make
make install
nginx默认安装路径为: /usr/local/nginx
3. 启动ngnix
cd /usr/local/nginx/sbin
./nginx
关闭命令: ./nginx -s stop
查看是否成功安装: http://192.168.1.57 将看到文字
Welcome to nginx!
4. 在服务器192.168.1.57启动2个tomcat server
server1端口采用tomcat默认端口
server2端口更改: 编辑%TOMCAT_HOME%/conf/server.xml
改shutdown端口8005为8006: <Server port="8006" shutdown="SHUTDOWN">
改Connector端口8080为8090: <Connector port="8090" protocol............
启动tomcat
5. 配置负载均衡
编辑 /usr/local/nginx/conf/nginx.conf
在server前添加:
upstream backend
{
server 192.168.1.57:8080 weight=1;
server 192.168.1.57:8090 weight=1;
}
#说明: 群组backend有2台server, 分别对应2个tomcat, 权重都为1表示每台server负载相同. weight越大权重越大
修改server配置节
server {
listen 80;
server_name 192.168.1.57;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
#保留用户真实信息
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
if (!-f $request_filename) {
rewrite ^/([a-zA-Z0-9]+).html /$1.jsp last;
proxy_pass http://backend;
break;
}
}
#说明proxy_pass http://backend;中的backend为upstream的名字
#配置了urlwrite规则: 如果访问http://192.168.1.57/xx.html 转发到tomcat变为 xx.jsp
6. 配置tomcat session复制
编辑tomcat的server.xml
server1找到Engine添加jvmRoute="jvm1": <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
server2找到Engine添加jvmRoute="jvm2": <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm2">
server1, server2均去掉Cluster的注释: <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
7. 修改应用程序的web.xml文件
添加<distributable/>到</web-app>之前就可以了
重启server1, server2, nginx
配置完成,实现了session复制, 负载均衡, 简单urlrewrite