1、什么是反向代理?
1.1 反向代理
2、配置nginx反向代理
2.1、实验准备
使用Nginx实现Web反向代理功能,实现如下的功能:
- 后台web服务器俩台,可以使用httpd实现
- Nginx采用轮询的方式调用后端Web服务器
- 俩台Web服务器的权重要求设置为不同的值
- 最大失败次数为1,失败超时时间为30秒
2.2、方案
- 如图进行配置
2.3 步骤
- 步骤一:部署实施后端Web服务器
##########部署第一台web服务器############
yum -y install httpd
echo "192.168.2.100" > /var/www/html/index.html
systemctl restart httpd
###########部署第二台web服务器##############
yum -y install httpd
echo "192.168.2.200" > /var/www/html/index.html
systemctl restart httpd
-
步骤二:部署Nginx服务器,添加服务器池,实现反向代理功能
vim /xxx/xxx/xxx/nginx/conf/nginx.conf....... http{ upstream webserver { server 192,168,2.100:80; server 192.168.2.200:80; } ...... server { listen 80; server_name localhost; location \ { proxy_pass http://webserver; //将用户的请求转发至webserver集群 } } }
-
步骤三:重启服务配置
服务重启详情传送门 -
步骤四 :客户端使用浏览器访问代理服务器测试轮询效果
curl http://192.169.4.5 curl http://192.168.4.5
3、配置upstream服务器集群池属性
设置失败次数、超时时间,权重weight可以设置后台服务器的权重,max_faiks可以设置后台服务器的失败次数,fail_timeout可以设置后台服务器的失败超时时间。
- 步骤一 :配置nginx服务配置
vim /xxx/xxx/xxx/nginx/conf/nginx.conf
....
http
{
....
upstrean webserver {
server 192.168.2.100 weight=1 max_fails=1 fail_timeout=30;
server 192,168,2,200 weight=2 max_fails=2 fail_timeout=30;
server 192.168.2.101 down;
}
# weight 设置服务器权重值,默认值为1
# max_fails设置最大失败次数,测试服务器几次才确认服务器失败
# fail_timeout设置失败超时时间,单位为妙
# down标记服务器已关机,不参与集群调度
}
....
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://webserver;
}
}
-
步骤二 : 重启nginx服务,使配置生效。
nginx服务重启详情传送门 -
步骤三: 关闭一台后端服务器
systemctl stop httpd
- 步骤四: 客户端使用浏览器访问代理服务器测试轮询效果
curl http://192.168.4.6