2015-04-03 43 views
0

我NGINX的業餘動態路由,我想設置NGINX作爲反向代理我的Web服務器。 我想知道,下面所列出的NGINX這些東西:與NGINX

當瀏覽器URL發送請求:http://nginxproxy.com/client/1.2.3.4/,這個請求應該被傳遞到客戶端IP 1.2.3.4 http://1.2.3.4/,瀏覽器仍然應該顯示URL nginxproxy /客戶/ 1.2.3.4/ 與同爲:

  • nginxproxy.com/client/2.3.4.5 - > //2.3.4.5
  • nginxproxy.com/client/2.3.4.6 - > //2.3.4.6

所有其他請求不會加載模式應該到我的默認服務器myserver。

我可以通過使用NGINX做到這一點?

研究後,我試着用下面的配置: 但不幸的,它不工作。按照預期,地址在瀏覽器的地址欄上改爲http:/1.2.3.4,而不是http:/nginxproxy.com/client/1.2.3.4。

server { 
    listen  80; 

    location ~ ^/client {  
     rewrite ^/client/?(.*) /$2 break; 
     proxy_pass $scheme://$1;    
    } 
    location/{ 
     proxy_pass http://myserver.com; 
    } 
} 

任何幫助,非常感謝。

+0

是的,可以。試過了什麼? – 2015-04-03 05:43:16

+0

是的,我試圖通過使用重寫模式,但不幸。瀏覽器地址欄上的地址更改爲http://1.2.3.4。 – Khate 2015-04-03 16:23:31

+0

而代碼是? – 2015-04-03 16:26:08

回答

1

做一些基於@Cole投入更多的研究和,這裏就是我的回答:

location ~ ^/client/(?<site>[^/]+)/? { 
    rewrite ^.*\/client\/(?<site>[^\/]+)\/?(.*) /$2 break; #passing all the remaining request URIs after <site> group to client server 
    proxy_pass $scheme://$site; 
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    proxy_set_header Host $http_host/client/$site; #this help to keep the address as it is on the browser's address bar 
    proxy_set_header X-Forwarded-Proto $scheme;    
} 

location/{ 
    proxy_pass $scheme://myserver.com 
} 
2
server { 
    listen 80; 

    location /client/ { 
     rewrite ^/client/(?<site>[^/]+)/? $scheme://$site; 
    } 

    location/{ 
     proxy_pass $scheme://myserver.com; 
    } 
} 
+0

謝謝科爾,我的答案非常接近。但是,如果我刪除重寫請求uri /client/1.2.3.4將傳遞到我的客戶端服務器。像這樣:[http://1.2.3.4/client/1.2.3.4/index.html](http://1.2.3.4/client/1.2.3.4/index.html) – Khate 2015-04-03 22:29:42

+0

我已經添加了一個外部重寫。這有幫助嗎? – 2015-04-04 18:33:27

+0

感謝@Cole的幫助。最後,我根據您的意見找到了我的解決方案。 – Khate 2015-04-05 02:16:39