Nginx 负载均衡 -- 案例实战
- 前言
前言
本环境是基于 Centos 7.8 系统构建Nginx学习环境
具体构建,请参考 Nginx-1.18.0 环境部署
环境准备
role | host | ip | nginx-version | OS |
nginx proxy host | node01 | 192.168.5.11 | Nginx-1.18.0 | Centos 7.8 |
nginx web server1 | node02 | 192.168.5.12 | Nginx-1.18.0 | Centos 7.8 |
nginx web server2 | node02 | 192.168.5.13 | Nginx-1.18.0 | Centos 7.8 |
nginx client1 | node04 | 192.168.5.14 | ---- | Centos 7.8 |
nginx client2 | windows 7 Ultimate. | 192.168.5.7 | ---- | windows 7 Ultimate. |
配置后端 web服务
---node02
[root@node02 ~]# echo "`hostname -I`www" > /usr/share/nginx/html/www/index.html
[root@node02 ~]# echo "`hostname -I`bbs" > /usr/share/nginx/html/bbs/index.html
[root@node02 ~]# vim /etc/nginx/conf.d/vhost.conf
server {
listen 80;
server_name bbs.yunjisuan.com;
location / {
root /usr/share/nginx/html/bbs;
index index.html index.htm;
}
}
server {
listen 80;
server_name www.yunjisuan.com;
location / {
root /usr/share/nginx/html/www;
index index.html index.htm;
}
}
[root@node02 ~]# systemctl restart nginx
---node03
[root@node03 ~]# mkdir /usr/share/nginx/html/{www,bbs}
[root@node03 ~]# echo "`hostname -I`www" > /usr/share/nginx/html/www/index.html
[root@node03 ~]# echo "`hostname -I`bbs" > /usr/share/nginx/html/bbs/index.html
[root@node03 ~]# vim /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name bbs.yunjisuan.com;
location / {
root /usr/share/nginx/html/bbs;
index index.html index.htm;
}
}
server {
listen 80;
server_name www.yunjisuan.com;
location / {
root /usr/share/nginx/html/www;
index index.html index.htm;
}
}
[root@node03 ~]# systemctl restart nginx
配置反向代理服务器
添加hosts解析
[root@node01 ~]# vim /etc/hosts
192.168.5.11 web.wan.org
192.168.5.12 www.yunjisuan.com bbs.yunjisuan.com
192.168.5.13 www.yunjisuan.com bbs.yunjisuan.com
[root@node01 ~]# vim /etc/nginx/conf.d/vhost.conf
upstream www.server_pools {
server 192.168.5.12 weight=1;
server 192.168.5.13 weight=1;
}
server {
listen 80;
server_name web.wan.org;
location / {
proxy_pass http://www.server_pools;
}
}
[root@node01 ~]# systemctl restart nginx
node04测试
添加hosts解析
[root@node04 ~]# vim /etc/hosts
192.168.5.11 web.wan.org
192.168.5.12 www.yunjisuan.com bbs.yunjisuan.com
192.168.5.13 www.yunjisuan.com bbs.yunjisuan.com
windows 7 Ultimate. 测试添加hosts解析
浏览器访问:http://web.wan.org/
刷新
再次刷新
再次刷新
测试 nginx 负载均衡的后端检测
停止node02 web服务
[root@node02 ~]# systemctl stop nginx
[root@node02 ~]# netstat -lnutp | grep 80
[root@node02 ~]#
node04 脚本测试
恢复node02 web服务
[root@node02 ~]# systemctl start nginx
[root@node02 ~]# netstat -lnutp | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 11875/nginx: master
node04 脚本测试
实现故障移除,故障恢复后继续添加进集群 -— 起到proxy host 端对后端服务器的健康检查的功能
健康检查主要靠upstream模块中的下面两个参数通过实现,不做配置,默认生效!
模块内参数 | 参数说明 |
max_fails=3 | Nginx尝试连接后端主机失败的次数,这个值是配合proxy_next_upstream、fastcgi_next_upstream和memcached_next_upstream 这三个参数来使用的。当nginx接收后端服务器返回这三个参数定义的状态码时,会将这个请求转发给正常工作的后端服务器,例如404、502、503、 Max_fails的默认值是1 ;企业场景下建议2-3次。如京东1次,蓝汛10次,根据业务需求去配置。 |
fail_timeout=10s | 在max_fails定义的失败次数后,距离下次检查的间隔时间,默认是10s ;如果max_fails是5 ,它就检测5次,如果5次都是502,那么,它就会根据fail_timeout的值,等待10s再去检查,还是只检查一次,如果持续502,在不重新加载 Nginx配置的情况下,每隔10s都只检查一次。常规业务2~3秒比较合理,比如京东3秒,蓝汛3秒,可根据业务需求去配置。 |