lighty 的生活

lighty 开发者博客

通过包含文件简化您的配置文件

或者应该命名为“虚拟主机轻松实现?” 无论如何。如果您的虚拟主机设置看起来有点复杂,例如有不同的选项(仅静态、PHP 支持、预安装的 Rails 应用程序等),但每个设置都相似,那么您就无法使用虚拟主机模块,必须手动编写所有内容。但通过包含文件和变量,我们可以得到解决。

其思想是将配置文件模块化,并仅包含虚拟主机所需的配置部分。

但首先我们定义我们的设置

  1. 所有虚拟主机都在 __ /var/www/servers/ 下/pages/ __
  2. 您有一些虚拟主机拥有受保护的文件夹
  3. 有些可能支持 PHP,有些可能使用预安装的 Rails 应用程序

经典方式是


$HTTP[“host”] == “www.example.org” {
server.document-root = “/var/www/servers/www.example.org/pages/”
auth.backend = “htpasswd”
auth.backend.htpasswd.userfile = “/var/www/servers/www.example.org/htpasswd”
auth.require = …
}

我们两次都指定了完整的路径。让我们先解决这个问题

$HTTP["host"] == "www.example.org" {
  var.basedir = "/var/www/servers/www.example.org/" 
  server.document-root = basedir + "pages/"
  auth.backend = "htpasswd"
  auth.backend.htpasswd.userfile = basedir + "/htpasswd"
  auth.require = ... 
}

但这只是一个主机,让我们再添加一个,只提供静态文件

$HTTP["host"] == "www.example.com" {
  var.basedir = "/var/www/servers/www.example.com/" 
  server.document-root = basedir + "pages/"
}

两者都有相同的目录根 __ /var/www/servers/ __,让我们将其移出来。

var.basedir = "/var/www/servers/"
$HTTP["host"] == "www.example.org" {
  var.servername = "www.example.org"

  server.document-root = basedir + servername + "/pages/"
  auth.backend = "htpasswd"
  auth.backend.htpasswd.userfile = basedir + servername + "/htpasswd"
  auth.require = ... 
}
$HTTP["host"] == "www.example.com" {
  var.servername = "www.example.com"

  server.document-root = basedir + servername + "/pages/"
}

现在,server.document-root 设置对于两个服务器都是完全相同的,让我们再次将其移出,这次移到一个名为 __ incl-docroot.conf __ 的包含文件中。

## set the docroot based on basedir and servername 
## both have to be defined before
  server.document-root = basedir + servername + "/pages/"

这是我们的新配置文件

var.basedir = "/var/www/servers/"
$HTTP["host"] == "www.example.org" {
  var.servername = "www.example.org"

  include "incl-docroot.conf"

  auth.backend = "htpasswd"
  auth.backend.htpasswd.userfile = basedir + servername + "/htpasswd"
  auth.require = ( "/download" => ... )
}
$HTTP["host"] == "www.example.com" {
  var.servername = "www.example.com"

  include "incl-docroot.conf"
}

最后一步是将认证部分也移到一个名为 __ incl-auth-htpasswd.conf __ 的包含文件中。

## set authentificate for a directory
auth.backend = "htpasswd"
auth.backend.htpasswd.userfile = basedir + servername + "/htpasswd"
auth.require = ( authdir => ... )

以及我们的配置文件

var.basedir = "/var/www/servers/"
$HTTP["host"] == "www.example.org" {
  var.servername = "www.example.org"
  var.authdir = "/download/"

  include "incl-docroot.conf"
  include "incl-auth-htpasswd.conf"
}
$HTTP["host"] == "www.example.com" {
  var.servername = "www.example.com"

  include "incl-docroot.conf"
}

好的,最后一步是主机的 FastCGI。我们从一开始就创建一个名为 __ incl-fastcgi-php.conf __ 的包含文件。

fastcgi.server = ( ".php" => ((
  "bin-path" => "/usr/bin/php-cgi",
  "socket" => basedir + servername + "/tmp/php-" + PID + ".socket"
)))

如果主机需要 PHP 支持,我们只需包含此文件。

var.basedir = "/var/www/servers/"
$HTTP["host"] == "www.example.org" {
  var.servername = "www.example.org"
  var.authdir = "/download/"

  include "incl-docroot.conf"
  include "incl-auth-htpasswd.conf"
}
$HTTP["host"] == "www.example.com" {
  var.servername = "www.example.com"

  include "incl-docroot.conf"
  include "incl-fastcgi-php.conf"
}

这就引出了最后一个问题:一个虚拟主机有多个名称。比方说 __ www.example.org __ 和 __ example.org __ 是同一个虚拟主机,只是名称不同。

var.basedir = "/var/www/servers/"
$HTTP["host"] =~ "^(www\.)?example\.org$" {
  var.servername = "www.example.org"
  var.authdir = "/download/"

  include "incl-docroot.conf"
  include "incl-auth-htpasswd.conf"
}
$HTTP["host"] == "www.example.com" {
  var.servername = "www.example.com"

  include "incl-docroot.conf"
  include "incl-fastcgi-php.conf"
}

现在,生成适用于您设置的这些模块化配置文件的 Web 前端就交给您来编写了。

包含文件和变量自 1.4.0 版本起可用,PID 变量自 1.4.6 版本起可用,如果我没记错的话(IIRC)。

请注意,我们不接受超过3个月的帖子评论! 此外,请使用我们的错误追踪器报告错误,并使用我们的 IRC 频道 #lighttpd@libera 进行聊天。

« lighttpd 1.4.8 和多个 Rails 应用程序 新闻报道 »