【问题标题】:Docker run on sub domainsDocker 在子域上运行
【发布时间】:2016-02-08 17:42:04
【问题描述】:

我的目标是部署多个 webapp 并通过子域访问它们,我目前在不同的端口上运行它们,我在服务器上有 nginx,容器正在运行 apache。

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

【问题讨论】:

标签: apache http ubuntu nginx docker


【解决方案1】:

我也有类似的问题。另外我经常需要添加和删除容器,所以我不想每次都编辑 nginx conf。我的解决方案是使用 jwilder/nginx-proxy。

然后您只需使用暴露端口(@98​​7654321@,而不是 -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;
    }

}                    

【讨论】:

  • 谢谢,我最终使用了 jwilder/nginx-proxy 哈哈,这是一个很棒的容器
【解决方案2】:

查看nginx-proxy 项目 (https://github.com/jwilder/nginx-proxy)。我在我的博客中写了一个教程,它准确地展示了你想用它来实现什么:https://blog.florianlopes.io/host-multiple-websites-on-single-host-docker

此工具会自动将请求转发到适当的容器(基于通过VIRTUAL_HOST 容器环境变量的子域)。

例如,如果您想通过 test1.example.com 访问您的容器,只需将 VIRTUAL_HOST 容器环境变量设置为 test1.example.com

【讨论】:

    猜你喜欢
    • 2019-10-16
    • 2018-04-07
    • 1970-01-01
    • 2018-06-12
    • 2017-12-18
    • 2020-03-14
    • 1970-01-01
    • 2013-10-19
    • 2015-12-16
    相关资源
    最近更新 更多