Nginx 实现七层的负载均衡

一、拓扑结构

bash 复制代码
							 [vip: 20.20.20.20]
外网 桥接模式(vip)
内网 nat模式
						[LB1 Nginx]		    [LB2 Nginx]
					     192.168.1.2		192.168.1.3

		[index]		[milis]		 [videos]	   [images]  	  [news]
		 1.11		 1.21		   1.31			1.41		  1.51
		 1.12		 1.22		   1.32			1.42		  1.52
		 1.13		 1.23		   1.33			1.43		  1.53
		 ...		 ...		   ...			...		      ...
		 /web     /web/milis    /web/videos     /web/images   /web/news
	  index.html  index.html     index.html      index.html   index.html

分析:

1.准备三台虚拟机

2.每台机器安装nginx

bash 复制代码
yum -y install nginx

代理机:需要添加一个桥接模式的网卡 ,保证同一网段内其他真实机器可以访问

因为是实验环境 直接VMware上添加即可

bash 复制代码
9: ens36: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:d1:c4:a1 brd ff:ff:ff:ff:ff:ff
    inet 192.168.101.89/24 brd 192.168.101.255 scope global noprefixroute dynamic ens36
       valid_lft 53818sec preferred_lft 53818sec

还剩两台服务器这里模仿有五个业务场景 每个业务场景有两台负载机 即一共需要十个机器,这里用新增ip的方式来模拟 剩余两台每台机器新增五个ip

bash 复制代码
ip addr add 192.168.1.100/24 dev eth0 #添加
ip addr del 192.168.1.100/24 dev eth0 #删除

# 需要注意的是这里的网段需要一样才能访问????  这里有疑问  暂时未解决

ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:fb:ff:6f brd ff:ff:ff:ff:ff:ff
    inet 192.168.29.143/24 brd 192.168.29.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 192.168.29.160/24 scope global secondary ens33
       valid_lft forever preferred_lft forever
    inet 192.168.29.161/24 scope global secondary ens33
       valid_lft forever preferred_lft forever
    inet 192.168.29.162/24 scope global secondary ens33
       valid_lft forever preferred_lft forever
    inet 192.168.29.163/24 scope global secondary ens33
       valid_lft forever preferred_lft forever
    inet 192.168.29.164/24 scope global secondary ens33
       valid_lft forever preferred_lft forever
# 这里每个ip 代表一个业务机[index][milis][videos][images][news]

代理机上配置添加:

bash 复制代码
    upstream index {
        server 192.168.29.170:80 weight=1 max_fails=2 fail_timeout=2;
        server 192.168.29.160:80 weight=2 max_fails=2 fail_timeout=2;
       }
    upstream news {
        server 192.168.29.171:81 weight=1 max_fails=2 fail_timeout=2;
        server 192.168.29.161:81 weight=2 max_fails=2 fail_timeout=2;
       }
          
    upstream milis {
        server 192.168.29.172:80 weight=1 max_fails=2 fail_timeout=2;
        server 192.168.29.162:80 weight=2 max_fails=2 fail_timeout=2;
       }
       
     upstream videos {
        server 192.168.29.173:80 weight=1 max_fails=2 fail_timeout=2;
        server 192.168.29.163:80 weight=2 max_fails=2 fail_timeout=2;
       }
       
     upstream images {
        server 192.168.29.174:80 weight=1 max_fails=2 fail_timeout=2;
        server 192.168.29.164:80 weight=2 max_fails=2 fail_timeout=2;
       }

       
     server {
          	location / {
      		proxy_pass http://index/;
      		}
      		
      		location  /news {
      		proxy_pass http://news/;
      		}
      		
      		location /milis {
      		proxy_pass http://milis/;
      		}
      		
      		location ~* \.(wmv|mp4|rmvb)$ { 
             proxy_pass http://videos; 
      		}

      		location ~* \.(png|gif|jpg)$ { 
      		proxy_pass http://images;
      		}
        }

# 这里需要注意的点是proxy_pass http://videos;proxy_pass http://milis/;
# 结尾/的问题
# 加/ 的情况假设你的访问路径  curl xxxxx/aa/milis   proxy_pass http://milis/;  结尾加了/   这里会将location后面匹配到的部分去掉  你的url就变为了  http://milis组负载均衡到的的ip/aa 
# 不加/ 的情况 会保留 即 假设你的 curl  xxxx/a.png     通过正则匹配location ~* \.(png|gif|jpg)$     实际url 就是:http://images组负载均衡到的的ip/a.png

服务器

下面以images 举例 首先在conf.d/下新增images.conf这个配置文件

bash 复制代码
server {
    listen       192.168.29.164:80;
    server_name  www.images.com;
    root         /usr/share/nginx/html/images;
    access_log   /var/log/nginx/index-access.log  main;

    # Load configuration files for the default server block.
    # include /etc/nginx/default.d/*.conf;
    location /  {
        index index.html  index.hml;
    }

    error_page 404 /404.html;
    location = /404.html {
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
    }
} 



[root@slave01 html]# tree /etc/nginx/
/etc/nginx/
├── conf.d
│   ├── default.conf
│   ├── images.conf
│   ├── index.conf
│   ├── milis.conf
│   ├── news.conf
│   └── videos.conf
├── fastcgi_params
├── mime.types
├── modules -> ../../usr/lib64/nginx/modules
├── nginx.conf
├── scgi_params
└── uwsgi_params

然后根据root /usr/share/nginx/html/images 新增发布文件目录

bash 复制代码
[root@slave01 html]# tree /usr/share/nginx/
/usr/share/nginx/
└── html
    ├── 50x.html
    ├── images
    │   ├── a.png
    │   └── index.html
    ├── index
    │   └── index.html
    ├── index.html
    ├── milis
    │   └── index.html
    ├── news
    │   └── index.html
    └── videos
        ├── a.mp4
        └── index.html
相关推荐
ζั͡山 ั͡有扶苏 ั͡✾4 小时前
EFK 日志系统搭建完整教程
运维·jenkins·kibana·es·filebeat
jun_bai5 小时前
python写的文件备份网盘程序
运维·服务器·网络
欢喜躲在眉梢里5 小时前
CANN 异构计算架构实操指南:从环境部署到 AI 任务加速全流程
运维·服务器·人工智能·ai·架构·计算
weixin_537765806 小时前
【容器技术】虚拟化原理与Docker详解
运维·docker·容器
胡斌附体6 小时前
docker健康检查使用
运维·docker·依赖·健康检查
摇滚侠6 小时前
2025最新 SpringCloud 教程,负载均衡 API 测试,笔记10
笔记·spring cloud·负载均衡
云飞云共享云桌面6 小时前
无需配置传统电脑——智能装备工厂10个SolidWorks共享一台工作站
运维·服务器·前端·网络·算法·电脑
福尔摩斯张6 小时前
《C 语言指针从入门到精通:全面笔记 + 实战习题深度解析》(超详细)
linux·运维·服务器·c语言·开发语言·c++·算法
虚伪的空想家7 小时前
arm架构服务器使用kvm创建虚机报错,romfile “efi-virtio.rom“ is empty
linux·运维·服务器·javascript·arm开发·云原生·kvm
火车头-1107 小时前
【docker 部署nacos1.4.7】
运维·docker·容器