nginx 负载均衡

nginx 负载均衡

任意选择3台centos系统的服务器(可以任意选择几台都可以),我的是1台centos7,2台centos6的

负载均衡端:192.168.153.171

web端:192.168.153.128 / 129

1.关闭所有服务器的防火墙以及selinux。
centos7的操作

systemctl stop firewalld
setenforce 0

centos6的操作

service iptables stop
setenforce 0

2.看看有没有正在运行的80端口

netstat -ntlp |grep 80

如果有其它应用,请先kill掉,要保证80端口不能被占用
3.下载或者源码安装nginx

我的centos7是yum安装

yum -y install epel-*
yum -y install nginx

centos6是源码安装
首先安装所需要的依赖环境

yum -y install gcc gcc-c++ pcre-devel zlib-devel lrzsz

rz 所需要的源码包( lrzsz 应用命令rz是上传,sz是下载 )
这时我们的目录中已经有未被解压的源码包了

tar zxf nginx-1.16.1.tar.gz

cd nginx-1.16.1 

./configure && make && make install

4.在web端新建测试页面

vim /usr/local/nginx/html/index.html

<h1>web 1/2  128 / 129</h1>
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

5.配置负载均衡端的配置文件

因为是yum安装所以它的配置文件在/etc下

vim /etc/nginx/nginx.conf

在http{}里面写

http{
   ~~~\
   upstream webs {
           server 192.168.153.128:80 weight=1;
           server 192.168.153.129:80 weight=3;
    }
   server {
           location / {
                proxy_pass http://webs;
        }
    }
}

以上为nginx的负载均衡轮询反向代理

6.启动所有服务器的nginx服务
centos7的操作

systemctl start nginx
systemctl enable nginx

centos6的操作

service nginx start

7.效果展示
在这里插入图片描述
因为是1:3轮询模式所以会展示出1个128页面,3个129页面

https://mp.weixin.qq.com/s/jS-7uRw7Ryv6B8aJFZWvYQ

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值