可能为同一个子域创建多个虚拟文档根目录?

时间:2015-12-15 23:54:46

标签: apache apache2 virtualhost

我正在设置一个开发服务器,我想知道是否可以使用虚拟文档根设置多个虚拟主机作为同一子域的后备?

为了澄清,我说这个虚拟主机基本上允许ex1.somedomain.com下的动态子域名,文件夹名称将成为子域名。这一切都很好。

<VirtualHost [ip address here]:80>
   UseCanonicalName off
   VirtualDocumentRoot /home/firstuser/www/%1/public_html/
   ServerAlias *.ex1.somedomain.com

   <Directory /home/firstuser/www/*/public_html/>
      AllowOverride all
   </Directory>

   AddHandler php5-fcgi .php
   Action php5-fcgi /php5-fcgi
   Alias /php5-fcgi /home/firstuser/www/php5-fcgi
   FastCgiExternalServer /home/firstuser/www/php5-fcgi -socket /tmp/php5-fpm-firstuser.sock -pass-header Authorization -idle-timeout 600
</VirtualHost>

那说..出于组织和权限相关的原因,我想我可以在它之后添加另一个虚拟主机,如果该文件夹在第一个虚拟文档根目录中不存在则可以作为后备。例如,如果/home/firstuser/www/test不存在,它将尝试下一个虚拟主机并查看是否存在/home/seconduser/www/test并提供该内容(如果有)但它似乎不起作用。

<VirtualHost [ip address here]:80>
   UseCanonicalName off
   VirtualDocumentRoot /home/seconduser/www/%1/public_html/
   ServerAlias *.ex1.somedomain.com

   <Directory /home/seconduser/www/*/public_html/>
      AllowOverride all
   </Directory>

   AddHandler php5-fcgi .php
   Action php5-fcgi /php5-fcgi
   Alias /php5-fcgi /home/seconduser/www/php5-fcgi
   FastCgiExternalServer /home/seconduser/www/php5-fcgi -socket /tmp/php5-fpm-seconduser.sock -pass-header Authorization -idle-timeout 600
</VirtualHost>

配置这样的东西的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

如果文件不存在,则没有内置支持从一个虚拟主机跳转到另一个虚拟主机,但mod_rewrite手册中有一些方法可以向您展示如何在将URI更改为文件系统映射之前在文件系统中查找

http://httpd.apache.org/docs/2.4/rewrite/remapping.html#multipledirs

RewriteEngine on

#   first try to find it in dir1/...
#   ...and if found stop and be happy:
RewriteCond         "%{DOCUMENT_ROOT}/dir1/%{REQUEST_URI}"  -f
RewriteRule "^(.+)" "%{DOCUMENT_ROOT}/dir1/$1"  [L]

#   second try to find it in dir2/...
#   ...and if found stop and be happy:
RewriteCond         "%{DOCUMENT_ROOT}/dir2/%{REQUEST_URI}"  -f
RewriteRule "^(.+)" "%{DOCUMENT_ROOT}/dir2/$1"  [L]

#   else go on for other Alias or ScriptAlias directives,
#   etc.
RewriteRule   "^"  "-"  [PT]