nginx

关注公众号 jb51net

关闭
首页 > 网站技巧 > 服务器 > nginx > Nginx七层负载均衡

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

作者:幸存者 · KXY

七层负载均衡它是在应用层,那么它可以完成很多应用方面的协议请求,本文主要介绍了Nginx七层负载均衡的实现示例,具有一定的参考价值,感兴趣的可以了解一下

七层负载均衡介绍

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

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

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

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

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七层负载均衡内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家! 

您可能感兴趣的文章:
阅读全文