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

 更新时间:2024年04月24日 08:52:49   作者:幸存者 · KXY  
七层负载均衡它是在应用层,那么它可以完成很多应用方面的协议请求,本文主要介绍了Nginx七层负载均衡的实现示例,具有一定的参考价值,感兴趣的可以了解一下

七层负载均衡介绍

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

相关文章

  • 解决nginx+lua搭建文件上传下载服务问题

    解决nginx+lua搭建文件上传下载服务问题

    这篇文章主要介绍了nginx+lua搭建文件上传下载服务,涉及到nginx安装配置方法,本文通过代码给大家介绍的非常详细,需要的朋友可以参考下
    2017-12-12
  • Nginx配置如何区分PC或手机访问不同域名

    Nginx配置如何区分PC或手机访问不同域名

    这篇文章主要介绍了Nginx配置如何区分PC或手机访问不同域名,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-10-10
  • 利用Nginx反向代理功能解决WEB网站80端口被封的解决方法

    利用Nginx反向代理功能解决WEB网站80端口被封的解决方法

    大陆的网络环境,都在天朝神兽的制度下让我等小P民悲剧一片;动不动就拔网线、封机房;现在更厉害的一招,从网关封杀你的80端口,一旦被封,网站域名就无法访问
    2012-08-08
  • 一文了解nginx HTTP安全响应问题

    一文了解nginx HTTP安全响应问题

    一些网站系统会经常遭到各类XSS攻击、点劫持等,从而造成重要信息的泄露以及服务器安全问题,本文就来介绍一下,感兴趣的可以了解一下
    2023-11-11
  • Nginx本地目录映射实现代码实例

    Nginx本地目录映射实现代码实例

    这篇文章主要介绍了Nginx本地目录映射实现代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-10-10
  • 详解Nginx 虚拟主机配置的三种方式(基于IP)

    详解Nginx 虚拟主机配置的三种方式(基于IP)

    Nginx配置虚拟主机支持3种方式主要有基于IP的虚拟主机配置,基于端口的虚拟主机配置,基于域名的虚拟主机配置。本文主要介绍了基于IP配置的实现,感兴趣的小伙伴们可以参考一下
    2018-10-10
  • Nginx出现403错误,应该如何解决

    Nginx出现403错误,应该如何解决

    这篇文章主要介绍了Nginx出现403错误,应该如何解决?具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-03-03
  • nginx作grpc的反向代理踩坑总结

    nginx作grpc的反向代理踩坑总结

    nginx是一款高性能的web服务器,常用于负载均衡和反向代理,本文主要介绍了nginx作grpc的反向代理踩坑总结,感兴趣的可以了解一下
    2021-07-07
  • nginx add_header指令使用方法

    nginx add_header指令使用方法

    这篇文章主要介绍了nginx add_header指令使用方法,nginx配置文件通过使用add_header指令来设置response header,需要的朋友可以参考下
    2014-03-03
  • nginx location 配置 正则表达式实例详解

    nginx location 配置 正则表达式实例详解

    本文通过实例代码给大家介绍了nginx location 配置 正则表达式的问题,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-07-07

最新评论