带有通配符 VirtualDocumentRoot 的 VirtualHost [英] VirtualHost with wildcard VirtualDocumentRoot

查看:42
本文介绍了带有通配符 VirtualDocumentRoot 的 VirtualHost的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的虚拟主机创建后备.我的配置如下:

# 获取所有预定义的主机包括conf/extra/vhosts/*.conf";# 倒退名称虚拟主机 *:80<目录C:/LocalServer/usr">选项索引 FollowSymLinks 包括允许覆盖所有命令允许,拒绝所有人都允许</目录><虚拟主机 *:80>VirtualDocumentRoot "C:/LocalServer/usr/%-1/projects/%-2+/public/";</虚拟主机>

这里的目标如下:如果我尝试访问 http://test.lab/,我希望它自动选择以下目录:C:/LocalServer/usr/lab/projects/test/public/.

现在,我已经创建了文件夹和一个空的索引文件(index.php).尽管如此,Apache 一直向我显示一个空的目录索引(Index of").

现在不太确定要做什么.尝试了一些东西,但似乎都不起作用.

有什么想法吗?

更新 - 6 月 1 日

我现在正在使用这个代码,基于第一个答案(好吧,唯一的一个):

使用规范名称关闭服务器别名 *.labVirtualDocumentRoot "C:/LocalServer/%2/%1/public"<目录C:/LocalServer/%2/%1/public">选项索引 FollowSymLinks 包括允许覆盖所有命令允许,拒绝所有人都允许</目录></虚拟主机>

我现在收到来自 Apache 的禁止访问错误.当目录不存在时,通常肯定会收到此错误?C:/LocalServer/lab/test/public 确实存在,并且一个空的 index.php 驻留在 public 目录中.

一般错误日志中的错误:[client 127.0.0.1:49342] AH01797: client denied by server configuration: C:/LocalServer/lab/test/public/

如果我删除 组,则没有任何变化.我仍然收到错误.(我什至可以在该组中使用 %n 吗?)

快速说明:

之前它不工作的原因是因为我通过 Include "conf/extra/vhosts/*.conf" 指令导入了其他虚拟主机.将其注释掉(从而使实验室规则成为唯一的规则)引发了禁止访问错误.

另请注意,我不再使用 usr 文件夹 - 每个实验室现在都在 lab 文件夹中,位于 LocalServer 下.

更新 2

似乎 块不允许插入变量,就像 VirtualDocumentRoot 一样.

更新 3 - 找到解决方案

它现在可以工作了 - 没有帮助就无法做到.最终代码如下:

使用规范名称关闭服务器别名 *.labVirtualDocumentRoot "C:/LocalServer/%2/%1/public"<目录C:/LocalServer/lab/*/public">期权索引 FollowSymLinks允许覆盖所有订单允许,拒绝所有人都允许</目录></虚拟主机>

更新 4(2015 年 4 月)

新指令,对于那些感兴趣的人(使用最新的 Apache 2.4):

使用规范名称关闭服务器别名 *.localVirtualDocumentRoot "D:/home/%-2+/public_html";<目录D:/home/*/public_html">要求所有授予允许覆盖所有期权索引 FollowSymLinks</目录></虚拟主机>

这与 Acrylic DNS Proxy 的结合,创造了奇迹.

更新 5(2016 年 12 月)

我现在使用 宏观方法.

# Directory Macro - 基于每个虚拟主机的默认目录配置<宏目录$dir><目录z:/var/www/$dir/public_html">要求所有授予选项包括索引 FollowSymLinks允许覆盖所有</目录># LocalSub 宏 - 对于需要自己的根的特定 *.*.local subs<Macro LocalSub $sub $domain><虚拟主机 127.0.0.1>服务器名称 $sub.$domain.localDocumentRoot z:/var/www/$domain/$sub/public_html"使用目录 $domain/$sub</虚拟主机>使用 LocalSub 博客 Rockettpw# 主虚拟主机<虚拟主机 127.0.0.1>使用规范名称关闭服务器别名 *.local *.*.localVirtualDocumentRoot z:/var/www/%-2/public_html"使用目录 *</虚拟主机>

解决方案

我使用它们 :) 你忘记关闭规范名称 - 不幸的是我不知道为什么我的配置中必须有 ServerAlias - 它只是不会没有它也能工作 - 下面的代码已经过测试并且可以正常工作

<目录C:/LocalServer/*/public">期权索引 FollowSymLinks允许覆盖所有命令允许,拒绝所有人都允许需要本地</目录><虚拟主机 *:80># Apache 将使用客户端提供的主机名形成 URL使用规范名称关闭# 可用的别名使用服务器别名 *.lab *.lab2# 把它们放在哪里VirtualDocumentRoot "C:/LocalServer/%2/%1/public/"</虚拟主机>

I'm trying to create a fallback for my virtual hosts. My configuration looks like this:

# Fetch all pre-defined hosts

Include "conf/extra/vhosts/*.conf"

# Fallback

NameVirtualHost *:80

<Directory "C:/LocalServer/usr">
    Options Indexes FollowSymLinks Includes
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>
<VirtualHost *:80>
    VirtualDocumentRoot "C:/LocalServer/usr/%-1/projects/%-2+/public/"
</VirtualHost>

The objective here is the following: If I try to access http://test.lab/, I want it to automatically pick up the following directory: C:/LocalServer/usr/lab/projects/test/public/.

Now, I have created the folders, and an empty index file (index.php). Nonetheless, Apache keeps showing me an empty Directory Index ("Index of").

No quite sure what to do now. Have tried a few things, none of which seem to work.

Any ideas?

Update - 1 June

I am now using this code, based on the first answer (well, the only one):

<VirtualHost *:80>
    UseCanonicalName Off
    ServerAlias *.lab
    VirtualDocumentRoot "C:/LocalServer/%2/%1/public"
    <Directory "C:/LocalServer/%2/%1/public">
        Options Indexes FollowSymLinks Includes
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

I now get an Access forbidden error from Apache. One would surely, normally, receive this error when the directory does not exist? C:/LocalServer/lab/test/public does exist, and an empty index.php resides in the public directory.

The error in the general error log: [client 127.0.0.1:49342] AH01797: client denied by server configuration: C:/LocalServer/lab/test/public/

If I remove the <Directory/> group, nothing changes. I still get the error. (Can I even use %n in that group?)

Quick Note:

The reason it wasn't working before was due to the fact that I had other Virtual Hosts being imported, by means of the Include "conf/extra/vhosts/*.conf" instruction. Commenting it out (and thus making the Labs rule the only one) initiated the Access forbidden error.

Also note that I am no longer using the usr folder - each Lab is now in the lab folder, under LocalServer.

Update 2

It seems that the <Directory/> block does not allow for variables to be inserted, like VirtualDocumentRoot does.

Update 3 - Solution Found

It is now working - would not have been able to do it without the help. Here's the final code:

<VirtualHost lab:80>
    UseCanonicalName Off
    ServerAlias *.lab
    VirtualDocumentRoot "C:/LocalServer/%2/%1/public"
    <Directory "C:/LocalServer/lab/*/public">
        Options Indexes FollowSymLinks
        AllowOverride All
        Order Allow,Deny
        Allow from all
    </Directory>
</VirtualHost>

Update 4 (April 2015)

New Directive, for those interested (using latest Apache 2.4):

<VirtualHost *:80>
    UseCanonicalName Off
    ServerAlias *.local
    VirtualDocumentRoot "D:/home/%-2+/public_html"
    <Directory "D:/home/*/public_html">
        Require all granted
        AllowOverride All
        Options Indexes FollowSymLinks
    </Directory>
</VirtualHost>

This, with the combination of Acrylic DNS Proxy, makes magic.

Update 5 (December 2016)

I'm now using a Macro approach.

# Directory Macro - Default Directory configuration on a per-vhost basis

<Macro Directory $dir>
    <Directory "z:/var/www/$dir/public_html">
        Require all granted
        Options Includes Indexes FollowSymLinks
        AllowOverride All
    </Directory>
</Macro>

# LocalSub Macro - For specific *.*.local subs that require their own root

<Macro LocalSub $sub $domain>
    <VirtualHost 127.0.0.1>
        ServerName $sub.$domain.local
        DocumentRoot "z:/var/www/$domain/$sub/public_html"
        Use Directory $domain/$sub
    </VirtualHost>
</Macro>

Use LocalSub blog rockettpw

# Main virtual host

<VirtualHost 127.0.0.1>
    UseCanonicalName Off
    ServerAlias *.local *.*.local
    VirtualDocumentRoot "z:/var/www/%-2/public_html"
    Use Directory *
</VirtualHost>

解决方案

I use them :) You forgot about switching off canonical names - unfortunately I don't know why there must be ServerAlias in my configuration - it just won't work without it - code below is tested and working

<Directory "C:/LocalServer/*/public">
    Options Indexes FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
    Require local
</Directory>

<VirtualHost *:80>
    # Apache will form URLs using the hostname supplied by the client
    UseCanonicalName Off

    # available aliases to use
    ServerAlias *.lab *.lab2

    # where to put them
    VirtualDocumentRoot "C:/LocalServer/%2/%1/public/"
</VirtualHost>

这篇关于带有通配符 VirtualDocumentRoot 的 VirtualHost的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆