【我的技术我做主】nginx反向代理负载均衡群集实战

本文涉及的产品
传统型负载均衡 CLB,每月750个小时 15LCU
应用型负载均衡 ALB,每月750个小时 15LCU
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
简介:

使用nginx做负载均衡器,代理web服务器,用户请求的数据都指向nginx负载均衡器,nginx负责调度后端的web服务器提供服务。


环境搭建及说明:

nginx负载均衡器LNMP环境或只安装nginx服务;两块网卡,192.168.11.30(模拟公网ip),192.168.20.30(内网)

web服务器LAMP环境1:ip地址为内网 192.168.20.10 apache为2.4版本

web服务器LAMP环境2:ip地址为内网 192.168.20.11

web服务器LAMP环境3:ip地址为内网 192.168.20.12

三台web服务器网站目录,程序保持一致。内网ip保持与nginx负载均衡器同一个网段。


1、只有一个站点的配置:

web1、web2、web3上操作:

httpd配置文件加入,允许网站目录访问;开启vhost虚拟配置文件。

1
2
3
4
5
6
7
# vi /etc/httpd/httpd.conf
Include  /etc/httpd/extra/httpd-vhosts .conf
<Directory  /data/www >
     Options FollowSymLinks
     AllowOverride none
     Require all granted
< /Directory >

虚拟主机配置

1
2
3
4
5
# vi /etc/httpd/extra/httpd-vhosts.conf
<VirtualHost *:80>
     DocumentRoot  "/data/www"
     ServerName  www.yong.com
< /VirtualHost >

创建目录,写入index.html文件区分。

mkdir /data/www

在每一个web目录下写入index.html,内容分别为:This is LAMP 1 !;This is LAMP 2!;This is LAMP 3!

1
2
3
4
5
6
# curl 192.168.20.10
This is LAMP 1 !
# curl 192.168.20.11
This is LAMP 2!
# curl 192.168.20.12
This is LAMP 3!

nginx代理服务器操作:

写一个代理配置文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# cat /usr/local/nginx/conf/vhosts/nginxproxy.conf
upstream backend {
    server 192.168.20.10:80 weight=1 max_fails=3 fail_timeout=30s;
    server 192.168.20.11:80 weight=1 max_fails=3 fail_timeout=30s;
    server 192.168.20.12:80 weight=1 max_fails=3 fail_timeout=30s;
}
server {
    listen 80;
    server_name www.yong.com;
    index index.html;
    location / {
     proxy_pass http: //backend ;
   }
}


hosts添加本地ip地址解析,模拟公网ip对应域名;windows本地hosts也要增加解析;

1
2
# cat /etc/hosts
192.168.11.30 www.yong.com

使用curl测试,默认rr轮询,访问一次web1,一次web2,一次web3

使用for循环执行,查看访问结果:

1
2
3
4
5
6
7
8
9
10
11
# for n in `seq 10`;do curl www.yong.com;sleep 2;done
This is LAMP 2!
This is LAMP 1 !
This is LAMP 3!
This is LAMP 2!
This is LAMP 1 !
This is LAMP 3!
This is LAMP 2!
This is LAMP 1 !
This is LAMP 3!
This is LAMP 2!


2、多个站点的配置:

在web1,web2,web3,增加第2个站点 bbs.yong.com

httpd配置文件加入,允许网站目录访问;

1
2
3
4
5
<Directory  /data/bbs >
     Options FollowSymLinks
     AllowOverride none
     Require all granted
< /Directory >

虚拟主机配置增加

1
2
3
4
5
# vi /etc/httpd/extra/httpd-vhosts.conf
<VirtualHost *:80>
     DocumentRoot  "/data/bbs"
     ServerName bbs.yong.com
< /VirtualHost >

创建目录,写入index.html文件区分。

mkdir /data/bbs

在每一个web目录下写入index.html,内容分别为:This is BBS.yong.com 1!;This is BBS.yong.com 2!;This is BBS.yong.com 3!


nginx负载均衡服务器,虚拟主机增加server:

1
2
3
4
5
6
7
8
9
10
server {
     listen 80;
     server_name bbs.yong.com;
     index index.html;
     location / {
         proxy_pass http: //backend ;
         proxy_set_header Host $host;
         proxy_set_header X-Forwarded-For $remote_addr;
     }
}

hosts添加本地ip地址解析,模拟公网ip对应域名;windows本地hosts也要增加解析;

1
2
# cat /etc/hosts
192.168.11.30 www.yong.com bbs.yong.com


使用for循环执行,查看访问结果:

1
2
3
4
5
6
7
8
9
10
11
# for n in `seq 10`;do curl bbs.yong.com;sleep 2;done
This is BBS.yong.com 1!
This is BBS.yong.com 2!
This is BBS.yong.com 3!
This is BBS.yong.com 1!
This is BBS.yong.com 2!
This is BBS.yong.com 3!
This is BBS.yong.com 1!
This is BBS.yong.com 2!
This is BBS.yong.com 3!
This is BBS.yong.com 1!


3、upstream 下面增加ip_hash;

测试结果如下:保持用户连接,也会导致分配不均。

1
2
3
4
5
6
7
8
9
10
11
# for n in `seq 10`;do curl bbs.yong.com;sleep 2;done
This is BBS.yong.com 3!
This is BBS.yong.com 3!
This is BBS.yong.com 3!
This is BBS.yong.com 3!
This is BBS.yong.com 3!
This is BBS.yong.com 3!
This is BBS.yong.com 3!
This is BBS.yong.com 3!
This is BBS.yong.com 3!
This is BBS.yong.com 3!


4、增加一个upstream,单独针对bbs的请求进行负载均衡,并设置权重,配置文件如下:

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
upstream backend {
     server 192.168.20.10:80 weight=2 max_fails=3 fail_timeout=30s;
     server 192.168.20.11:80 weight=1 max_fails=3 fail_timeout=30s;
}
upstream bbs {
     server 192.168.20.11:80 weight=1 max_fails=3 fail_timeout=30s;
     server 192.168.20.12:80 weight=2 max_fails=3 fail_timeout=30s;
}
server {
     listen 80;
     server_name www.yong.com;
     index index.html;
     location / {
         proxy_pass http: //backend ;
     }
}
server {
     listen 80;
     server_name bbs.yong.com;
     index index.html;
     location / {
         proxy_pass http: //bbs ;
         proxy_set_header Host $host;
         proxy_set_header X-Forwarded-For $remote_addr;
     }
}

实验结果如下

1
2
3
4
5
6
7
8
9
10
11
# for n in `seq 10`;do curl bbs.yong.com;sleep 2;done
This is BBS.yong.com 2!
This is BBS.yong.com 3!
This is BBS.yong.com 3!
This is BBS.yong.com 2!
This is BBS.yong.com 3!
This is BBS.yong.com 3!
This is BBS.yong.com 2!
This is BBS.yong.com 3!
This is BBS.yong.com 3!
This is BBS.yong.com 2!
1
2
3
4
5
6
7
8
9
10
11
# for n in `seq 10`;do curl www.yong.com;sleep 2;done
This is LAMP 2!
This is LAMP 1 !
This is LAMP 1 !
This is LAMP 2!
This is LAMP 1 !
This is LAMP 1 !
This is LAMP 2!
This is LAMP 1 !
This is LAMP 1 !
This is LAMP 2!

同理,如果有多台服务器,多个站点可以进行分配、关联对应。


51cto十周年博客活动正在进行,你也来参加吧

   活动地址http://51ctoblog.blog.51cto.com/26414/1679643






本文转自 模范生 51CTO博客,原文链接:http://blog.51cto.com/mofansheng/1681807,如需转载请自行联系原作者
相关实践学习
SLB负载均衡实践
本场景通过使用阿里云负载均衡 SLB 以及对负载均衡 SLB 后端服务器 ECS 的权重进行修改,快速解决服务器响应速度慢的问题
负载均衡入门与产品使用指南
负载均衡(Server Load Balancer)是对多台云服务器进行流量分发的负载均衡服务,可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应用系统的可用性。 本课程主要介绍负载均衡的相关技术以及阿里云负载均衡产品的使用方法。
目录
相关文章
|
2月前
|
开发框架 负载均衡 Java
当热门技术负载均衡遇上 Spring Boot,开发者的梦想与挑战在此碰撞,你准备好了吗?
【8月更文挑战第29天】在互联网应用开发中,负载均衡至关重要,可避免单服务器过载导致性能下降或崩溃。Spring Boot 作为流行框架,提供了强大的负载均衡支持,通过合理分配请求至多台服务器,提升系统可用性与可靠性,优化资源利用。本文通过示例展示了如何在 Spring Boot 中配置负载均衡,包括添加依赖、创建负载均衡的 `RestTemplate` 实例及服务接口调用等步骤,帮助开发者构建高效、稳定的应用。随着业务扩展,掌握负载均衡技术将愈发关键。
54 6
|
9天前
|
运维 负载均衡 监控
提升系统性能:高效运维的秘密武器——负载均衡技术
在当今数字化时代,系统的高可用性和高性能成为各类企业和组织追求的目标。本文旨在探讨负载均衡技术在运维工作中的关键作用,通过深入分析其原理、类型及实际应用案例,揭示如何利用这项技术优化资源分配,提高系统的响应速度和可靠性,确保用户体验的稳定与流畅。无论是面对突如其来的高流量冲击,还是日常的运维管理,负载均衡都展现出了不可或缺的重要性,成为现代IT架构中的基石之一。
27 4
|
14天前
|
负载均衡 前端开发 应用服务中间件
前后端分离技术与NGINX的简单使用
前后端分离技术与NGINX的简单使用
|
1天前
|
缓存 负载均衡 应用服务中间件
解决Nginx常见问题的技术指南
解决Nginx常见问题的技术指南
18 0
|
29天前
|
负载均衡 网络协议 Unix
Nginx负载均衡与故障转移实践
Nginx通过ngx_http_upstream_module模块实现负载均衡与故障转移,适用于多服务器环境。利用`upstream`与`server`指令定义后端服务器组,通过`proxy_pass`将请求代理至这些服务器,实现请求分发。Nginx还提供了多种负载均衡策略,如轮询、权重分配、IP哈希等,并支持自定义故障转移逻辑,确保系统稳定性和高可用性。示例配置展示了如何定义负载均衡设备及状态,并应用到具体server配置中。
|
1月前
|
负载均衡 网络协议 应用服务中间件
web群集--rocky9.2源码部署nginx1.24的详细过程
Nginx 是一款由 Igor Sysoev 开发的开源高性能 HTTP 服务器和反向代理服务器,自 2004 年发布以来,以其高效、稳定和灵活的特点迅速成为许多网站和应用的首选。本文详细介绍了 Nginx 的核心概念、工作原理及常见使用场景,涵盖高并发处理、反向代理、负载均衡、低内存占用等特点,并提供了安装配置教程,适合开发者参考学习。
|
2月前
|
负载均衡 算法 应用服务中间件
负载均衡技术在Web服务器集群中的应用
【8月更文第28天】随着互联网的发展和用户对Web服务需求的增长,单台服务器很难满足大规模访问的需求。为了提高系统的稳定性和扩展性,通常会采用Web服务器集群的方式。在这种架构中,负载均衡器扮演着至关重要的角色,它能够合理地分配客户端请求到不同的后端服务器上,从而实现资源的最优利用。
77 2
|
2月前
|
负载均衡 应用服务中间件 Linux
"揭晓nginx的神秘力量:如何实现反向代理与负载均衡,拯救服务器于水火?"
【8月更文挑战第20天】在Linux环境下,nginx作为高性能HTTP服务器与反向代理工具,在网站优化及服务器负载均衡中扮演重要角色。本文通过电商平台案例,解析nginx如何解决服务器压力大、访问慢的问题。首先介绍反向代理原理,即客户端请求经由代理服务器转发至内部服务器,隐藏真实服务器地址;并给出配置示例。接着讲解负载均衡原理,通过将请求分发到多个服务器来分散负载,同样附有配置实例。实践表明,采用nginx后,不仅服务器压力得到缓解,还提升了访问速度与系统稳定性。
60 3
|
2月前
|
负载均衡 应用服务中间件 Linux
在Linux中,Nginx如何实现负载均衡分发策略?
在Linux中,Nginx如何实现负载均衡分发策略?
|
5月前
|
负载均衡 应用服务中间件 API
Nginx配置文件详解Nginx负载均衡Nginx静态配置Nginx反向代理
Nginx配置文件详解Nginx负载均衡Nginx静态配置Nginx反向代理
138 4