环境:centos7.2
nginx版本:1.12.2
node1:192.168.169.103(nginx代理服务器:80端口、http服务:8080端口)
node2:192.168.169.113(http服务:80端口)
node3:192.168.169.154(http服务:80端口)
关闭防火墙
node1安装nginx及http服务,并修改端口
node2、node3安装http服务。
在三台服务器上的http服务的指定路径下,/var/www/html下创建test文件夹,然后在/var/www/html/test文件夹下,服务器node1创建node1文件夹,服务器node2创建node2文件夹,服务器node3创建node3文件夹。在node1、2、3文件夹下创建index.html文件。
node1的html文件内容为:this is node1_server!
node2的html文件内容为:this is node2_server!
node3的html文件内容为:this is node3_server!
三台服务器分别开启http服务。
设置代理:
cd /etc/nginx/conf.d #此路径下为nginx配置文件指定路径
vim proxy.conf #文件名自取,文件名以conf结尾即可被加载
server {
listen 80;
server_name _;
location ~/node1 {
proxy_pass http://192.168.169.103:8080;
}
location ~/node2 {
proxy_pass http://192.168.169.113:80;
}
location ~/node3 {
proxy_pass http://192.168.169.154:80;
}
}
node1开启nginx
浏览器分别访问代理指定路径,查看效果:
node1被代理的http服务
node2被代理的http服务
node3被代理的http服务
发现nginx代理服务器自身的http代理失败,经排查,是访问控制策略的问题,关闭selinux或者设置
setsebool httpd_can_network_connect on
setsebool httpd_can_network_connect on -P
然后重启或重新加载nginx
重新访问:
代理访问成功。