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