Nginx 实现负载均衡

1. 负载均衡概念说明

1.1 什么是负载均衡

  1. 实现用户访问请求进行调度分配
  2. 实现用户访问压力分担

1.2 什么是反向代理

1.2.1 反向代理

外网(用户) ===> (外网网卡)代理服务器(内网网卡)===> 公司网站服务器Web(内网)

1.2.2 正向代理(翻墙)

内网(局域网主机) ===> (内网网卡)代理服务器(外网网卡) ===> 互联网 Web服务器

1.3 集群

1.3.1 什么是集群

完成相同任务或工作的一组服务器

1.3.2 特点

  1. 高性能
  2. 价格有效性
  3. 可伸缩性
  4. 高可用性
  5. 透明性

2. 负载均衡环境搭建

2.1 集群服务器部署

集群中每台服务器的配置 一模一样

  1. 企业中,先部署好一台 LNMP 服务器,上传代码信息
  2. 进行访问测试
  3. 批量部署多台 Web 服务器
  4. 将 Nginx 配置文件批量分发
  5. 将站点目录分发给所有主机

2.2 负载均衡服务器部署

2.2.1 安装 Nginx 软件

2.2.2 编写 Nginx 负载服务配置文件

  • Nginx 模块
# 模块01 
ngx_http_upstream_module
指令: upstream    -- 负载均衡 

# 模块02 
ngx_http_proxy_module
指令: proxy_pass  -- 反向代理
  • 编写配置文件 (lb.conf)
# 定义可将请求分配给哪些服务器 (upstream 配置在 http 区域)
upstream yunxuanedu {
    server 10.1.1.7:80;
    server 10.1.1.8:80;
    server 10.1.1.9:80;
}
# 将请求分配给指定的集群
server {
    listen  80;
    server_name www.yunxuanedu.com;
    location / {
        proxy_pass http://yunxuanedu;
    }
}

2.2.3 实现负载功能测试

2.2.4 抓包

image-20211230162632129.png

3. 负载均衡访问网站异常排错思路

  1. 在负载均衡服务器上,测试 Web 节点服务器访问是否正常
# 在命令行 指定 映射关系测试
[root@lb01 ~]# curl -H host:www.yunxuanedu.com 10.1.1.7/test001.html
www 172.16.1.7
[root@lb01 ~]# curl -H host:www.yunxuanedu.com 10.1.1.8/test001.html
www 172.16.1.8
[root@lb01 ~]# curl -H host:www.yunxuanedu.com 10.1.1.9/test001.html
www 172.16.1.9
  1. 在负载均衡服务器上,利用 curl 访问负载均衡服务器 是否正常

  2. 在本地测试 访问 负载均衡服务器是否正常

image-20211230163416629.png

4. 负载均衡配置模块详细说明

4.1 ngx_http_upstream_module (upstream)

4.1.1 实现不同调度功能

  1. 轮询分配请求(平均)
  2. 权重分配请求(能力越强责任越重)
upstream yunxuanedu {
    server 10.1.1.7:80  weight=3;
    server 10.1.1.8:80  weight=2;
    server 10.1.1.9:80  weight=1;
}
  1. 实现热备功能
# 9为热备,所有请求分发给7和8,只有7和8均无法提供服务,才使用9
upstream yunxuanedu {
    server 10.1.1.7:80;
    server 10.1.1.8:80;
    server 10.1.1.9:80  backup;
}
  1. 定义最大失败次数(健康检查参数)
# 默认次数为 1
upstream yunxuanedu {
    server 10.1.1.7:80  max_fails=5;
    server 10.1.1.8:80;
    server 10.1.1.9:80;
}
  1. 定义失败之后重发的间隔时间(健康检查参数)
upstream yunxuanedu {
    server 10.1.1.7:80  fail_timeout=10s;
    server 10.1.1.8:80;
    server 10.1.1.9:80;
}

4.1.2 实现不同调度算法

  1. rr 轮询调度算法

  2. wrr 权重调度算法

  3. ip_hash 算法(可避免 cookie 分发问题,导致闪退)

    ip_hash 会出现负载不均衡问题

    cookie 不一致,闪退。此外还可以使用缓存服务器保存 会话记录,使用 memcache

upstream yunxuanedu {
    ip_hash;
    server 10.1.1.7:80;
    server 10.1.1.8:80;
    server 10.1.1.9:80;
}
  1. least_conn (最小连接数)

    那台服务器 连接数较少,分配给谁

4.2 ngx_http_proxy_module (proxy_pass)

4.2.1 访问不同的网站地址,不能显示不同的网站页面(有旁站)

  • 问题现状
[root@lb01 conf.d]# curl -H host:www.yunxuanedu.com 10.1.1.7/test001.html
www 172.16.1.7
[root@lb01 conf.d]# curl -H host:www.yunxuanedu.com 10.1.1.8/test001.html
www 172.16.1.8
[root@lb01 conf.d]# curl -H host:www.yunxuanedu.com 10.1.1.9/test001.html
www 172.16.1.9
[root@lb01 conf.d]# curl -H host:www.yunxuanedu.com 10.1.1.5/test001.html
bbs 172.16.1.7
[root@lb01 conf.d]# curl -H host:www.yunxuanedu.com 10.1.1.5/test001.html
bbs 172.16.1.8
[root@lb01 conf.d]# curl -H host:www.yunxuanedu.com 10.1.1.5/test001.html
bbs 172.16.1.9
  • 解决方法
location / {
    proxy_pass http://yunxuanedu;
    proxy_set_header Host $host;
}

4.2.2 访问网站用户地址信息无法进行分析统计

  • 问题如下,日志记录全部为负载服务器地址
[root@web01 ~]# tail /var/log/nginx/access.log
……
10.1.1.5 - - [30/Dec/2021:16:20:22 +0800] "GET /test001.html HTTP/1.0" 200 15 "-" "curl/7.29.0" "-"
10.1.1.5 - - [30/Dec/2021:16:20:25 +0800] "GET /test001.html HTTP/1.0" 200 15 "-" "curl/7.29.0" "-"
10.1.1.5 - - [30/Dec/2021:16:25:28 +0800] "GET / HTTP/1.0" 200 28 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36 SLBrowser/7.0.0.6241 SLBChan/27" "-"
10.1.1.5 - - [30/Dec/2021:16:36:53 +0800] "GET /test001.html HTTP/1.1" 200 15 "-" "curl/7.29.0" "-"
  • 解决
location / {
    proxy_pass http://yunxuanedu;
    proxy_set_header X-Forwarded-For $remote_addr;
}

4.2.3 访问负载均衡,出现错误页面,影响用户体验

location / {
    proxy_pass http://yunxuanedu;
    proxy_next_upstream error timeout http_404 http_502 http_403;
}

4.3 lb.conf 文件内容

[root@lb01 conf.d]# cat lb.conf 
upstream yunxuanedu {
    server 10.1.1.7:80;
    server 10.1.1.8:80;
    server 10.1.1.9:80;
}
server {
    listen  80;
    server_name www.yunxuanedu.com;
    location / {
        proxy_pass http://yunxuanedu;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_next_upstream error timeout http_404 http_502 http_403;
    }
}
server {
    listen  80;
    server_name bbs.yunxuanedu.com;
    location / {
        proxy_pass http://yunxuanedu;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_next_upstream error timeout http_404 http_502 http_403;
    }
}
server {
    listen  80;
    server_name blog.yunxuanedu.com;
    location / {
        proxy_pass http://yunxuanedu;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_next_upstream error timeout http_404 http_502 http_403;
    }
}

5. 负载均衡企业实践应用

5.1 根据用户访问的uri信息进行负载均衡 (动静分离)

5.1.1 环境规划

用户请求(URI) 处理请求服务器 站点目录 功能
/upload 10.1.1.8:80 /html/www/upload upload 服务器集群
/static 10.1.1.7:80 /html/www/static static 静态服务器 集群
/ 10.1.1.9:80 /html/www 默认
  • 访问 URL 信息
www.yunxuanedu.com/test.html
www.yunxuanedu.com/upload/test.html
www.yunxuanedu.com/static/test.html

5.1.2 环境部署

  • 10.1.1.7
[root@web01 conf.d]# cat www.conf 
server {
    listen  80;
    server_name www.yunxuanedu.com yx.com;
    location / {
      root  /html/www;
      index test.html;
    }
}
[root@web01 ~]# systemctl reload nginx
[root@web01 ~]# cd /html/www/
[root@web01 www]# mkdir static
[root@web01 www]# cd static/
[root@web01 static]# echo "static-RAC-10.1.1.7" > test.html
[root@web01 static]# cat test.html 
static-RAC-10.1.1.7
  • 10.1.1.8
[root@web02 conf.d]# cat www.conf 
server {
    listen  80;
    server_name www.yunxuanedu.com yx.com;
    location / {
      root  /html/www;
      index test.html;
    }
}
[root@web02 ~]# systemctl reload nginx
[root@web02 ~]# cd /html/www
[root@web02 www]# mkdir upload
[root@web02 www]# cd upload/
[root@web02 upload]# echo "upload-RAC-10.1.1.8" > test.html
[root@web02 upload]# cat test.html 
upload-RAC-10.1.1.8
  • 10.1.1.9
[root@web03 conf.d]# cat www.conf 
server {
    listen  80;
    server_name www.yunxuanedu.com yx.com;
    location / {
      root  /html/www;
      index test.html;
    }
}
[root@web03 ~]# systemctl reload nginx
[root@web03 ~]# cd /html/www/
[root@web03 www]# echo "default-RAC-10.1.1.9" > test.html
[root@web03 www]# cat test.html 
default-RAC-10.1.1.9
  • 10.1.1.5 (负载均衡服务器)
[root@lb01 ~]# cd /etc/nginx/conf.d/
[root@lb01 conf.d]# ll
total 8
-rw-r--r-- 1 root root 1072 Nov 16 23:02 default.conf
-rw-r--r-- 1 root root  746 Dec 30 18:52 lb.conf
[root@lb01 conf.d]# cp lb.conf{,.bak01}
[root@lb01 conf.d]# vim lb.conf
[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]# cat lb.conf
upstream default {
    server 10.1.1.9:80;
}

upstream static {
    server 10.1.1.7:80;
}

upstream upload {
    server 10.1.1.8:80;
}
server {
    listen  80;
    server_name www.yunxuanedu.com;
    location / {
        proxy_pass http://default;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $remote_addr;
    }
    location /upload {
        proxy_pass http://upload;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $remote_addr;
    }
    location /static {
        proxy_pass http://static;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $remote_addr;
    }
}
[root@lb01 conf.d]# systemctl reload nginx

5.1.3 访问测试

5.1.4 总结 - 实现网站集群动静分离

  1. 提高网站服务的安全性
  2. 简化运维管理操作
  3. 可以划分不同人员管理不同的集群服务器

5.2 根据用户访问的终端信息进行负载均衡

5.2.1 环境规划

用户请求 处理请求服务器 站点目录 功能
iphone 10.1.1.7:80 /html/www 移动端 集群
google 浏览器 10.1.1.8:80 /html/www google 端访问集群
其他浏览器 10.1.1.9:80 /html/www default 集群

5.2.2 环境部署

  • 10.1.1.7
[root@web01 ~]# echo "Mobile-RAC-10.1.1.7" > /html/www/test.html
[root@web01 ~]# cat /html/www/test.html
Mobile-RAC-10.1.1.7
  • 10.1.1.8
[root@web02 ~]# echo "Google-RAC-10.1.1.8" > /html/www/test.html
[root@web02 ~]# cat /html/www/test.html
Google-RAC-10.1.1.8
  • 10.1.1.9
[root@web03 ~]# echo "Default-RAC-10.1.1.9" > /html/www/test.html
[root@web03 ~]# cat /html/www/test.html
Default-RAC-10.1.1.9
  • 10.1.1.5 (负载均衡服务器)
[root@lb01 conf.d]# vim lb.conf
[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 reload nginx
[root@lb01 conf.d]# cat lb.conf
upstream mobile {
    server 10.1.1.7:80;
}

upstream google {
    server 10.1.1.8:80;
}

upstream default {
    server 10.1.1.9:80;
}

server {
    listen  80;
    server_name www.yunxuanedu.com;
    location / {
        if ($http_user_agent ~* iphone) {
            proxy_pass  http://mobile;
        }
        if ($http_user_agent ~* Chrome) {
            proxy_pass  http://google;
        }
        proxy_pass http://default;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $remote_addr;
    }
}

5.2.3 访问测试

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,509评论 6 504
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,806评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,875评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,441评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,488评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,365评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,190评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,062评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,500评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,706评论 3 335
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,834评论 1 347
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,559评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,167评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,779评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,912评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,958评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,779评论 2 354

推荐阅读更多精彩内容