Nginx是一个免费的开源Web服务器。 Nginx也可以用作反向代理,负载均衡,邮件代理和HTTP缓存。 目前,地球上有非常多Web服务器正在使用Nginx作为反向代理和负载均衡。
在本文中,我们将Nginx配置为CentOS 7中的HTTP负载平衡器。
仅供学习与交流,请勿用于商业用途
准备三台虚拟机
系统环境如下
主机名 | IP地址 | 操作系统 |
---|---|---|
web01.doragon.xyz | 192.168.122.101 | Centos7 |
web02.doragon.xyz | 192.168.122.102 | Centos7 |
proxy00.doragon.xyz | 192.168.122.103 | Centos7 |
三台机器分别配置清华epel,并安装nginx
yum-config-manager --add-repo=https://mirrors.tuna.tsinghua.edu.cn/epel/7/x86_64/
rpm --import https://mirrors.tuna.tsinghua.edu.cn/epel/RPM-GPG-KEY-EPEL-7
rpm --import https://mirrors.tuna.tsinghua.edu.cn/epel/RPM-GPG-KEY-EPEL-7Server
yum install nginx -y
防火墙放行80端口
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --reload
启动NGINX服务
systemctl enable nginx
systemctl start nginx
分别修改web01和web02的index方便呈现负载均衡效果
[root@t4900v-centos ~]# curl 192.168.122.102
this web1
[root@t4900v-centos ~]# curl 192.168.122.103
this web2
ssh进入proxy00,添加虚拟主机
vim /etc/nginx/conf.d/proxy.conf
upstream wdnmd {
server 192.168.122.101;
server 192.168.122.102;
}
server {
listen 8888;
location / {
proxy_pass http://wdnmd;
}
}
修改selinux,允许8888端口运行HTTP
semanage port -a -t http_port_t -p tcp 8888
防火墙放行8888
firewall-cmd --permanent --add-port=8888/tcp
firewall-cmd --reload
重启nginx
systemctl restart nginx
测试效果
while true ; do curl 192.168.122.101; done
this web1
this web2
this web1
this web2
this web1
this web2
^C