ngin负载均衡集群(一)
一、nginx负载均衡集群介绍:
1.反向代理与负载均衡概念简介
严格地说, nginx仅仅是作为 Nginx Proxy反向代理使用的,因为这个反向代理功能表现的效果是负载均衡集群的效果,所以本文称之为nginx负载均衡。那么,反向代理和负载均衡有什么区别呢?
普通负载均衡软件,例如大名鼎鼎的LVS,其实现的功能只是对请求数据包的转发(也可能会改写数据包)、传递,其中DR模式明显的特征是从负载均衡下面的节点服务器来看,接收到的请求还是来自访问负载均衡器的客户端的真实用户,而反向代理就不样了,反向代理接收访问用户的请求后,会代理用户重新发起请求代理下的节点服务器,最后把数据返回给客户端用户,在节点服务器看来,访问的节点服务器的客户端用户就是反向代理服务器了,而非真实的网站访问用户。句话,LVS等的负载均衡是转发用户请求的数据包,而 nginx反向代理是接收用户的请求然后重新发起请求去请求其后面的节点。
2、实现负载均衡的组件说明:
实现负载均衡的组件主要有两个:
1 2 | ngx_http_proxy_module proxy代理模块,用于把请求后抛给服务器节点或upstream服务器池 ngx_http_upstream_module 负载均衡模块,可以实现网站的负载均衡功能及结点的健康检查 |
二、环境准备:
系统:CentOS Linux release 7.5.1804 (Core)
LB01 192.168.100.105 nginx主负载均衡器
LB02 192.168.100.106 nginx辅负载均衡器
Web01 192.168.100.107 Web01服务器
Web02 192.168.100.108 Web02服务器
nginx版本:1.8.1
三、安装nginx软件
在以上4台服务器上安装nginx
编译安装nginx请参考:https://www.cnblogs.com/Mr-Ding/p/9502529.html
nginx启动脚本参考:https://www.cnblogs.com/Mr-Ding/p/9502972.html
四、配置用于测试的web服务
nginx web01和web02配置如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | cat nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on ; keepalive_timeout 65; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"' ; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } access_log logs/access.log main; } } 重启服务: systemctl reload nginx web01下面写入: [root@web01 conf]# echo "192.168.100.107" > ../html/index.html web02下面写入: [root@web02 conf]# echo "192.168.100.108" > ../html/index.html 配置hosts: web01: [root@web01 conf]# tail -1 /etc/hosts 192.168.100.107 www.dmtest.com web02: [root@web02 conf]# tail -1 /etc/hosts 192.168.100.108 www.dmtest.com 测试: [root@web01 conf]# curl www.dmtest.com 192.168.100.107 [root@web02 conf]# curl www.dmtest.com 192.168.100.108 |
五、 实现一个简单的负载均衡
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | 在LB01上作如下操作: [root@lb01 conf]# cat nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on ; keepalive_timeout 65; upstream www_server_pools { #这里是定义web服务器池,包含了107和108两个web节点; server 192.168.100.107:80 weight=1; server 192.168.100.108:80 weight=1; } server { #这里是定义代理的负载均衡域名虚拟主机; listen 80; server_name www.dmtest.com; location / { proxy_pass http: //www_server_pools; #访问www.dmtest.com,请求发送给www_server_pools里面的节点; } } } 检查语法并重启nginx服务 [root@lb01 conf]# ../sbin/nginx -t nginx: the configuration file /application/nginx-1.8.1/conf/nginx.conf syntax is ok nginx: configuration file /application/nginx-1.8.1/conf/nginx.conf test is successful [root@lb01 conf]# systemctl restart nginx 测试: [root@lb01 conf]# tail -1 /etc/hosts 192.168.100.105 www.dmtest.com [root@lb01 conf]# curl www.dmtest.com 192.168.100.107 [root@lb01 conf]# curl www.dmtest.com 192.168.100.108 [root@lb01 conf]# curl www.dmtest.com 192.168.100.107 [root@lb01 conf]# curl www.dmtest.com 192.168.100.108 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY