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