Centos7 配置Nginx负载均衡

Nginx是一个免费的开源Web服务器。 Nginx也可以用作反向代理,负载均衡,邮件代理和HTTP缓存。 目前,地球上有非常多Web服务器正在使用Nginx作为反向代理和负载均衡。
在本文中,我们将Nginx配置为CentOS 7中的HTTP负载平衡器。
仅供学习与交流,请勿用于商业用途

准备三台虚拟机
系统环境如下

主机名IP地址操作系统
web01.doragon.xyz192.168.122.101Centos7
web02.doragon.xyz192.168.122.102Centos7
proxy00.doragon.xyz192.168.122.103Centos7

三台机器分别配置清华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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值