一个和同一个Docker容器上有多个虚拟主机
我试图在一个相同的Docker容器和端口上运行两个不同的域.
I'm trying to run two different domains on one and the same Docker container and port.
Docker容器运行CentOS. docker-compose.yml
看起来像这样:
The Docker container runs CentOS. docker-compose.yml
looks like so:
web:
image: fab/centos
ports:
- "80:80"
volumes:
- ./src/httpd.conf:/etc/httpd/conf/httpd.conf
- ./src:/var/www/html
- ./src/hosts:/etc/hosts
environment:
- VIRTUAL_HOST=dummy.dev,tests.dev
我还声明了主机(OS X.)上/etc/hosts
内的两个.dev域名.
I also declared both .dev domain names inside of /etc/hosts
on the host computer (OS X.)
自从我配置虚拟主机已经有一段时间了.我的理解是,我只需要声明它们,Apache就会根据请求的HTTP HOST自动提供适当的文件.
It's been a while since I configured virtual hosts. My understanding was that I just needed to declare them and that Apache would automatically serve the proper files depending on the HTTP HOST being requested.
这是我所拥有的,添加到httpd.conf
的末尾:
This is what I have, added at the end of httpd.conf
:
<VirtualHost *:80> # first host = default host
DocumentRoot /var/www/html/default
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/html/dummy
ServerName dummy.dev
ServerAdmin webmaster@dummy.dev
ErrorLog logs/dummy.dev-error_log
CustomLog logs/dummy.dev-access_log common
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/html/tests
ServerName tests.dev
ServerAdmin webmaster@tests.dev
ErrorLog logs/tests.dev-error_log
CustomLog logs/tests.dev-access_log common
</VirtualHost>
但是,实际上,访问dummy.dev或tests.dev实际上是为/var/www/html/default
服务的.好像Apache没有意识到正在调用哪个主机(尽管PHP中的$_SERVER
转储确实显示了预期的HTTP_HOST
值,即:127.0.0.1,dummy.dev或tests.dev取决于哪个)我访问的网址.)
However, in practice, visiting either dummy.dev or tests.dev actually serves /var/www/html/default
. This is as if Apache didn't realize which host is being called (though a dump of $_SERVER
in PHP does show the expected HTTP_HOST
value, i.e.: either 127.0.0.1, dummy.dev or tests.dev depending on which URL I visit.)
我想念什么?
我不清楚这是Apache问题还是Docker问题.
It's unclear to me whether this is an Apache issue or a Docker one.
(请注意,这与如何在同一域中使用不同端口托管多个应用程序是一个不同的问题.就我而言,我确实希望虚拟主机都位于同一应用程序/端口/容器内部/之内.)
(Please note this is a different question from how to host multiple apps on the same domain with different port. In my case, I do want the virtual hosts to be all inside/on the same app/port/container.)
原来是Apache配置问题.
Turns out this was an Apache configuration issue.
我需要显式启用域名虚拟主机,就像这样:
I needed to explicitly enable domain-named virtualhosts, like so:
NameVirtualHost *:80
Docker与这件事无关.
Docker had nothing to do with the matter.