设置动态虚拟主机(Ubuntu 上的 Apache2)
我想设置一个虚拟主机,它可以根据用于访问它的主机名动态处理所有请求。如果 %{HTTP_HOST} 可以在 DocumentRoot 中使用,这可能正是我想要的:
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /var/www/live/%{HTTP_HOST}/public
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/live/%{HTTP_HOST}/public>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
# Possible values include: debug, info, notice, warn, error, crit, alert, emerg.
LogLevel warn
ErrorLog /var/www/live/%{HTTP_HOST}/logs/error.log
CustomLog /var/www/live/%{HTTP_HOST}/logs/access.log combined
</VirtualHost>
...不幸的是,DocumentRoot 中不允许使用 %{HTTP_HOST} (警告:DocumentRoot [/var/www/live/ %{HTTP_HOST}/public] 不存在
)。我还能怎样实现我的目标?
更新:我想过将一个包罗万象的虚拟主机指向一个目录,并让 .htaccess 使用 mod_rewrite 来动态选择路径,但(老实说)我已经筋疲力尽了。我会在早上再次尝试,但与此同时,如果有人有好的想法,我很乐意听到他们!谢谢你!
I want to set up a single virtual host that can dynamically handle all requests based on the hostname used to access it. If %{HTTP_HOST} could be used in a DocumentRoot, this is probably exactly what I want:
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /var/www/live/%{HTTP_HOST}/public
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/live/%{HTTP_HOST}/public>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
# Possible values include: debug, info, notice, warn, error, crit, alert, emerg.
LogLevel warn
ErrorLog /var/www/live/%{HTTP_HOST}/logs/error.log
CustomLog /var/www/live/%{HTTP_HOST}/logs/access.log combined
</VirtualHost>
...unfortunately, %{HTTP_HOST} is not allowed in the DocumentRoot (Warning: DocumentRoot [/var/www/live/%{HTTP_HOST}/public] does not exist
). How else can I achieve my goal?
Update: I thought of pointing a catch-all vhost to a single directory and having a .htaccess use mod_rewrite to dynamically select the path but (honestly) I'm exhausted. I'll try at it again in the morning, but in the meantime, if anyone has good ideas, I'd love to hear them! Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许您可以尝试本文中的以下解决方案: Apache:动态虚拟主机
Maybe you can try the following solution from this article: Apache: Dynamic Virtual Hosts
Apache文档中解释了官方实现动态虚拟主机的方法:
http:// httpd.apache.org/docs/2.0/vhosts/mass.html
The official methods for achieving dynamic virtual hosts are explained in the Apache documentation:
http://httpd.apache.org/docs/2.0/vhosts/mass.html