nginx 是一款非常优秀的代理,静态资源,负载均衡的软件。用它来和tomcat结合,正好可以弥补tomcat静态资源处理能力弱的缺点。
配置nginx
1.首先在nginx的conf目录下新建文件夹vhost 用来存放虚拟主机的配置文件(nginx.conf 中默认是有包含该文件夹下的所有 conf文件)
mkdir /usr/local/nginx/conf/vhost
2.新建配置文件
vi www.domain.com.conf
内容如下
upstream dumain_server {
server 127.0.0.1:80 weight=1;# 服务器1 ip 权重为1
server 118.244.197.83:80 weight=1;# 服务器2 ip 权重为1
}
server {
listen 80;
server_name www.domian.com;
location / {
root html;
index index.jsp index.html index.htm;
proxy_redirect off;
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://domian_server;
}
error_page 500 502 503 504 /50x.html; #配置错误页面
location = /50x.html {
root html;
}
}
3.检查配置文件是否有错
nginx -t
4.如果配置文件没错重启nginx
service nginx restart
5.测试
访问www.domain.com 多次刷新 看是否会切换页面,如果会的话 第一步就成功啦!
6.配置session共享
session问题是负载均衡必须解决的问题,但是当集群数量较多时,不建议采用session共享,因为很耗资源。具体过程如下
http://blog.csdn.net/caiwenfeng_for_23/article/details/45666831