【问题标题】:Wildcard domains with virtualhost with Apache on Mac带有虚拟主机的通配符域和 Mac 上的 Apache
【发布时间】:2014-10-26 16:35:21
【问题描述】:

我目前正在为本地开发运行多个域

http://wordpress.dev
http://phpmyadmin.dev
http://projectx.dev
http://projecty.dev
...

这些项目大部分位于用户“站点”目录中,但有些位于其他位置:

/Users/[username]/Sites/wordpress
/Users/[username]/Sites/phpmyadmin
/Users/[username]/Sites/projectx
/Users/[username]/OtherDirectory/projecty

我目前正在通过向 /etc/hosts/etc/apache2/extra/httpd-vhosts.conf 添加专用条目来进行所有设置

/etc/hosts:

127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost 
fe80::1%lo0     localhost
# Virtuelle Hosts
127.0.0.1       wordpress.dev
127.0.0.1       phpmyadmin.dev
127.0.0.1       projectx.dev
....

/etc/apache2/extra/httpd-vhosts.conf:

<VirtualHost *:80>
    ServerName [PROJECT].dev
    ServerAlias [PROJECT].dev
    DocumentRoot /Users/[username]/Sites/[PROJECT]/
    <Directory /Users/[username]/Sites/[PROJECT]/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>
</VirtualHost>
<VirtualHost *:80>
 ...

有没有办法使用某种通配符设置将域映射到某个目录,例如

http://foo.dev =>  /Users/[username]/Sites/foo
http://bar.dev =>  /Users/[username]/Sites/bar
...

在保持projecty 工作的同时(当然还有一些额外的设置),所以我只需要在Sites 中创建一个文件夹,它可以通过http://[foldername].dev 立即访问

[username] 可以硬编码

【问题讨论】:

    标签: apache localhost virtualhost


    【解决方案1】:

    使用mod_vhost_aliasVirtualDocumentRoot

    只要您没有将 foo.dev 捕获为 ServerNameServerAlias 的 VirtualHost,您应该能够执行以下操作:

    <VirtualHost *:80>
      ServerName default.dev
    
      VirtualDocumentRoot /Users/youruser/%-2
      ...
      ...
    </VirtualHost>
    

    您需要对用户名进行硬编码,尽管您说这不是问题。

    VirtualDocumentRoot 中的%-2 表示foo.com 的倒数第二个点分隔部分,即foo。然后,您可以根据需要将目录映射到站点:

    http://foo.dev =>  /Users/youruser/Sites/foo
    http://bar.dev =>  /Users/youruser/Sites/bar
    http://subdom.baz.dev =>  /Users/youruser/Sites/baz
    

    您需要确保您想以这种方式映射的任何其他域在您的主机文件中都有适当的条目,如果您使用的是 DNS,则需要确保该条目。

    【讨论】:

    • 你能给我一个完整的例子吗?这不起作用:pastebin.com/AMM895bu
    • 删除ServerAlias,如果您在foo.dev 的其他任何地方有ServerNamevhosts.conf,则需要将其删除。您需要此配置成为默认主机才能工作。
    • 不起作用:(。我已经加载了mod_vhost_alias,我没有定义foo.dev VirtualHost,我的etc/hosts文件中有127.0.0.1 default.dev,我在站点中有一个名为带有 index.html 文件的“foo”。有什么遗漏吗?
    • 似乎我需要将127.0.0.1 foo.dev 添加到etc/hosts 才能完成这项工作。也有机会获得这种“动态”
    • 也从您的主机文件中删除 default.dev。否则它可能会寻找 /Users/username/Sites/default。注释掉测试时可能加载的任何其他文件。
    【解决方案2】:

    为了完成你所需要的,你可以配置一个动态配置的海量虚拟主机。

    要拥有它,您应该在您的 httpd.conf 文件中添加如下内容:

    # get the server name from the Host: header
    UseCanonicalName Off
    
    # this log format can be split per-virtual-host based on the first field
    LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon
    CustomLog logs/access_log vcommon
    
    # include the server name in the filenames used to satisfy requests
    VirtualDocumentRoot /www/hosts/%0/docs
    VirtualScriptAlias /www/hosts/%0/cgi-bin 
    

    这将与以下内容相同:

    NameVirtualHost 111.22.33.44
    <VirtualHost 111.22.33.44>
        ServerName www.customer-1.com
        DocumentRoot /www/hosts/www.customer-1.com/docs
        ScriptAlias /cgi-bin/ /www/hosts/www.customer-1.com/cgi-bin
    </VirtualHost>
    <VirtualHost 111.22.33.44>
        ServerName www.customer-2.com
        DocumentRoot /www/hosts/www.customer-2.com/docs
        ScriptAlias /cgi-bin/ /www/hosts/www.customer-2.com/cgi-bin
    </VirtualHost>
    # blah blah blah
    <VirtualHost 111.22.33.44>
        ServerName www.customer-N.com
        DocumentRoot /www/hosts/www.customer-N.com/docs
        ScriptAlias /cgi-bin/ /www/hosts/www.customer-N.com/cgi-bin
    </VirtualHost> 
    

    更多详情,请查看here

    【讨论】:

      最近更新 更多