云服务?使用阿里云线上搭建nginx负载均衡群集

前言

学习过程中天天虚拟机,今天换个新颖的,使用阿里云的云服务器ECS做做线上。

在这里插入图片描述
这是我准备的环境,3台nginx服务器

设备作用用途
nginx服务器(172.16.46.98)做负载均衡服务器
nginx服务器(172.16.46.99)web1
ngin服务器(172.16.46.100)web2
使用xshell登录公网ip,然后再通过ssh,连接其他两台内部主机
[E:\~]$ ssh root@39.104.79.172


Connecting to 39.104.79.172:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

WARNING! The remote SSH server rejected X11 forwarding request.

Welcome to Alibaba Cloud Elastic Compute Service !

[root@iZhp33kjmxtw7j000sxgfoZ ~]# 
[root@iZhp33kjmxtw7j000sxgfoZ ~]# 
[root@iZhp33kjmxtw7j000sxgfoZ ~]# 
[root@iZhp33kjmxtw7j000sxgfoZ ~]# hostname lb
[root@iZhp33kjmxtw7j000sxgfoZ ~]# bash
[root@lb ~]# 

类似于跳板机,再打开2个一模一样的终端,ssh连接另两台

[root@iZhp33kjmxtw7j000sxgfoZ ~]# ssh root@172.16.46.99
The authenticity of host '172.16.46.99 (172.16.46.99)' can't be established.
ECDSA key fingerprint is SHA256:iYytabIi/PCVEaOhb2iaEq14gzVHLG6egDPCIjApxVc.
ECDSA key fingerprint is MD5:8c:a6:3f:fd:f5:00:6e:25:78:8e:86:78:6d:84:2e:f9.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.46.99' (ECDSA) to the list of known hosts.
root@172.16.46.99's password: 

Welcome to Alibaba Cloud Elastic Compute Service !

[root@iZhp33kjmxtw7j000sxgfnZ ~]# hostname web1
[root@iZhp33kjmxtw7j000sxgfnZ ~]# bash
[root@web1 ~]# 

web2操作一样,略

安装nginx(3台云主机一样)
[root@lb ~]# yum -y install nginx
[root@web1 ~]# yum -y install nginx
[root@web2 ~]# yum -y install nginx

阿里云服务器自带阿里云的yum源

[root@lb ~]# cd /etc/yum.repos.d/
[root@lb yum.repos.d]# ls
CentOS-Base.repo  epel.repo
[root@lb yum.repos.d]# 

没有的话可以这样下载:

epel源(扩展包):wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

linux镜像源(组包):wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

(2)配置web1(web2类似,略)

添加一个虚拟主机,添加基本测试行。

[root@web1 ~]# cat /etc/nginx/conf.d/mb.conf 
server {
        listen 80;
        server_name www.lzj.com;

        location / {
                root /www;
                index index.html index.htm;
        }
}
[root@web1 ~]# 

添加测试文件

[root@web1 ~]# mkdir /www
[root@web1 ~]# echo '<h1>web1:172.16.46.99</h1>' > /www/index.html
[root@web2 ~]# mkdir /www
[root@web2 ~]# echo '<h1>web2:172.16.46.100</h1>' > /www/index.html

都准备好以后,开启服务

[root@web1 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web1 ~]# systemctl start nginx
[root@web1 ~]# curl 172.16.46.100
<h1>web2:172.16.46.100</h1>

web2如下:

[root@web2 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web2 ~]# systemctl start nginx
[root@web2 ~]# curl 172.16.46.99
<h1>web1:172.16.46.99</h1>

配置7层负载均衡nginx

编辑虚拟主机配置文件
vim /etc/nginx/conf.d/lb.conf

upstream web_cluster {
        server 172.16.46.99:80;
        server 172.16.46.100:80;
}

server {
        listen 80;
        server_name www.lzj.com;

        location / {
                proxy_pass http://web_cluster;
                include nginx_params;
        }
}
保存退出

编辑添加的变量文件nginx_params

添加:
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;

proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;

proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;
保存退出

systemctl start nginx

客户端修改hosts,访问测试

在这里插入图片描述
添加:39.104.79.172 www.lzj.com

在这里插入图片描述
多次刷新就会在web1和web2切换

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值