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
相关推荐
广东航连科技14 分钟前
RFID手持机——物联网时代的核心工具
大数据·运维·网络·数据库·人工智能·物联网·交通物流
lyl00123417 分钟前
【Linux扩容根分区】LVM分区扩容过程踩坑记录
linux·运维·服务器
Sundayday4720 分钟前
tomcat的安装,管理与配置
java·运维·服务器·nginx·tomcat·云计算
匡博16526 分钟前
PyCharm远程连接AutoDL服务器实现程序调试
运维·服务器·python·pycharm
Mr. Sun_28 分钟前
Ubuntu 升级特定软件包
linux·运维·ubuntu
吃面不喝汤661 小时前
如何提升网页加载和跳转速度:Flask 模板渲染 vs Nginx 静态资源处理
python·nginx·flask
晨欣1 小时前
何为supervisorctl以及我们如何使用它
linux·运维·服务器
小安运维日记1 小时前
Linux云计算 |【第四阶段】NOSQL-DAY3
linux·运维·服务器·redis·云计算·html
午与羽1 小时前
Nestjs构建Certeasy证书自动化平台 - 系统部署
运维·docker·pm2
Mo_YuO.o2 小时前
Linux下的基本指令/命令(二)
linux·运维·服务器