前言
学习过程中天天虚拟机,今天换个新颖的,使用阿里云的云服务器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 [email protected]
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 [email protected]
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.
[email protected]'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切换