Linux中web集群-nginx负载均衡及案例

概述

代理:外卖,中介,中间商,用户无法直接做事情,通过中介进行处理

用户--》代理--》节点,后面只有一个节点,一般使用的是nginx代理功能即可,如果是集群就需要使用nginx负载均衡

代理分类

本质:根据用户位置(定是否那个为正反向代理)

正向代理:用户(服务器)-》代理-》外部(网站)服务器通过代理实现共享上网/某一个网站

反向代理:用户(app/浏览器)-》代理-》网站服务器(WEB) 给网站设置一个统一的入口,可以进行使用负载均衡的功能进行使用

环境概述

代理:web lb01 10.0.0.5/172.16.1.5

web web01 10.0.0.7/172.16.1.7

域名proc.wulinlinux.cn

站点目录:/app/code/proxy/index.html

建议开启db01和nfs01

bash 复制代码
[root@web01 ~]#vim /etc/nginx/conf.d/proxy.wulinlinux.cn.conf

检查语法:

root@web01 \~\]#nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful systemctl reload nginx \[root@web01 \~\]#mkdir -p /app/code/proxy/ \[root@web01 \~\]#echo proxy.wulinlinux.cn \>/app/code/proxy/index.html 测试: ![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/91b959fb9dce4da39482ff98e76b1372.png) ```bash [root@lb01 ~]#vim /etc/nginx/conf.d/proxy.wulinlinux.cn.conf ``` ![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/79688565499c48d88c05f2e8712bdebd.png) ![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/fd22924cf0764670b58942bd15c357c5.png) ![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/ded357291bc249e984ea8e3695809a7a.png) 测试负载: ![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/e129bcebeb634f1db24687d8663070f0.png) 保存 ![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/152d8b53feb04b439eeff886a8ec543e.png) ![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/93538bdfa66746d7a4a89f744385b6ad.png) ![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/0aae7a61e6dc424ba63c5dd4b576283a.png) ![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/d4ecad571b8a45b2bdab916a178f4810.png) ## web集群-nginx-负载均衡 ### 案例:部署nginxconf站点并进行访问 | lb01 192.168.65.5/172.16.1.5 | | |-------------------------------|---| | 环境要求 说明 | | | web01 192.168.65.7/172.16.1.7 | | | web02 192.168.65.8/172.16.1.8 | | | 域名:conf.wulinlinux.cn | | | 站点目录:/app/code/cfg/ | | #### web服务器准备 ```go [root@web01 ~]#vim /etc/nginx/conf.d/cfg.wulinlinux.cn.conf ``` ![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/3dc53e04d4ac46609f9ba9ba1be5fc46.png) 创建站点目录: ```bash [root@web01 ~]#mkdir -p /app/code/cfg/ ``` 传输部署网站的代码 ![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/5472d0f0db4f4917acd35222e975a30b.png) 解压传输的部署网站代码(解压进行查看是否有嵌套的代码文件目录) ![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/24b4b99fe33a40a0b557329211ac2a5b.png) 解压到站点目录 ```bash [root@web01 ~]#unzip star-battle-master.zip -d /app/code/cfg/ ``` 把下面所有的东西移动到相对应的站点目录里面 ![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/96873b2d334f4aee8ee16f3ab01bfb76.png) ```bash#检查语法并且进行重启nginx服务 [root@web01 /app/code/cfg]#nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@web01 /app/code/cfg]#systemctl reload nginx 在这里插入代码片 ``` 打开web02发现没有nginx等源 进入web01服务器进行传输到web02服务器上 ![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/314d612a6ec6431cbdbcce73ba49f6e9.png) ![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/b087e36e8ad7449ba7b091b5e7b8f666.png)![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/3ffb8828173b474eac83946c67501303.png) ![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/fdd45992185d4455907779a183365dbc.png) \[root@web01 /etc/nginx/conf.d\]#cd /app/ \[root@web01 /app\]#scp -r /app/ web02:/ ssh: connect to host web02 port 22: Connection timed out lost connection \[root@web01 /app\]#scp -r /app/ 192.168.65.8:/ \[root@web02 \~\]#systemctl enable nginx Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service. \[root@web02 \~\]#systemctl start nginx ![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/31d4763b97964d50b06cfe8bb65e806c.png) ![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/2843c708d95a48b7b2507f2aa4527276.png) ## 负载均衡配置 * load balance(lb) * 创建池塘进行分组,存放主机 * upstream 与server是并列的 * 名字要进行保持一致 * upstream创建池塘,proxy_pass将数据传给池塘 ```bash [root@lb01 /etc/nginx/conf.d]#cat cfg.wulinlinux.cn upstream cfg_pols { server 192.168.65.7:80; server 192.168.65.8:80; } server { listen 80; server_name cfg.wulinlinux.cn; error_blog /var/log/nginx/cfg-error.blog notice; access_blog /var/log/nginx/cfg-access.log main; location / { # proxy_pass http://?????; proxy_pass http://cfg_pols; proxy_set_header Host $http_host; proxy_set_header X-Real-Ip $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } ``` ![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/8edd58c2d918493c939fa56647548b75.png) \[root@lb01 /etc/nginx/conf.d\]#curl -H Host:cfg.wulinlinux.cn http://192.168.65.5/ 会发现这里返回的结果是web01与web02服务器交换进行:(负载均衡---平均分配) ## 总结 * upstream模块的upstream指令; * \[root@lb01 /etc/nginx/conf.d\]#cat cfg.wulinlinux.cn upstream cfg_pols { server 192.168.65.7:80; server 192.168.65.8:80; } server { listen 80; server_name cfg.wulinlinux.cn; error_blog /var/log/nginx/cfg-error.blog notice; access_blog /var/log/nginx/cfg-access.log main; location / { proxy_pass http://???; ​ proxy_pass http://cfg_pols; ​ proxy_set_header Host $http_host; ​ proxy_set_header X-Real-Ip $remote_addr; ​ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } * lnmp深入原理及排查故障 * 1.代理基础功能把请求通过代理转发到某一台web节点上 * 2.转发的时候记录真实的ip * 3.负载均衡基本使用

相关推荐
飞龙AI2 分钟前
鸿蒙Next实现瀑布流布局
前端
快起来别睡了3 分钟前
代理模式:送花风波
前端·javascript·架构
斗转星移35 分钟前
解决ubuntu20.04无法唤醒的问题的一种方法
linux·ubuntu·电脑
海底火旺5 分钟前
电影应用开发:从代码细节到用户体验优化
前端·css·html
陈随易14 分钟前
Gitea v1.24.0发布,自建github神器
前端·后端·程序员
前端付豪17 分钟前
汇丰银行技术架构揭秘:全球交易稳定背后的“微服务+容灾+零信任安全体系”
前端·后端·架构
邹荣乐20 分钟前
uni-app开发微信小程序的报错[渲染层错误]排查及解决
前端·微信小程序·uni-app
今天出摊吗20 分钟前
表单元素的默认提交行为
前端
今天出摊吗21 分钟前
CSS 动态视口单位 dvh 和 dvw
前端
南方kenny22 分钟前
React组件化实战:从零打造智能TodoList清单
前端·react.js·aigc