设置动态虚拟主机(Ubuntu 上的 Apache2)

发布于 2024-12-29 17:53:41 字数 1195 浏览 3 评论 0原文

我想设置一个虚拟主机,它可以根据用于访问它的主机名动态处理所有请求。如果 %{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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨夜星沙 2025-01-05 17:53:41

也许您可以尝试本文中的以下解决方案: Apache:动态虚拟主机

几个月前,我寻找解决方案来克服以下问题
每次我想在 Apache 中创建单独的虚拟主机
在开发机器上配置一个新站点(一个很大的东西)
我们有很多网站的工作中的问题)。阿帕奇能够
使用模块和一些模块相对容易地支持此功能
配置文件中的行。我在 Fedora 14 上设置了这个,所以
对于其他操作系统,结果可能略有不同(不同的路径,
配置文件设置等)

打开主 Apache conf (/etc/httpd/conf/httpd.conf),并确保
模块 mod_vhost_alias 已启用。中应该有一行
配置如

LoadModule vhost_alias_module 模块/mod_vhost_alias.so 

接下来,添加
将以下几行添加到该文件的底部。您需要编辑
具有 sudo 权限的文件。

NameVirtualHost *:80

使用规范名称关闭

<虚拟主机 *:80>
    VirtualDocumentRoot /var/www/html/domains/%0 

这为通过端口 80(即
http 流量的默认端口,如果您使用 https,则需要
使用 443 - 或者您可以删除端口限制)。这
这里重要的一行是 VirtualDocumentRoot。告诉 Apache 在哪里
您的文件将驻留在磁盘上。 %0 部分占据整个域
名称并将其插入到路径中。为了说明这一点,如果我们去
VirtualDocumentRoot 域 testing.com.dev 为:

/var/www/html/domains/testing.com.dev 

这种类型的配置可能
适合大多数情况,但我不想
磁盘上我的文件夹中的 .dev 域的一部分。我能够实现
通过将 VirtualDocumentRoot 设置为:

VirtualDocumentRoot /var/www/html/domains/%-2+ 

上面的 testing.com.dev 示例现在将指向:

/var/www/html/domains/testing.com 

请记住将域名添加到您的
主机文件(/etc/hosts)

有关选项的完整列表,请参阅 mod_vhost_alias 文档。
可以在此处找到其他文档。

Maybe you can try the following solution from this article: Apache: Dynamic Virtual Hosts

A few months back I looked for a solution to overcome the problem of
creating individual Virtual Hosts in Apache every time I wanted to
configure a new site on a development machine (something that is a big
issue in work where we have a lot of websites). Apache is able to
support this functionality relatively easy using a module and a few
lines in the configuration file. I set this up on Fedora 14, so
results may be slightly different for other OS's (different paths,
configuration file setup, etc)

Open up the main Apache conf (/etc/httpd/conf/httpd.conf), and ensure
the module mod_vhost_alias is enabled. There should be a line in the
configuration like

LoadModule vhost_alias_module modules/mod_vhost_alias.so 

Next, add the
following lines to the bottom of this file. You'll need to edit the
file with sudo privileges.

NameVirtualHost *:80

UseCanonicalName Off

<VirtualHost *:80>
    VirtualDocumentRoot /var/www/html/domains/%0 
</VirtualHost>

This sets up a catch all for any domain coming in over port 80 (the
default port for http traffic, if your using https you will need to
use 443 - alternatively you could remove the port restriction). The
important line here is the VirtualDocumentRoot. The tells Apache where
your files will reside on disk. The %0 part takes the whole domain
name and inserts it into the path. To illustrate this if we went to a
domain testing.com.dev the VirtualDocumentRoot would be:

/var/www/html/domains/testing.com.dev 

This type of configuration might
be suitable for most situations, however I didn't want to have the
.dev part of the domain in my folders on disk. I was able to achieve
this by setting the VirtualDocumentRoot to:

VirtualDocumentRoot /var/www/html/domains/%-2+ 

The above example of testing.com.dev would now point to:

/var/www/html/domains/testing.com 

Remember to add the domain to your
hosts file (/etc/hosts)

For a full list of options see the mod_vhost_alias documentation.
Additional documentation can be found here.

圈圈圆圆圈圈 2025-01-05 17:53:41

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文