Nginx七层负载均衡

server/2024/9/23 12:16:20/

1、七层负载均衡介绍

Nginx七层负载均衡是在应用层(HTTP/HTTPS)上进行的,可以根据HTTP请求的具体内容,如URL、Cookie、Header等,来决定将请求转发到哪个后端服务器。这种方式不仅能够均衡服务器的计算负载,还能实现更复杂的路由策略,例如:

  • 会话粘性(Sticky Sessions):确保用户的会话请求始终被定向到同一个后端服务器。

  • 基于内容的路由:根据请求的内容(如URL、头部信息)将请求分发到不同的服务器。

1.1 四层与七层负载均衡的区别

1.1.1 四层负载均衡(Layer 4 Load Balancing)

  • 在传输层(Transport Layer)上进行。

  • 关注网络层面的信息,如源和目标IP地址、端口号等。

  • 根据网络信息决定将数据包转发到哪个服务器。

  • 不深入检查数据包的内容。

  • 主要适用于基于TCP/UDP的流量,如HTTP和HTTPS。

1.1.2 七层负载均衡(Layer 7 Load Balancing)

  • 在应用层(Application Layer)上进行。

  • 深入检查网络流量的内容,如HTTP请求头、URL、Cookie等。

  • 根据流量内容做复杂的路由决策。

  • 可以处理多种应用层协议,不仅限于HTTP。

1.2 七层负载均衡配置

服务器类型IP地址发行版
代理服务器(proxy)192.168.110.31/24Rocky Linux 8
静态地址服务器(static)192.168.110.32/24Rocky Linux 8
默认地址服务器(default)192.168.110.33/24Rocky Linux 8
动态地址服务器(upload)192.168.110.34/24Rocky Linux 8

1.2.1 后端节点配置

1.2.1.1 静态地址服务器(static)
[root@static ~]# mkdir /nginx/static
[root@static ~]# echo "This is static page IP=`hostname -I`" >> /nginx/static/index.html
[root@static ~]# vim /etc/nginx/conf.d/VirtualHost.conf
server {listen 192.168.110.32:80;server_name www.nginx,com;root /nginx;
​location / {index index.html;}
}
​
[root@static ~]# nginx -s reload
[root@static ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@static ~]# curl 192.168.110.32/static/
This is static page IP=192.168.110.32 
1.2.1.2 默认地址服务器(default)
[root@default ~]# mkdir /nginx/default
[root@default ~]# echo "This is default page IP=`hostname -I`" >> /nginx/default/index.html
[root@default ~]# vim /etc/nginx/conf.d/VirtualHost.conf 
server {listen 192.168.110.33:80;server_name www.nginx,com;root /nginx/default;
​location / {index index.html;}
}
​
[root@default ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@default ~]# nginx -s reload
[root@default ~]# curl 192.168.110.33/default/
This is default page IP=192.168.110.33 
1.2.1.3 动态地址服务器(upload)
[root@upload ~]# mkdir /nginx/upload
[root@upload ~]# echo "This is upload page IP=`hostname -I`" >> /nginx/upload/index.html
[root@upload ~]# vim /etc/nginx/conf.d/VirtualHost.conf 
server {listen 192.168.110.34:80;server_name www.nginx,com;root /nginx;
​location / {index index.html;}
}
​
[root@upload ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@upload ~]# nginx -s reload
[root@upload ~]# curl 192.168.110.34/upload/
This is upload page IP=192.168.110.34 

1.2.2 代理服务器配置

[root@proxy ~]# vim /etc/nginx/conf.d/proxy.conf
upstream static_pools {server 192.168.110.32;
}
​
upstream default_pools {server 192.168.110.33;
}
​
upstream upload_pools {server 192.168.110.34;
}
​
server {listen 80;server_name www.nginx.com;
​location /static {proxy_pass http://static_pools;proxy_set_header host $host;proxy_set_header X-Forwarded-For $remote_addr;}
​location /default {proxy_pass http://default_pools;proxy_set_header host $host;proxy_set_header X-Forwarded-For $remote_addr;}
​location /upload {proxy_pass http://upload_pools;proxy_set_header host $host;proxy_set_header X-Forwarded-For $remote_addr;}
}
​
[root@proxy ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@proxy ~]# nginx -s reload

注意:这里location的写法,这里的主要难点就在这。例如:如果站点目录写 root /nginx/ststic; 那最后访问 http://www.nginx.com/static/ 时转发路径就为http://www.nginx.com/static/static。所以写/nginx就行了

1.2.3 客户端测试访问

[root@client ~]# echo '192.168.110.31 www.nginx.com' >> /etc/hosts
[root@client ~]# curl http://www.nginx.com
This is default page IP=192.168.110.33 
[root@client ~]# curl http://www.nginx.com/static/
This is static page IP=192.168.110.32 
[root@client ~]# curl http://www.nginx.com/upload/
This is upload page IP=192.168.110.34 

1.2.4 查看各节点访问日志

[root@static ~]# tail -1 /var/log/nginx/access.log
192.168.110.31 - - [21/Apr/2024:17:07:34 +0800] "GET /static/ HTTP/1.0" 200 39 "-" "curl/7.61.1" "192.168.110.35"
​
[root@default ~]# tail -1 /var/log/nginx/access.log
192.168.110.31 - - [21/Apr/2024:17:07:32 +0800] "GET / HTTP/1.0" 200 40 "-" "curl/7.61.1" "192.168.110.35"
​
[root@upload ~]# tail -1 /var/log/nginx/access.log
192.168.110.31 - - [21/Apr/2024:17:07:36 +0800] "GET /upload/ HTTP/1.0" 200 39 "-" "curl/7.61.1" "192.168.110.35"

http://www.ppmy.cn/server/11972.html

相关文章

ubuntu 23.04 Dell T3660 听歌没声音的尝试

首先,还是要安装PulseAudio Volume Control sudo apt install pulseaudio 或者 snap install pulseaudio 装了pulseaudio可以在configure和playback间切换选择用哪个声卡输出声音,一般选Stereo Analog Output 网上其他办法也可以试试,比…

【内附完整redis配置文件】linux服务器命令设置redis最大限制内存大小,设置redis内存回收机制,redis有哪些回收机制

redis经常出现进程自己挂掉,经排查后是因为redis占用内存过大,导致服务器内存爆满进程自己挂掉 第一步:打开 Redis 的配置文件 打开 Redis 的配置文件 redis.conf,通常位于 /etc/redis/redis.conf。 第二步:设置redi…

vue项目打包时因为图片问题报错

执行 npm run build命令打包项目时报错,看起来是图片的问题: package.json里面image-webpack-loader的版本是^7.0.1 解决方案: 1、先卸载 npm uninstall image-webpack-loader 2、用cnpm重新安装 cnpm install image-webpack-loader --save…

代码随想录算法训练营第四十六天| LeetCode139.单词拆分

一、LeetCode139.单词拆分 题目链接/文章讲解/视频讲解:https://programmercarl.com/0139.%E5%8D%95%E8%AF%8D%E6%8B%86%E5%88%86.html 状态:已解决 1.思路 单词明显就是物品,字符串s明显就是背包,那么问题就变成了物品能不能把背…

便携式应急指挥箱规格参数

概况: 微缩型的无线视频音频传输的机动挥所。体积小、重量轻、公配电方便、携带便携、功能齐全。可进行单兵作战,通过此无线音频视频传输的指挥箱能完成现场图像、语音、数据的采集等功能,可以通过5G/4G/WIFI等多种无线网络完成传输的需求,或…

Excel 冻结前几行

Excel中有冻结首航和冻结首列的选项,但是如果想冻结前几行该怎么操作? 冻结首行或冻结首列 视图 -> 冻结窗格 -> 冻结首行或冻结首列 冻结前几行或前几列 视图 -> 冻结窗格 -> 冻结拆分窗格 具体冻结几行和几列取决于当前选中的单元格。…

java 学习一

jdk下载地址 配置环境变量

CSS基础选择器

标签选择器&#xff1a;标签名 p{ font-size:16px;} 类选择器&#xff1a;.class { font-size:16px;} <标签名 class "类名称">标签内容</标签名> id选择器&#xff1a;#id { font-size:16px;} <标签名 id "id名称">标签内容</标签…