配置语法¶
概览¶
lighttpd 配置语法是基础的,许多配置都可以简单地用配置语法表达。
然而,配置语法并非一种完整的编程语言,它也无意于此。对于任何复杂的逻辑,建议创建生成 lighttpd 配置语法作为输出的脚本。
基本语法的BNF类表示法¶
option : NAME = VALUE merge : NAME += VALUE replace : NAME := VALUE (replace/overwrite earlier value) (since 1.4.46) NAME : modulename.key VALUE : ( <string> | <integer> | <boolean> | <array> | VALUE [ + VALUE ]*) INCLUDE : "include" <string> INCLUDE_SHELL : "include_shell" <string> <string> : '"' [text] '"' (can be constructed from multiple stringifiable values: VALUE [ + VALUE ]*) <integer>: digit+ <boolean>: ( "enable" | "disable" | 1 | 0 ) <array> : "(" [ [ <string> "=>" ] <value>, ]* ")" (key => value) list : "(" [ <string> "=>" <value>, ]* ")" value list : "(" [ <value>, ]* ")" LOCAL_VARIABLE : "var." NAME : "var." NAME ( = | += | := ) VALUE ENVIRONMENT_VARIABLE: "env." NAME (lighttpd.conf environment at startup)
示例
# default document-root server.document-root = "/var/www/example.org/pages/" # TCP port server.port = 80 # selecting modules server.modules = ( "mod_access", "mod_rewrite" ) # variables, computed when config is read. var.mymodule = "foo" server.modules += ( "mod_" + var.mymodule ) # var.PID is initialised to the pid of lighttpd before config is parsed # include, relative to dirname of main config file include "mime.types.conf" # read configuration from output of a command include_shell "/usr/local/bin/custom-create-mime.conf.pl /etc/mime.types"
条件配置¶
大多数(非全局)选项可以使用以下语法(包括嵌套)进行条件配置。
<field> <operator> <value> { # (since 1.4.74, may be preceded by literal "if") ... <field> <operator> <value> { ... nesting: match only when parent match } } else <field> <operator> <value> { # (since 1.4.74, "elif", "elsif", "elseif", or "else if" are also accepted in lieu of "else") ... the "elseif" block } else { # (since 1.4.46) ... the "else" block }
其中 <field> 是以下之一
字段名称 | 描述 |
---|---|
$REQUEST_HEADER["..."] | 匹配任意 HTTP 请求头(不区分大小写)(自 1.4.46 起) |
$HTTP["request-method"] | 匹配请求方法(自 1.4.19 起) |
$HTTP["scheme"] | 匹配传入连接使用的方案。可以是“http”或“https”(自 1.4.19 起) |
$HTTP["host"] | 匹配主机 |
$HTTP["url"] | 匹配 URL 路径(不包括主机或查询字符串) |
$HTTP["querystring"] | 匹配查询字符串,例如在此类 URL 中问号后的部分:index.php?module=images... |
$HTTP["remoteip"] | 匹配远程 IP 地址或远程网络 == 或 != CIDR 掩码(自 1.4.40 起支持 IPv6) |
$HTTP["cookie"] | (自 1.4.46 起被 $REQUEST_HEADER["Cookie"] 取代)匹配 Cookie |
$HTTP["useragent"] | (自 1.4.46 起被 $REQUEST_HEADER["User-Agent"] 取代)匹配 User-Agent |
$HTTP["language"] | (自 1.4.46 起被 $REQUEST_HEADER["Accept-Language"] 取代)(自 1.4.21 起)匹配 Accept-Language |
$HTTP["referer"] | (自 1.4.46 起被 $REQUEST_HEADER["Referer"] 取代)匹配 Referer |
$SERVER["socket"] | 匹配套接字(本地地址,非远程地址)。值必须是“ip:port”格式——其中 ip 是可选的 IP 地址,port 是端口号——或者必须是 Unix 域路径。如果省略 IP 地址,则使用 INADDR_ANY (0.0.0.0),除非server.use-ipv6 = "enable" 在此块内,在这种情况下使用 in6addr_any ([::])。使用 == 设置此指令还会指示 lighttpd 绑定到此套接字并监听请求。$SERVER["socket"] 在全局范围有效;将 $SERVER["socket"] 放置在其他条件中可能会产生不良结果(如果不是因为历史上的(误)用,则会被拒绝)。 |
<operator> 是以下之一
操作符 | 值 |
---|---|
== | 字符串相等匹配 |
!= | 字符串不相等匹配 |
=~ | Perl 风格正则表达式匹配 |
!~ | Perl 风格正则表达式不匹配 |
=^ | 字符串前缀匹配(自 1.4.65 起) |
=$ | 字符串后缀匹配(自 1.4.65 起) |
而 <value> 是一个带引号 ("") 的字符串,可以是字符串字面量或 正则表达式。
示例
# disable directory-listings for /download/* dir-listing.activate = "enable" $HTTP["url"] =~ "^/download/" { dir-listing.activate = "disable" } # handle virtual hosting # map all domains of a top-level-domain to a single document-root $HTTP["host"] =~ "(^|\.)example\.org$" { server.document-root = "/var/www/htdocs/example.org/pages/" } # multiple sockets $SERVER["socket"] == "127.0.0.1:81" { server.document-root = "..." } $SERVER["socket"] == "127.0.0.1:443" { ssl.pemfile = "/var/www/certs/localhost.pem" ssl.engine = "enable" server.document-root = "/var/www/htdocs/secure.example.org/pages/" } # deny access for all googlebot $HTTP["useragent"] =~ "Google" { url.access-deny = ( "" ) } # deny access for all image stealers (anti-hotlinking for images) $HTTP["referer"] !~ "^($|http://www\.example\.org)" { url.access-deny = ( ".jpg", ".jpeg", ".png" ) } # deny the access to www.example.org to all user which # are not in the 10.0.0.0/8 network $HTTP["host"] == "www.example.org" { $HTTP["remoteip"] != "10.0.0.0/8" { url.access-deny = ( "" ) } } # Allow only 200.19.1.5 and 210.45.2.7 to # have access to www.example.org/admin/ $HTTP["host"] == "www.example.org" { # !~ is a perl style regular expression not match $HTTP["remoteip"] !~ "^(200\.19\.1\.5|210\.45\.2\.7)$" { $HTTP["url"] =~ "^/admin/" { url.access-deny = ( "" ) } } }
条件配置合并¶
lighttpd 配置在启动时解析并优化。配置在启动后是静态的。
在运行时,动态配置选择仅限于使用 lighttpd 条件语法和静态配置评估请求,例如与启动时解析的静态列表进行匹配。
换句话说,各种配置对象在启动后是静态的,包括后端服务器配置。在运行时解析请求时,可能会选择一个后端服务器,但后端配置在启动后是静态的。
配置不同部分中的选项不会在运行时动态合并。对于给定选项,该选项在最后匹配条件中的值是应用的实际值。在相同的 lighttpd 条件 {
... }
和相同的 {
... }
嵌套级别内,可以使用 +=
,它在启动时解析,但 +=
不适用于不同的 lighttpd 条件 {
... }
或条件内的不同嵌套级别 {
... }
。
在相同的范围级别(相同的 { } 嵌套级别),相同的条件块在启动时的解析过程中会合并。请注意,如果条件在相同的作用域级别(例如在其他 if-else 语句中)已经存在,则在 if-else 语句中重复条件可能会导致语法错误。同样,配置语法并非一种完整的编程语言,它也无意于此。对于任何复杂的逻辑,建议创建生成 lighttpd 配置语法作为输出的脚本。
故障排除¶
如果您没有在默认端口上运行,$HTTP["host"] 将附加端口,因此以 $ 结尾(不允许端口)的 正则表达式将不匹配。
要匹配带或不带端口,请将
"(^|\.)example\.org$"改为
"(^|\.)example\.org(\:[0-9]*)?$"
请注意,某些早期版本的 lighttpd 不支持此处列出的完整配置文件语法。特别是,某些版本不支持“var.”变量、使用“+=”进行追加、嵌套条件或“else”块。某些选项的名称(例如,“server.dir-listing”)在 lighttpd 版本之间也已更改(即更改为“dir-listing.activate”)。
如果您在配置 lighttpd 时遇到问题,请考虑使用“-t”或“-p”选项来调试您的配置。请注意,某些早期版本的 lighttpd 不支持“-t”或“-p”选项。
高级用法¶
查看博客:https://blog.lighttpd.ac.cn/articles/2005/05/07/advanced-configuration-in-up-upcoming-1-4-x
使用变量¶
您可以在配置中设置自己的变量以简化配置。
注意:变量在启动时解析配置时展开;变量不会在运行时为每个请求展开。
var.basedir = "/home/www/servers/" $HTTP["host"] == "www.example.org" { server.name = "www.example.org" include "incl-base.conf" }
在 incl-base.conf 中
server.document-root = basedir + server.name + "/pages/" accesslog.filename = basedir + server.name + "/logs/access.log"
您还可以使用环境变量或默认变量 var.PID 和 var.CWD。
var.basedir = env.LIGHTTPDBASE $HTTP["host"] == "www.example.org" { server.name = "www.example.org" include "incl-base.conf" include "incl-fastcgi.conf" }
在 incl-fastcgi.conf 中
fastcgi.server = ( ... => (( "socket" => basedir + server.name + "/tmp/fastcgi-" + PID + ".sock" )) )
或者像用于 Rails 的 lighttpd 脚本那样
var.basedir = var.CWD server.document-root = basedir + "/public/"
限制¶
(非详尽列表)
配置语法并非一种完整的编程语言,它也无意于此。对于任何复杂的逻辑,建议创建生成 lighttpd 配置语法作为输出的脚本。
lighttpd 配置中无法完成的一些有用事情:(作为替代,您可以在真实的编程语言中创建一个脚本,然后使用 include_shell。)
# testing if a variable has been set is NOT possible # INVALID var.not_sure_if_it_exists == undefined { ... set to default value ... } # INVALID # removing from arrays is NOT possible # INVALID server.modules -= ( "mod_idontwantyou" ) # INVALID
全局上下文¶
global { ... }
您不需要在主配置文件中使用它。
但是您可能难以在条件句的包含文件中设置服务器范围的配置。
示例
在 lighttpd.conf 中
server.modules = () $HTTP["host"] == "www.example.org" { include "incl-php.conf" }
在 incl-php.conf 中
global { server.modules += ("mod_fastcgi") static-file.exclude-extensions += (".php") } fastcgi.server = "..."
选项¶
所有配置选项都可以在以下位置找到:配置选项