【问题标题】:Apache: <Directory> directive with no path specifiedApache:没有指定路径的 <Directory> 指令
【发布时间】:2014-01-02 16:28:39
【问题描述】:

我有一个使用 MAMP 在我的 OS X 机器上运行的本地服务器环境,我目前正在设置一些虚拟主机。我发现,为了让我的一个虚拟主机识别我网站的.htaccess 文件,我需要在&lt;VirtualHost&gt; 指令中添加一个&lt;Directory&gt; 指令。

这是我用来配置我的虚拟主机之一的示例:

<VirtualHost *>
    DocumentRoot "/path/to/mysite"
    ServerName mysite.local
    <Directory "/path/to/mysite">
        AllowOverride All
        Order Allow,Deny
        Allow from all
    </Directory>
</VirtualHost>

现在,我想通过从 &lt;Directory&gt; 指令中删除路径来避免冗余。我尝试这样做并且它似乎有效,尽管我对 Apache 的熟悉程度不足以了解这样做的潜在后果。此外,我在 Apache 文档中找不到对这种情况的解释。

这是我喜欢做的:

<VirtualHost *>
    DocumentRoot "/path/to/mysite"
    ServerName mysite.local
    <Directory>
        AllowOverride All
        Order Allow,Deny
        Allow from all
    </Directory>
</VirtualHost>

因此,我的问题是:当我从 &lt;Directory&gt; 指令中省略路径(在这种情况下)时会发生什么?

【问题讨论】:

    标签: apache directory virtualhost httpd.conf directive


    【解决方案1】:

    Directory 是一个针对文件系统目录的指令,并且应该始终有一个路径(允许使用外配符)。

    使用一些针对无目录的指令可能会被无声地丢弃,或者可能会出现错误(您可以测试它),但肯定会无用

    你为什么要写一些完全没有目录的配置指令

    但您的问题可能还有更多内容。

    通常一个Virtualhost应该至少包含一个Directory指令,DocumentRoot目录,通常我们也会添加一个针对文件系统的根目录,这样:

    <Directory />
        (instructions to deny access and avoid .htaccess reads)
    </Directory>
    <Directory /my/document/root/>
        (instructions to allow access and still avoid .htaccess reads or to allow them if you are lazy and feel over confident that .htaccess are useful in any way and you do not need speed)
    </Directory>
    

    您还可以添加更多内容,尤其是避免使用 .htaccess 文件,因为存储在 /path/to/somewhere 中的文件与 &lt;Directory /path/to/somewhere&gt; 部分相同,但速度较慢。

    现在您不喜欢在您的 VH 配置中重复自己,因此您可以使用 .htaccess 文件或全局级别的指令来处理通常在那里设置的内容。您还可以使用另一个神奇的关键字,即 Location

    您可以在 &lt;Location /foo&gt; 指令中使用大量 Apache 指令。位置适用于 url,而不适用于文件系统路径。

    所以&lt;Location /&gt; 是您的documentRoot,&lt;Location /foo&gt; 可能是您DocumentRoot 下的子目录'foo'。这可以帮助您在 VH 中编写内容,而无需在任何地方重用 documentroot 前缀。但是小心,url 不是文件系统路径。与应用在目录级别的保护相比,在 url 级别应用的任何保护都可能更弱或更强。这取决于您的别名、您使用 url 的方式等。

    更新

    如果您使用新的 Apache 2.4 版本,您现在可以使用mod_macro 或更简单的built-in variables support

    Define docroot "/path/to/mysite"
    DocumentRoot ${docroot}
    <Directory ${docroot}>
        (...)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-28
      相关资源
      最近更新 更多