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

简介:

使用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,如需转载请自行联系原作者
相关实践学习
部署高可用架构
本场景主要介绍如何使用云服务器ECS、负载均衡SLB、云数据库RDS和数据传输服务产品来部署多可用区高可用架构。
负载均衡入门与产品使用指南
负载均衡(Server Load Balancer)是对多台云服务器进行流量分发的负载均衡服务,可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应用系统的可用性。 本课程主要介绍负载均衡的相关技术以及阿里云负载均衡产品的使用方法。
目录
相关文章
|
25天前
|
Web App开发 编解码 运维
LNMP详解(十二)——Nginx URL重写实战
LNMP详解(十二)——Nginx URL重写实战
19 2
|
29天前
|
运维 负载均衡 应用服务中间件
LNMP详解(九)——Nginx虚拟IP实战
LNMP详解(九)——Nginx虚拟IP实战
44 2
|
23天前
|
负载均衡 算法 应用服务中间件
面试题:Nginx有哪些负载均衡算法?Nginx位于七层网络结构中的哪一层?
字节跳动面试题:Nginx有哪些负载均衡算法?Nginx位于七层网络结构中的哪一层?
35 0
|
1月前
|
运维 负载均衡 应用服务中间件
LNMP详解(九)——Nginx虚拟IP实战
LNMP详解(九)——Nginx虚拟IP实战
57 2
|
1月前
|
负载均衡 应用服务中间件 API
Nginx配置文件详解Nginx负载均衡Nginx静态配置Nginx反向代理
Nginx配置文件详解Nginx负载均衡Nginx静态配置Nginx反向代理
43 4
|
9天前
|
负载均衡 算法 网络协议
LVS、Nginx和HAProxy负载均衡器对比总结
LVS、Nginx和HAProxy负载均衡器对比总结
|
13天前
|
负载均衡 应用服务中间件 nginx
Nginx 负载均衡
Nginx 负载均衡
23 2
|
23天前
|
缓存 负载均衡 应用服务中间件
nginx的各种负载均衡策略与各种负载均衡策略如何配置
Nginx支持多种负载均衡策略,如轮询、加权轮询、IP哈希、最少连接、URL哈希和fair策略。轮询是默认策略,每个请求按顺序分发;加权轮询根据权重分配请求;IP哈希确保相同IP的请求始终发送到同一服务器;最少连接将请求发送给连接数最少的服务器;URL哈希(需额外工具或模块)和fair策略则依据URL和响应时间分配请求。配置变更需更新nginx.conf并重新加载或重启服务,具体配置应参照官方文档。
40 0
|
23天前
|
应用服务中间件 nginx
nginx进行反向代理的配置
在Nginx中设置反向代理的步骤:编辑`/etc/nginx/nginx.conf`,在http段加入配置,创建一个监听80端口、服务器名为example.com的虚拟主机。通过`location /`将请求代理到本地3000端口,并设置代理头。保存配置后,使用`sudo nginx -s reload`重载服务。完成配置,通过example.com访问代理服务器。
26 0
|
4月前
|
负载均衡 应用服务中间件 nginx
百度搜索:蓝易云【Nginx和tomcat实现负载均衡教程】
至此,你已经成功地使用Nginx和Tomcat实现了负载均衡。Nginx将根据配置的负载均衡策略将客户端请求分发到多个Tomcat服务器上,以提高系统的性能和可用性。请注意,在实际生产环境中,还需要进行其他配置和优化,如健康检查、会话保持等,以满足具体的需求。
35 0