10分钟搞定nginx实现负载均衡

10.1 负载均衡的概念

  • 对用户请求的数据进行调度的作用
  • 对用户访问的请求网站可以进行压力的分担

10.2 常见的代理方式

10.2.1 正向代理

10分钟搞定nginx实现负载均衡

10.2.2 反向代理

10分钟搞定nginx实现负载均衡

10.3 负载均衡的部署环节

10.3.1 服务器的准备

lb01 服务器:172.16.1.5

web01 服务器:172.16.1.7

web02 服务器:172.16.1.8

10.3.2 服务器环境的准备

10.3.2.1 web 服务器的配置(172.16.1.7,172.16.1.8)

[root@web02 ~] # cd /etc/nginx/conf.d/

[root@web02 conf.d] # vim www.conf

server {

listen 80;

server_name www.oldboy.com;

location / {

root /html/www;

index index.html index.htm;

}

}

[root@web01 conf.d] # cd /html/ 创建站点目录

[root@web01 html] # mkdir -p www

[root@web01 html] # cd www

[root@web01 www] # echo “$(hostname -i) www.oldboy.com” > index.html

[root@web01 www] # systemctl restart nginx

10.3.2.1.2 出现启动失败
  • 查看nginx -t查看语法是否出错
  • 查看/etc/nginx/nginx.conf看下是不是include没有加载www.conf导致的
10.3.2.3 web 服务器测试是否成功

[root@web01 www] # curl 10.0.0.7

172.16.1.7 www.oldboy.com 已经成功

[root@web02 www] # curl 10.0.0.8

172.16.1.8 www.oldboy.com

10.3.2.2 lb01 服务器的配置

10.3.2.2.1 安装nginx,并且查看版本是不是最新版本

[root@lb01 ~] # yum -y install nginx 安装nginx

[root@lb01 ~] # nginx -V

nginx version: nginx/1.16.1 版本信息

10.3.2.2.2 编辑负载均衡配置文件

[root@lb01 ~] # cd /etc/nginx/conf.d/

[root@lb01 conf.d] # vim www.conf 编辑配置文件

upstream oldboy { 设置集群名称

server 172.16.1.7:80; 指定具体的集群中的服务器主机信息

server 172.16.1.8:80;

}

server {

listen 80;

server_name localhost;

location / {

proxy_pass http://oldboy; 使用http 协议来进行集群中的服务器代理

}

}

[root@lb01 conf.d] # nginx -t 检查语法是

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@lb01 conf.d] # systemctl restart nginx 重启nginx 服务

10.3.2.2.3 查看代理是否成功(linux界面)

[root@lb01 conf.d] # curl 10.0.0.7

172.16.1.7 www.oldboy.com

[root@lb01 conf.d] # curl 10.0.0.8

172.16.1.8 www.oldboy.com

10.3.2.2.4 查看代理是否成功(浏览器界面)

10分钟搞定nginx实现负载均衡

10分钟搞定nginx实现负载均衡

10.4 nginx 实现负载均衡模块

10.4.1 按照比列进行分配(默认不加指令信息为1:1)

10.4.1.1 应用场景

多台web服务器性能不同的时候使用

10.4.1.2 weight 指定的使用

[root@lb01 conf.d] # vim www.conf

upstream oldboy {

server 172.16.1.7:80 weight=4; 2

server 172.16.1.8:80 weight=2; 1

}

server {

listen 80;

server_name localhost;

location / {

proxy_pass http://oldboy;

}

}

[root@lb01 conf.d] # nginx -t 检查语法是否正确

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@lb01 conf.d] # systemctl restart nginx 重启nginx

10.4.1.3 查看是否分配成功

[root@lb01 conf.d] # curl 10.0.0.5

172.16.1.8 www.oldboy.com 一次8

[root@lb01 conf.d] # curl 10.0.0.5

172.16.1.7 www.oldboy.com 2 7

[root@lb01 conf.d] # curl 10.0.0.5

172.16.1.7 www.oldboy.com 2 次7

10.4.2 负载均衡健康检查功能

10.4.2.1 应用场景

应用在某台服务器出现故障不能进行处理前端发过来的请求的时候

10.4.2.2 指令的使用

[root@lb01 conf.d] # vim www.conf

upstream oldboy {

server 172.16.1.7:80;

server 172.16.1.8:80 max_fails=3 fail_timeout=60s; 设置最大失败次数为3 次,超时时间为60s

}

server {

listen 80;

server_name localhost;

location / {

proxy_pass http://oldboy;

}

}

10.4.2.3 查看是否成功

10.4.2.3.1 172.16.1.8服务器nginx停止

[root@web02 www] # systemctl stop nginx

10.4.2.3.2 查看现在的现象

[root@lb01 conf.d] # curl 10.0.0.5 只会给7 分配

172.16.1.7 www.oldboy.com

[root@lb01 conf.d] # curl 10.0.0.5

172.16.1.7 www.oldboy.com

[root@lb01 conf.d] # curl 10.0.0.5

172.16.1.7 www.oldboy.com

[root@lb01 conf.d] # curl 10.0.0.5

172.16.1.7 www.oldboy.com

[root@lb01 conf.d] # curl 10.0.0.5

172.16.1.7 www.oldboy.com

[root@lb01 conf.d] # curl 10.0.0.5

172.16.1.7 www.oldboy.com

[root@lb01 conf.d] # curl 10.0.0.5

^[[A172.16.1.7 www.oldboy.com

[root@lb01 conf.d] # curl 10.0.0.5

172.16.1.7 www.oldboy.com

10.4.2.3.3 将172.16.1.8开启

[root@web02 www] # systemctl start nginx

10.4.2.3.4 再次查看

[root@lb01 conf.d] # curl 10.0.0.5 发现7,8 都会分配

172.16.1.8 www.oldboy.com

[root@lb01 conf.d] # curl 10.0.0.5

172.16.1.7 www.oldboy.com

[root@lb01 conf.d] # curl 10.0.0.5

172.16.1.8 www.oldboy.com

[root@lb01 conf.d] # curl 10.0.0.5

172.16.1.7 www.oldboy.com

[root@lb01 conf.d] #

10.4.3 热备的使用

10.4.3.1 应用场景

正常的服务器出现了问题,热备服务器就会工作,正常服务器好了,热备就不需要了

  • 上面的理解可能不好懂
  • 可以这样理解,男生和女生谈恋爱,女孩子可能有备胎,在他和她的现任出现了问题以后,备胎就会启动作用,来继续维持,当男朋友和她和好了,备胎自然就不需要了,可以去歇歇了

10.4.3.2 backup 指令的使用

[root@lb01 conf.d] # vim www.conf

upstream oldboy {

server 172.16.1.7:80

server 172.16.1.8:80 backup; 假如8 服务器当做热备

}

server {

listen 80;

server_name localhost;

location / {

proxy_pass http://oldboy;

}

}

[root@lb01 conf.d] # nginx -t 检查语法是否正确

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@lb01 conf.d] # systemctl restart nginx 重启nginx

10.4.3.3 查看是否成功

[root@lb01 conf.d] # curl 10.0.0.5 现在只有7 在工作,只有7 坏了,8 才会工作

172.16.1.7 www.oldboy.com

[root@lb01 conf.d] # curl 10.0.0.5

172.16.1.7 www.oldboy.com

[root@lb01 conf.d] # curl 10.0.0.5

10.4.4 按照连接数进行分配

10.4.4.1 应用场景

企业中出现某台服务器的负载压力一直很高的情况下

10.4.4.2 指令信息的使用

[root@lb01 conf.d] # vim www.conf

upstream oldboy {

least_conn; 按照连接数进行分配

server 172.16.1.7:80;

server 172.16.1.8:80;

}

server {

listen 80;

server_name localhost;

location / {

proxy_pass http://oldboy;

}

}

10.4.5 按照 ip_hash 数值进行负载均衡

10.4.5.1 应用场景

出现在web网站界面反复登录的情况

指定某个客户端访问某个web服务器

10.4.5.2 指令 ip_hash 的使用

[root@lb01 conf.d] # vim www.conf

upstream oldboy {

ip_hash;

server 172.16.1.7:80;

server 172.16.1.8:80;

}

server {

listen 80;

server_name localhost;

location / {

proxy_pass http://oldboy;

}

}

10.5 通过负载均衡实现访问不同的界面信息

10.5.1 web 服务器的准备(172.16.1.7,172.16.1.8)

[root@web01 conf.d] # cat *.conf

server {

listen 80;

server_name bbs.oldboy.com; bbs 网站的配置

location / {

root /html/bbs;

index index.html index.htm;

}

}

server {

listen 80;

server_name blog.oldboy.com; blog 网站的配置

location / {

root /html/blog;

index index.html index.htm;

}

}

server {

listen 80;

server_name www.oldboy.com; www 网站的配置

location / {

root /html/www;

index index.html index.htm;

}

}

[root@web01 conf.d] #

[root@web01 conf.d] # cd /html/

[root@web01 html] # mkdir -p blog bbs 创建bbs,blog 站点目录

[root@web01 html] # cd /html/blog/

[root@web01 blog] # echo “$(hostname -i) blog.oldboy.com” > index.html 添加内容

[root@web01 blog] # cd ..

[root@web01 html] # cd bbs

[root@web01 bbs] # echo “$(hostname -i) bbs.oldboy.com” > index.html 添加内容

[root@web01 bbs] # cd ../

[root@web01 html] # cd www

[root@web01 www] # echo “$(hostname -i) www.oldboy.com” > index.html 添加内容

[root@web01 www] #

[root@web01 nginx] # systemctl restart nginx 重启nginx

[root@web01 nginx] # nginx -t 检查nginx 语法

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@web01 nginx] #

10.5.2 负载均衡的验证

10分钟搞定nginx实现负载均衡
  • 发现不管使用什么域名访问只会出现www站点的内容信息

10.5.3 出现这种问题的原因

10.5.3.1 使用 wireshark 抓 http 包分析

10.5.3.1.1 抓包工具的使用

10分钟搞定nginx实现负载均衡
10.5.3.1.2 使用vmnat8口来进行抓包

10分钟搞定nginx实现负载均衡
10.5.3.1.3 选择http进行抓包

10分钟搞定nginx实现负载均衡
10.5.3.1.4 开始分析http包

10分钟搞定nginx实现负载均衡

10.5.4 解决办法

[root@lb01 conf.d] # vim www.conf

upstream oldboy {

server 172.16.1.7:80;

server 172.16.1.8:80 max_fails=3 fail_timeout=60s;

}

server {

listen 80;

server_name localhost;

location / {

proxy_pass http://oldboy;

proxy_set_header Host $host; 设置不同域名可以通过负载均衡访问不同网站的内容

}

}

[root@lb01 conf.d] # systemctl restart nginx 重启nginx 服务

[root@lb01 conf.d] # nginx -t 检查nginx 语法的正确性

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@lb01 conf.d] #

10.5.5 再次验证是否成功

10分钟搞定nginx实现负载均衡

10分钟搞定nginx实现负载均衡

10.6 实现日志可以知道是哪位客户端访问的日志信息

10.6.1 目前的现象

172.16.1.5 – – [02/Dec/2019:13:39:43 +0800] “GET / HTTP/1.0” 200 26 “-” “Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36” “-“

  • *上述发现只能知道是负载均衡分配过来的信息,不知道客户端是谁

10.6.2 问题的原因

出现此类问题是因为负载均衡服务器没有将客户端的访问地址告诉web服务器造成的

10.6.3 问题的解决

[root@lb01 conf.d] # vim www.conf

upstream oldboy {

server 172.16.1.7:80;

server 172.16.1.8:80 max_fails=3 fail_timeout=60s;

}

server {

listen 80;

server_name localhost;

location / {

proxy_pass http://oldboy;

proxy_set_header Host $host;

proxy_set_header X-Forwarded-For $remote_addr; 加入远程需要添加的代理信息

}

}

10.6.4 再次查看

172.16.1.5 – – [02/Dec/2019:13:52:01 +0800] “POST /wp-admin/admin-ajax.php HTTP/1.0” 404 555 “http://blog.oldboy.com/wp-admin/post.php?post=5&action=edit” “Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36″ ” 10.0.0.1成功

10.7 页面上出现错误怎么健康检查

[root@lb01 conf.d] # vim www.conf

upstream oldboy {

server 172.16.1.7:80;

server 172.16.1.8:80 max_fails=3 fail_timeout=60s;

}

server {

listen 80;

server_name localhost;

location / {

proxy_pass http://oldboy;

proxy_set_header Host $host;

proxy_set_header X-Forwarded-For $remote_addr;

proxy_next_upstream error timeout invalid_header http_403 http_502; 设置上出现错误代码的指令信息

}

}

10.8 负载均衡实现动静分离

10.8.1 环境的准备

10.8.1.1 172.16.1.7 环境的准备

[root@web01 ~] # cd /html/www/

[root@web01 www] # ll

total 4

-rw-r–r– 1 root root 26 Dec 2 11:12 index.html

[root@web01 www] # echo “static.oldboy.com” > index.html 添加测试静态数据

10.8.1.2 172.16.1.8 环境的准备

[root@web02 ~] # cd /html/www/

You have new mail in /var/spool/mail/root

[root@web02 www] # ll

total 4

-rw-r–r– 1 root root 26 Dec 2 11:08 index.html

[root@web02 www] # echo “dynamic oldboy.com” > index.html 添加测试动态数据

10.8.1.3 负载均衡的环境的准备

[root@lb01 conf.d] # vim www.conf

upstream static_oldboy { 创建静态集群

server 172.16.1.7:80;

}

upstream dynamic_oldboy { 创建动态集群

server 172.16.1.8:80;

}

server {

listen 80;

server_name localhost;

location ~ /static_oldboy { 代理静态网站*

proxy_pass http://static_oldboy;

proxy_set_header Host $host;

proxy_set_header X-Forwarded-For $remote_addr;

}

location ~* /dynamic_oldboy {

proxy_pass http://dynamic_oldboy; 代理动态网站

proxy_set_header Host $host;

proxy_set_header X-Forwarded-For $remote_addr;

}

}

[root@lb01 conf.d] # nginx -t 检查语法信息

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@lb01 conf.d] # systemctl restart nginx 重启nginx 服务

[root@lb01 conf.d] #

10.8.2 查看是否成功

10.9 负载均衡实现访问不同的终端显示不一样的 H5 界面

10.9.1 环境的准备

10.9.1.1 172.16.1.7(iphone) 的环境准备

[root@web01 ~] # cd /html/www/

[root@web01 www] # echo “iphone.oldboy.com” > index.html 添加测试是手机端过来的数据信息

You have new mail in /var/spool/mail/root

[root@web01 www] #

10.9.1.2 172.16.1.8( 谷歌 ) 的环境准备

[root@web02 ~] # cd /html/www/

[root@web02 www] # echo “pc.oldboy.com” > index.html 添加测试是电脑发送过来的数据

[root@web02 www] #

10.9.1.3 负载均衡的配置

[root@lb01 conf.d] # vim www.conf

upstream iphone {

server 172.16.1.7:80;

}

upstream pc {

server 172.16.1.8:80;

}

server {

listen 80;

server_name localhost;

location / {

if ($http_user_agent ~ iphone){ 看终端是否匹配iphone*

proxy_pass http://iphone;

}

proxy_pass http://pc; 不匹配iphone 剩下的匹配电脑

proxy_set_header Host $host;

proxy_set_header X-Forwarded-For $remote_addr;

}

}

  • $http_user_agent这个是判断客户终端信息使用的是什么类型,后面匹配的具体值,不能随便写,需要在日记中去看看

172.16.1.5 – – [02/Dec/2019:15:24:22 +0800] “GET / HTTP/1.0” 200 18 “-” “Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1″ “10.0.0.1”

  • 上面的日志我们可以看到我现在使用的是iphone来进行访问的

10.9.2 查看最终的结果

10.9.2.1 iphone 访问的结果

10分钟搞定nginx实现负载均衡

10.9.2.2 pc 端访问的结果

10分钟搞定nginx实现负载均衡

Original: https://www.cnblogs.com/liangyuxing/p/11971296.html
Author: HXX-LYX
Title: 10分钟搞定nginx实现负载均衡

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/596664/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球