centos7 使用 nginx 反向代理,web 端高可用

148 篇文章 2 订阅

centos7 使用 nginx 反向代理,web 端高可用

实验目的: 访问 nginx 服务器,自动跳转到 web1 或者 web2 服务器,web1 web2 任意一个挂掉,都不会影响服务,实现高可用
实验环境: 3 台 centos7 服务器,web1: 10.2.7.200 web2: 10.2.7.201 nginx: 10.2.7.203

1 web1, web2 安装 httpd,关闭防火墙(web1, web2 上操作)

yum install -y httpd
systemctl start httpd
systemctl stop firewalld

2 修改 web1 的显示页面(web1 上操作)

echo "Hello web1" > /var/www/html/index.html

3 修改 web2 的显示页面(web2 上操作)

echo "Hello web2" > /var/www/html/index.html

4 nginx 服务器安装 nginx, 关闭防火墙,修改 nginx 配置文件(nginx 上操作)

yum install -y nginx
systemctl stop firewalld
# cat /etc/nginx/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream test {
        server 10.2.7.200:80 weight=1;
        server 10.2.7.201:80 weight=1;
    }
    server {
        listen 80;
        server_name localhost;
        location / {
        proxy_pass 
http://test
;
        }
    }
}

5 浏览器访问 10.2.7.203,会发现 web1 和 web2 页面轮流显示。当关闭 web1,访问10.2.7.203,只会显示 web2

注意事项:
1 如果 nginx 服务已经开启,再修改 /etc/nginx/nginx.conf,记得关闭 nginx服务,同步配置,最后开启服务

systemctl stop nginx
nginx -c /etc/nginx/nginx.conf
nginx -t 
systemctl start nginx

2 如果启动 nginx 失败,可以使用 lsof 查看占用端口,查找占用端口的进程,然后 kill 掉

lsof -i:80
kill 3210
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值