nginx反向代理--根据访问路径将请求分发至不同服务器

环境: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服务

150331_2vll_3703522.jpg

node2被代理的http服务

150402_dvj8_3703522.jpg

node3被代理的http服务

150459_LUjj_3703522.jpg

发现nginx代理服务器自身的http代理失败,经排查,是访问控制策略的问题,关闭selinux或者设置

setsebool httpd_can_network_connect on
setsebool httpd_can_network_connect on -P

然后重启或重新加载nginx

重新访问:

151003_vNwm_3703522.jpg

代理访问成功。

 

转载于:https://my.oschina.net/u/3703522/blog/1603984

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值