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

环境:centos7.2html

nginx版本:1.12.2node

node1:192.168.169.103(nginx代理服务器:80端口、http服务:8080端口)linux

node2:192.168.169.113(http服务:80端口)nginx

node3:192.168.169.154(http服务:80端口)vim

关闭防火墙centos

 

node1安装nginx及http服务,并修改端口浏览器

node二、node3安装http服务。服务器

在三台服务器上的http服务的指定路径下,/var/www/html下建立test文件夹,而后在/var/www/html/test文件夹下,服务器node1建立node1文件夹,服务器node2建立node2文件夹,服务器node3建立node3文件夹。在node一、二、3文件夹下建立index.html文件。this

node1的html文件内容为:this is node1_server!centos7

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

从新访问:

代理访问成功。

相关文章
相关标签/搜索