首页 > 系统相关 >Web集群案例实战 -- Nginx 负载均衡 -- 案例实战

Web集群案例实战 -- Nginx 负载均衡 -- 案例实战

时间:2022-12-21 11:34:11浏览次数:44  
标签:实战 index www 案例 -- server nginx html root


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

Web集群案例实战 -- Nginx 负载均衡 -- 案例实战_Web集群


windows 7 Ultimate. 测试添加hosts解析

Web集群案例实战 -- Nginx 负载均衡 -- 案例实战_运维_02


浏览器访问:http://web.wan.org/

Web集群案例实战 -- Nginx 负载均衡 -- 案例实战_Nginx 负载均衡 案例实战_03


刷新

Web集群案例实战 -- Nginx 负载均衡 -- 案例实战_nginx_04


再次刷新

Web集群案例实战 -- Nginx 负载均衡 -- 案例实战_html_05


再次刷新

Web集群案例实战 -- Nginx 负载均衡 -- 案例实战_Nginx 负载均衡 案例实战_06


测试 nginx 负载均衡的后端检测

停止node02 web服务

[root@node02 ~]# systemctl stop nginx
[root@node02 ~]# netstat -lnutp | grep 80
[root@node02 ~]#

node04 脚本测试

Web集群案例实战 -- Nginx 负载均衡 -- 案例实战_Nginx 负载均衡 案例实战_07


恢复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 脚本测试

Web集群案例实战 -- Nginx 负载均衡 -- 案例实战_html_08


实现故障移除,故障恢复后继续添加进集群 -— 起到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秒,可根据业务需求去配置。


标签:实战,index,www,案例,--,server,nginx,html,root
From: https://blog.51cto.com/u_14904176/5959188

相关文章

  • 一对一与一对多
    一对一<!--建立对应关系--><resultMaptype="account"id="accountMap"><idcolumn="aid"property="id"/><resultcolumn="uid"property="uid"/><res......
  • Web集群案例实战 -- Nginx 负载均衡 之 客户端访问日志优化
    Nginx负载均衡之客户端访问日志优化​​前言​​前言本环境是基于Centos7.8系统构建Nginx学习环境具体构建,请参考​​Nginx-1.18.0环境部署​​Nginx在做反向代理......
  • 青龙面板之淘宝
    青龙面板之淘宝【公众号@“项目管理研究所”发送xj】即可查看最全教程详细教程持续更新!!!1.拉库2.一个账号每天约等于1+。自己拿去换啥都可!qlrawhttps://ra......
  • python编程实战案例--turtle图案绘制
    1.turtle库基本介绍(1)turtle(海龟库):是python的标准库之一,是python绘图体系的实现具体实现:海龟(画笔)处于画面正中央,当海龟落下,行走的所形成的的轨迹,就是我们所绘制的图案2.turt......
  • 面试官:讲讲session的持久化
    一、session知识回顾1.1session何时创建?默认第一次访问时,使用到了HttpSession就会创建(request.getSession),访问JSP时由于会自备九大内置对象,因此也会创建session1.2session......
  • tomcat线程池总结
    一.tomcat线程池在开发中我们经常会碰到“池”的概念,比如数据库连接池、内存池、线程池、常量池等。为什么需要“池”呢?程序运行的本质,就是通过使用系统资源(CPU、内存、网......
  • 30秒在Centos7安装Nginx(步骤简单)
    Nginx安装1、安装好依赖gcc、gcc-c++、pcre-devel、zlib-devel、openssl、openssl-devel、wgetyum-yinstallgccpcre-develzlib-developensslopenssl-develgcc-c++w......
  • Linux服务搭建--DHCP
    1.什么是DHCP服务?DHCP(动态主机配置协议):是局域网常见的网络协议,它主要是通过客户端发送广播数据包给整个物理网段内的所有主机,若局域网内存在DHCP服务器,该服务器收到广播包,才......
  • 开流运行多线程CompletableFuture
    CompletableFuture提高接口性能//根据排口查询因子信息(异步)CompletableFuture<List<FactorInfo>>listCompletableFuture=CompletableFuture.supplyAsyn......
  • Linux负载均衡解决方案 -- LVS 理论概述
    Lvs理论概述​​一、什么是LVS?​​​​二、为什么需要LVS?​​​​三、LVS原理​​​​1、LVS体系结构​​​​2、LVS工作模式​​​​3、LVS调度算法​​​​四、L......