2017-04-27 208 views
1

我想在一臺主機上運行多個docker容器,只能通過一個域訪問。我想使用請求url來區分容器。 要做到這一點,我想設置的nginx服務器作爲反向代理和容器也偵聽端口運行80nginx反向代理可訪問多個docker容器

比方說,我有兩個容器在端口3000和4000 運行路由將以下:

docker-host.example.com/3000 -> this will access container exposing port 3000 
docker-host.example.com/4000 -> this will access container exposing port 4000 

事情是我目前正在嘗試爲這種反向代理定義靜態規則堆棧。 它工作正常,沒有任何的位置:

upstream application { 
    server <docker container>:3000; 
} 

server { 
     listen 80; 

     location/{ 
      proxy_pass_header Server; 
      proxy_set_header Host $http_host; 
      proxy_redirect  off; 
      proxy_set_header X-Real-IP $remote_addr; 
      proxy_set_header X-Scheme $scheme; 
      proxy_pass   http://application/; 
     } 
} 

但是當我添加端口位置並嘗試使用本地主機訪問它:{nginx的端口}/3000/

upstream application { 
    server <docker container>:3000; 
} 

server { 
     listen 80; 

     location /3000/ { 
      proxy_pass_header Server; 
      proxy_set_header Host $http_host; 
      proxy_redirect  off; 
      proxy_set_header X-Real-IP $remote_addr; 
      proxy_set_header X-Scheme $scheme; 
      proxy_pass   http://application/3000/; 
     } 
} 

看來,第一資源(主html)被正確請求,但其他依賴資源(例如本站點需要的js或css)缺失。 如果我考察那些我在日誌中的資源請求:

09:19:20 [error] 5#5: *1 open() "/etc/nginx/html/public/css/fonts.min.css" failed (2: No such file or directory), client: 172.17.0.1, server: , request: "GET /public/css/fonts.min.css HTTP/1.1", host: "localhost:8455", referrer:"http://localhost:8455/3000/" 

所以請求的URL是http://localhost:8455/public/css/fonts.min.css

相反http://localhost:8455/3000/public/css/fonts.min.css

我能問你的任何建議嗎?這種情況可能嗎?

回答

0

您可以爲每個端口選擇一個碼頭工人的容器,你的例子:

但有我更喜歡,因爲我覺得這是更清晰的另一種方法,通過域名訪問碼頭集裝箱,例如:

無論選擇哪種方式,有在github上一個項目,幫助你實現搬運工多容器反向代理:https://github.com/jwilder/nginx-proxy

我使用泊塢窗,構成了一個書面的例子類似的情況:http://carlosvin.github.io/posts/reverse-proxy-multidomain-docker/