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

從新訪問:

代理訪問成功。

相關文章
相關標籤/搜索