Nginx七层负载均衡
作者:mmseoamin日期:2024-04-27

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"