当前位置: 首页 > article >正文

Nginx 实现七层的负载均衡

一、拓扑结构

							 [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

yum -y install nginx

代理机:需要添加一个桥接模式的网卡 ,保证同一网段内其他真实机器可以访问
因为是实验环境 直接VMware上添加即可

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

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]


代理机上配置添加:

    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这个配置文件

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 新增发布文件目录

[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

http://www.kler.cn/news/311119.html

相关文章:

  • 4位整数的数位和
  • OJ在线评测系统 前端开发设计优化通用菜单组件二 调试用户自动登录
  • 面试官:什么是CAS?存在什么问题?
  • 探索RESTful风格的网络请求:构建高效、可维护的API接口【后端 20】
  • AI换脸等违法行为的最关键原因是个人隐私信息的泄露,避免在网络上发布包含个人敏感信息的照片。
  • 图书管理系统(面向对象的编程练习)
  • 高级c语言(一)
  • Mybatis续
  • 36.贪心算法3
  • Android 内置应用裁剪
  • Java集合面试(上)
  • Kafka+PostgreSql,构建一个总线服务
  • k8s 微服务 ingress-nginx 金丝雀发布
  • ESRGAN——老旧照片、视频帧的修复和增强,提高图像的分辨率
  • ozon买家网址是什么,跨境电商ozon买家网址
  • YOLOv8的GPU环境搭建方法
  • 一个基于 laravel 和 amis 开发的后台框架, 友好的组件使用体验,可轻松实现复杂页面(附源码)
  • 一对一,表的设计
  • Nginx中return和rewrite的区别
  • python 实现entropy熵算法
  • c++ static(详解)
  • Snowflake怎么用?
  • 系统架构设计师 云原生架构篇
  • 字符设备驱动 — 4 异常与中断
  • 【Elasticsearch系列七】索引 crud
  • 【Java】网络编程-地址管理-IP协议后序-NAT机制-以太网MAC机制
  • 爬虫逆向学习(六):补环境过某数四代
  • C++初阶学习第六弹------标准库中的string类
  • 每日刷题(算法)
  • 开发一个电商API接口的步骤!!!