apache - Docker 在子域上运行

标签 apache http ubuntu nginx docker

我的目标是部署多个 webapps 并通过子域访问它们,我目前在不同的端口上运行它们,我在服务器上有 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

最佳答案

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

然后你只需启动带有暴露端口的容器(--expose 80,而不是-p 80:80)并添加环境变量:

-e VIRTUAL_HOST=foo.bar.com

不要忘记使用正确的 header 传输您的主要 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;
    }

}                    

关于apache - Docker 在子域上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33586062/

相关文章:

java - 如何在Android中读取HttpResponse?

linux - Apache - 授予对服务器上 "shared"目录的访问权限

android - 应用程序到 Web API 产生 400 无效的主机名

ubuntu - 在 Ubuntu 上安装 DCMTK 时出错

php - REMOTE_ADDR 为空,不包含在 SERVER 数组中

php - 调用未定义函数 mysql_query()

javascript - 我如何在请求模块中进行response.write

java - 如何将参数值发送到服务器并使用 AsyncTask 获取数据(通过 POST 请求)?

php - 为什么只有一台服务器抛出错误 "Array to String Conversion"?

python - 升级到 sklearn 0.15 后导入错误