nginx实现负载均衡
web02配置(我的测试环境web01和web03也是一样的配置)
[root@web02 conf.d]# cat /etc/nginx/conf.d/{bbs,www}.conf
server { listen 80; server_name bbs.soulchild.cn; location / { root /html/bbs; index index.html; } } server { listen 80; server_name www.soulchild.cn; location / { root /html/www; index index.html; }
}
[root@web02 conf.d]# cat /html/{bbs,www}/index.html web02-10.0.0.8-bbs web02-10.0.0.8-www
负载均衡lb01配置
[root@lb01 ~]# cat /etc/nginx/conf.d/lb.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
proxy_pass http://soulchild;
proxy_set_header host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
upstream soulchild {
server 10.0.0.7:80 backup;
server 10.0.0.8:80 weight=7 max_fails=3 fail_timeout=20s;
server 10.0.0.9:80 weight=3 max_fails=3 fail_timeout=20s;
}
upstream区块特殊指令和用法:
upstream:定义集群信息
server:定义集群节点(可以使用以下参数)
max_fails:连接失败,重试次数(默认1)
fail_timeout:重新检查间隔时间(默认10s),失败指定次数(max_fails)后,间隔(fail_timeout)时间后,重试1次,失败则结束.(成功后下次检测还是3次机会)
backup:热备,所有节点不能访问时,使用backup
weight:设置轮询权重(分配比例)
ip_hash:同一个用户访问,分配到同一台服务器,可以解决session问题