2015-11-07 61 views
1

我的目標是部署多個webapps並通過子域訪問它們,我當前在不同的端口上運行它們,我在服務器上運行了nginx,而容器運行的是Apache。Docker在子域上運行

docker run -p 8001:80 -d apache-test1 
docker run -p 8002:80 -d apache-test2 

,我能夠通過將

http://example.com:8001 

http://example.com:8002

訪問他們,但我想通過子域,而不是

http://example.com:8001 -> http://test1.example.com 
http://example.com:8002 -> http://test2.example.com 

訪問它們我使用以下服務器設置在服務器上運行nginx

server { 
    server_name test1.anomamedia.com; 
    location/{ 
     proxy_redirect off; 
     proxy_set_header Host $host ; 
     proxy_set_header X-Real-IP $remote_addr ; 
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for ; 
     proxy_pass http://localhost:8001; 
    } 
} 

server { 
    server_name test2.anomamedia.com; 
    location/{ 
     proxy_redirect off; 
     proxy_set_header Host $host ; 
     proxy_set_header X-Real-IP $remote_addr ; 
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for ; 
     proxy_pass http://localhost:8002; 
    } 
} 

如果它的任何幫助,這是我的Dockerfile

FROM ubuntu 

RUN apt-get update 
RUN apt-get -y upgrade 

RUN sudo apt-get -y install apache2 php5 libapache2-mod-php5 

# Install apache, PHP, and supplimentary programs. curl and lynx-cur are for debugging the container. 
RUN DEBIAN_FRONTEND=noninteractive apt-get -y install apache2 libapache2-mod-php5 php5-mysql php5-gd php-pear php-apc php5-curl curl lynx-cur 

# Enable apache mods. 
RUN a2enmod php5 
RUN a2enmod rewrite 

EXPOSE 80 

# Copy site into place. 
ADD html /var/www/html 

# Update the default apache site with the config we created. 
ADD apache-config.conf /etc/apache2/sites-enabled/000-default.conf 

# By default, simply start apache. 
CMD /usr/sbin/apache2ctl -D FOREGROUND 
+0

以及實際的問題是什麼? –

+0

子域部分不工作,它在問題中... – santi6291

+0

正如https://meta.discourse.org/t/how-do-you-go-on-configuring-another-sub-domain -with-the-nginx-instance-inside-the-docker/16846/16,你可以嘗試使用'proxy_pass http://0.0.0.0:8002;'(而不是localhost) – VonC

回答

2

我有類似的問題。另外我經常需要添加和刪除容器,所以我不會每次都編輯nginx conf。我的解決方案是使用jwilder/nginx-proxy。

然後你剛開始帶外露端口(--expose 80,不-p 80:80)的容器,並添加ENV變量:

-e VIRTUAL_HOST=foo.bar.com 

不要忘記轉移你的主要交通nginx的右頭:

server { 
    listen  80;# default_server; 
    #send all subdomains to nginx-proxy 
    server_name *.bar.com; 

    #proxy to docker nginx reverse proxy 
    location/{ 
     proxy_set_header X-Real-IP $remote_addr; #for some reason nginx 
     proxy_set_header Host  $http_host; #doesn't pass these by default 
     proxy_pass   http://127.0.0.1:5100; 
    } 

}      
+0

謝謝,我結束了使用jwilder/nginx-proxy哈哈,這是一個偉大的容器 – santi6291