猿问

使用docker compose服务时如何使两个虚拟主机指向同一个项目?

我有两个域共享相同的代码库和数据库。现在我需要两个域来连接到各自域的两个不同数据库。我正在使用 docker compose 在本地设置中提供服务。然后我转到浏览器并输入 localhost:2000 以查看我的项目是否已服务。我需要两个不同的域(例如 xyz.com 和 xyz.net)来指向同一个项目。谁可以帮我这个事?



梦里花落0921
浏览 121回答 1
1回答

当年话下

您需要解决访问时两个域都指向本地主机的问题。在/etc/hosts中添加:127.0.0.1 xyz.com127.0.0.1 xyz.net现在,如果您在浏览器中访问 xyz.com 或 xyz.com,它将解析为您的本地计算机。配置 apache vhosts 以指向 localhost:2000,如下所示:<VirtualHost *:80>&nbsp; &nbsp; &nbsp; &nbsp; ServerName xyz.com&nbsp; &nbsp; &nbsp; &nbsp; ProxyPreserveHost On&nbsp; &nbsp; &nbsp; &nbsp; ProxyRequests off&nbsp; &nbsp; &nbsp; &nbsp; <Location />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ProxyPass http://localhost:2000/&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ProxyPassReverse http://localhost:2000/&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Order allow,deny&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Allow from all&nbsp; &nbsp; &nbsp; &nbsp; </Location></VirtualHost>第二个:<VirtualHost *:80>&nbsp; &nbsp; &nbsp; &nbsp; ServerName xyz.net&nbsp; &nbsp; &nbsp; &nbsp; ProxyPreserveHost On&nbsp; &nbsp; &nbsp; &nbsp; ProxyRequests off&nbsp; &nbsp; &nbsp; &nbsp; <Location />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ProxyPass http://localhost:2000/&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ProxyPassReverse http://localhost:2000/&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Order allow,deny&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Allow from all&nbsp; &nbsp; &nbsp; &nbsp; </Location></VirtualHost>启用两个虚拟主机(sudo a2ensite [name].conf)并重新加载 apache。您还需要 proxy_http mod: sudo a2enmod proxy_http。现在两个 url 都指向您的 localhost:2000。在您的 PHP 应用程序中检测域并基于该域连接到您需要的任何数据库。我使用了这个文件 g.php:<?phpvar_dump($_SERVER['HTTP_HOST']);并在 localhost:2000 上用 php 的开发服务器提供它(您将在 docker 上使用它):php -S localhost:2000 g.php&nbsp;PHP 7.0.33-0ubuntu0.16.04.15 Development Server started at Thu Jul&nbsp; 9 20:34:03 2020Listening on http://localhost:2000当我在浏览器中访问 xyz.net 时,我得到:string(7) "xyz.net" 当我在浏览器中访问 xyz.com 时,我得到:string(7) "xyz.com"&nbsp;
随时随地看视频慕课网APP
我要回答