当前位置: 代码网 > 服务器>服务器>Nginx > Nginx七层负载均衡的实现示例

Nginx七层负载均衡的实现示例

2024年05月26日 Nginx 我要评论
七层负载均衡介绍nginx七层负载均衡是在应用层(http/https)上进行的,可以根据http请求的具体内容,如url、cookie、header等,来决定将请求转发到哪个后端服务器。这种方式不仅

七层负载均衡介绍

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

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

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

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

1.1 四层负载均衡(layer 4 load balancing)

  • 在传输层(transport layer)上进行。

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

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

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

  • 主要适用于基于tcp/udp的流量,如http和https。

1.2 七层负载均衡(layer 7 load balancing)

  • 在应用层(application layer)上进行。

  • 深入检查网络流量的内容,如http请求头、url、cookie等。

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

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

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

2.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

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

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

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就行了

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

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"

到此这篇关于nginx七层负载均衡的实现示例的文章就介绍到这了,更多相关nginx七层负载均衡内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网! 

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com