简易虚拟主机¶
- 目录
- 简易虚拟主机
模块: mod_simple_vhost
描述¶
简单假设
每个虚拟主机都位于一个基目录下,其路径与虚拟主机的名称相同。在此虚拟主机路径下可能还有一个额外的目录,作为虚拟主机的文档根目录。
每个虚拟主机的文档根目录由三个值构成
- server-root
- 主机名
- document-root
完整的文档根目录通过以下方式构建:
server-root + hostname + document-root
或者如果此路径不存在,则通过以下方式构建:
server-root + default-host + document-root
一个简单的例子应该能说明这个想法
/var/www/ /var/www/logs/ /var/www/servers/ /var/www/servers/example.org/ /var/www/servers/example.org/lib/ /var/www/servers/example.org/pages/ /var/www/servers/mail.example.org/ /var/www/servers/mail.example.org/lib/ /var/www/servers/mail.example.org/pages/ simple-vhost.server-root = "/var/www/servers/" simple-vhost.default-host = "example.org" simple-vhost.document-root = "pages"
通过此设置,对 "example.org" 或 "something-else.example.org" 的请求将指向 /var/www/server/example.org/pages
,而对 "mail.example.org" 的请求将指向 /var/www/server/mail.example.org/pages
。您可以使用符号链接将多个主机名映射到同一个目录。
条件语句 vs. 简易虚拟主机¶
您必须记住,条件语句和简易虚拟主机之间会相互影响。
simple-vhost.server-root = "/var/www/servers/" simple-vhost.default-host = "example.org" simple-vhost.document-root = "pages" $HTTP["host"] == "news.example.org" { server.document-root = "/var/www/servers/news2.example.org/pages/" }
当请求 news.example.org
时,server.document-root
将被设置为 /var/www/servers/news2.example.org/pages/
,但 simple-vhost 会很快覆盖它。
如果 /var/www/servers/news.example.org/pages/
存在,则将使用该路径。如果不存在,则将使用 /var/www/servers/example.org/pages/
,因为它是默认路径。
要将条件语句与简易虚拟主机一起使用,您应该这样做
$HTTP["host"] != "news.example.org" { simple-vhost.server-root = "/var/www/servers/" simple-vhost.default-host = "example.org" simple-vhost.document-root = "pages" } $HTTP["host"] == "news.example.org" { server.document-root = "/var/www/servers/news2.example.org/pages/" }
这将为除 news.example.org
之外的所有主机启用简易虚拟主机。
对于两个或更多主机
$HTTP["host"] !~ "^(test1\.example\.org|test2\.example\.org)$" { simple-vhost.server-root = "/var/www" simple-vhost.document-root = "/html/" ## the default host if no host is sent simple-vhost.default-host = "example.org" } $HTTP["host"] == "test1.example.org" { server.document-root = "/home/user/sites/test1.example.org/" accesslog.filename = "/home/user/sites/logs/test1.example.org.access.log" } $HTTP["host"] == "test2.example.org" { server.document-root = "/home/user/sites/test2.example.org" accesslog.filename = "/home/user/sites/logs/test2.example.org.access.log" }
这将为除 test1.example.org
和 test2.example.org
之外的所有主机启用简易虚拟主机。
当然,如果您像示例中那样操作,您需要为文件夹设置权限(将所有者更改为运行 lighttpd 的用户)。
选项¶
simple-vhost.server-root¶
虚拟主机的根目录
simple-vhost.default-host¶
如果请求的主机名没有自己的目录,则使用此主机名
simple-vhost.document-root¶
虚拟主机目录下的路径
simple-vhost.debug¶
如果非零,当构建的路径不存在或不是目录时,会将跟踪信息打印到 lighttpd 错误日志中。
提示¶
当您将 mod_simple_vhost 与 mod_cache 和 mod_proxy 一起使用时,请确保 mod_simple_vhost 是 'server.modules' 列表中的第一个模块。
您可以使用 'or' 语法配置具有多个域名的虚拟主机,例如
$HTTP["host"] =~ "^(hosta\.example\.org|hostb\.example\.net)$" { ... virtualhost configuration here ... }
WWW¶
您不需要为 example.com 和 www.example.com 使用两个条目。以下语法将作为 example.com 及其下所有子域的通用匹配:
$HTTP["host"] =~ "(^|\.)example\.com$" { ... }
(对您不起作用?如果您没有在默认端口上运行,$HTTP["host"] 将附加端口号,并且上述正则表达式将无法匹配它,因为末尾的 "$" 意味着“字符串以此方式结束”。)