显示标签为“lighttpd”的博文。显示所有博文
显示标签为“lighttpd”的博文。显示所有博文

2008年8月12日星期二

lighttpd 配置文件详解 Configuration File

Configuration File
Module: core ¶

Table of Contents

* Description
o Basic Syntax
o Example
* Conditional Configuration
o Example
o Troubleshooting
* advanced usage
* Using variables
* Global context
o Example
* Options

Description
Basic Syntax

A BNF like notation:

option : NAME = VALUE
merge : NAME += VALUE
NAME : modulename.key
VALUE : ( | | | | VALUE [ + VALUE ]*)
: "text"
: digit*
: ( "enable" | "disable" )
: "(" [ "=>" ] [, [ "=>" ] ]* ")"
INCLUDE : "include" VALUE
INCLUDE_SHELL : "include_shell" STRING_VALUE

Example

# 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/confmimetype /etc/mime.types"

Conditional Configuration

Most options can be configured conditionally by using the following syntax (including nesting).

{
...
{
... nesting: match only when parent match
}
}
else {
... the "else if" block
}

where is one of one of the following:

$HTTP["cookie"]
match on cookie
$HTTP["host"]
match on host
$HTTP["useragent"]
match on useragent
$HTTP["referer"]
match on referer
$HTTP["url"]
match on url. If there are nested blocks, this must be the most inner block.
$HTTP["querystring"]
match on querystring, eg, after the ? in this type url: index.php?module=images..
$HTTP["remoteip"]
match on the remote IP or a remote Network (Warning: doesn't work with IPv6 enabled)
$HTTP["scheme"] (Introduced in version 1.4.19)
match on the scheme used by the incoming connection. This is either "http" or "https".
$SERVER["socket"]
match on socket. Value must be on the format "ip:port" where ip is an IP address and port a port number, or ":port" to match port only. Only equal match (==) is supported. It also binds the daemon to this socket. Use this if you want to do IP/port-based virtual hosts.
$PHYSICAL["path"] (Introduced in version 1.5.0)
match on the mapped physical path of the file / cgi script to be served.
$PHYSICAL["existing-path"] (Introduced in version 1.5.0)
match on the mapped physical path of the file / cgi script to be served only if such a file exists on the local filesystem.

is one of:

==
string equal match
!=
string not equal match
=~
perl style regular expression match
!~
perl style regular expression not match

and is either a quoted ("") literal string or regular expression.
Example

# 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 = ( "" )
}
}
}

Troubleshooting

If you're not running on the default port, $HTTP["host"] will have the port appended to it, so regular expressions ending in "$" (without allowing for a port) won't match. To match with or without a port, change "(^|\.)example\.org$" to "(^|\.)example\.org(\:[0-9]*)?$"
advanced usage

check the blog: http://blog.lighttpd.net/articles/2005/05/07/advanced-configuration-in-up-upcoming-1-4-x
Using variables

You can set your own variables in the configuration to simplify your config.

var.basedir = "/home/www/servers/"
$HTTP["host"] == "www.example.org" {
server.name = "www.example.org"
include "incl-base.conf"
}

in incl-base.conf:
server.document-root = basedir + server.name + "/pages/"
accesslog.filename = basedir + server.name + "/logs/access.log"

You can also use environment variables or the default variables var.PID and var.CWD:

var.basedir = env.LIGHTTPDBASE

$HTTP["host"] == "www.example.org" {
server.name = "www.example.org"
include "incl-base.conf"
include "incl-fastcgi.conf"
}

in incl-fastcgi.conf:
fastcgi.server = ( ... => ((
"socket" => basedir + server.name + "/tmp/fastcgi-" + PID + ".sock"
)) )

Or like the lighttpd script for rails does:

var.basedir = var.CWD

server.document-root = basedir + "/public/"

Global context

global {
...
}

You don't need it in the main configuration file. But you might have difficulty setting a server wide configuration inside a included-file from conditionals.
Example

in lighttpd.conf:
server.modules = ()
$HTTP["host"] == "www.example.org" {
include "incl-php.conf"
}

in incl-php.conf:
global {
server.modules += ("mod_fastcgi")
static-file.exclude-extensions += (".php")
}
fastcgi.server = "..."

Options

All Configuration Options

Windows-Install

主題:在 Windows 系列安裝 Lighttpd
作業環境:Windows XP Professional
軟體版本:

* Windows XP Professional
* Lighttpd 1.4.18-1

說明:好用又輕量級的 Lighttpd 已經廣為 Unix 系列作業系統採用,然而 Windows 系列難道就只能流口水嗎?當然網路上的有高手將 Lighttpd 編譯成 Windows 系列版本,所以就可以享受到 Lighttpd 的好。
版本更新

* 2008.2.14 新增頁面

最後更新

* 2008 年 02 月 24 日,04:57 AM

1.下載 Lighttpd for Windows
http://wlmp.dtech.hu/down_lighty.php?type=ssl&lang=en
download-lighttpd-for-windows-1.png
有兩個版本可以選擇,如果不需要 SSL 連線,就選擇 Without OpenSSL Support 大約會少 1MB

download-lighttpd-for-windows-2.png

選擇 Download Site,因為 Lighttpd for Windows 是匈牙利人開發的,所以主要的 Download Site 在匈牙利,所以可以選擇 Google 的 mirror site 台灣對美國的連線是比較快的。

2.安裝 Lighttpd for Windows
基本上有安裝精靈,一直下一步就可以了。

3.以服務的方式啟動 Lighttpd
安裝完後,啟動 Lighttpd 要以服務的方式啟動,根據我測試的結果,手動啟動會有問題,所以要以 Windows 服務的方式啟動 Lighttpd。

首先到安裝的目錄裡面[C:\Program Files\LightTPD\bin]執行[Service-Install.exe]程式會將 Lighttpd 安裝成 Windows 服務,之後再用瀏覽器瀏覽[http://127.0.0.1],有出現 LightTPD Test Page 就表示成功了。

4.資料夾說明
htdocs 首頁及其他網頁放置在這邊
conf 放設定檔的地方
logs

lighttpd来代替apache

这两天试用了lighttpd来代替bbs使用的apache,印象深刻

bbs的在使用fweb之后一直使用unix下的标准服务器apache作为web服务器。

但是apache表现却不怎么令人满意。

因为使用了php,所以还是用apache的1.3版本。

但是由于并发很高,导致随时有200+apache进程在运行,而且每个进程占用的内存随时间增加越来越多(可能是php的问题)。

最后可能占用超过700M的内存。同时apache导致系统用于nice的cpu也很高,一般在10%-20%之间。

也许可以通过一些设置,比如静态文件的缓存时间,减少每个apache进程的服务次数。

这些问题是由于apache的prefork工作模式有关。

每个apache进程只能同时服务于一个http连接。

这种模式好处在于每个进程不互相干扰,稳定性好;

缺点也建立在优点之上,就是占用资源多,即使每个进程只使用2M内存(如果使用了php,这点内存根本不够),
100的并发连接就用掉200M的内存。

不要以为100的并发很难达到,在低速连接占多数的时候,20req/s就能达到100的并发。
即使在高速连接占多数的情况(比如bbs),并发也是req/s的两倍左右。
在使用apache的时候,bbs的web高峰期连接大概在150req/s,即使关掉keepalive,还是要使用200多个进程来服务。
为了解决这个问题,我试用了其它一些http服务软件。
很多短小精悍的,比如thttpd,boa之类的,不过大多不能支持fastcgi,也就不能用了。
剩下有两个不错,一个是LiteSpeed Web Server(http://freshmeat.net/projects/lsws/),还有一个就是lighttpd。

先说LiteSpeed,这个不是开源软件,但标准版非商业使用是免费的,当然,只有二进制版本。

这个软件速度稳定性都不错,而且管理界面友好(web管理界面,他的配置文件是xml,很难配置),而且支持的功能也不少,
文档比较齐全。但配置起来却让我摸不着头脑,总是不能按照预期想法运行,由于没有形成一个社区,
网上资源也很少,最后只能放弃.

lighttpd(www.lighttpd.net)是一个开源软件,历史只有两三年。
但功能不弱,我想用到的fastcgi,url rewrite都有,而且最重要的是,性能很强劲。

在默认模式下,lighttpd使用poll()作为event handler,如果在linux2.4下,还可以选择rtsig作为event handler。

如果不使用大文件(>4G)支持,lighttpd使用sendfile()来发送文件,完全的zero-copy。

如果他的功能还不能满足你的要求,你甚至可以自己写一个plugin,满足需求。

缺点就是bug比较多,软件并不稳定,而且文档太简略。

尤其是使用内存,很难说清楚具体使用量,一般在10-20M(繁忙站点),但有时候会突发到100多M,并稳定下来。

不过相对apache的使用量,这个已经不算多。
同时,软件不稳定也很让人头痛,比如突然segfault,对于一个繁忙站点,这个是不能接受的!
文档简略也很让人头痛,有些功能需要你自己猜测才懂得怎么配置。

不过总得来说,试用下来的感觉很好,如果配置得当,在相同并发下,使用的cpu和内存远小于apache。

比如bbs,使用lighttpd后网页速度明显加快,高峰时期达到300+req/s,并发600+。但只使用了20多M内存,4%cpu。

具体配置可以参考网上的资料,下面说说要注意的地方:
1,编译的时候比较奇怪,我至今没弄清楚怎么选定只编译特定的模块。
看了一下configure文件,大概是只要相应的库存在,就会编译相应的模块。
比如如果有pcre的dev文件,就会编译rewrite模块。我只能放弃编译的静态版本的lighttpd的想法。
2,它的Conditional Configuration非常有用,可以实现很多想法。比如限制php只在特定目录下可以使用。简单的虚拟主机。
不过似乎$HTTP[\"url\"]的块里面不能使用mod_rewrite,只能二选其一。
3,lighttpd支持监听多端口,不过配置比较奇异,要使用Conditional Configuration。
比如$SERVER[\"socket\"] == \"0.0.0.0:8000\" {}
就会让lighttpd同时监听server.port和8000端口。注意,{}里面不能再配置server.port,否则会导致重复绑定端口而退出。
4,linux2.4下使用rtsig后,性能表现要比poll要好很多,推荐使用!只需要增加server.event-handler = \"linux-rtsig\"
到配置文件就可以了。

不过,软件实现方面有bug。在运行lighttpd时一定要加上-D参数,让lighttpd不自动运行到后台。
否则你会发现一切正常,但就是不能访问。

估计是进程在设定socket fd的owner process后再daemonize,系统不能发送连接信号到程序。
当然,一个程序运行于前台不是我们所需要的,我的解决方案是使用daemon这个软件包。
daemon是一个daemonize某个前台程序的工具,让人高兴的是,在子进程非正常退出的时候,daemon还会自动respawn!
这点有些类似于init进程,不过要比init好用点。
daemon可以指定respawn的次数,间隔时间,放弃时间等等,否则,一个配置错误会让daemon不停的respawn,让服务器假死。
这样就曲线解决了lighttpd不稳定的问题。

5,如果你的站点很繁忙,那就把keepalive时间和次数减少点,比如
server.max-keep-alive-requests = 5
server.max-keep-alive-idle = 5
否则,即使lighttpd很强劲,处理太多fd也不是好想法,而且很容易导致fd overflow。

6,lighttpd使用fastcgi模式兼容php(还有python,ruby,perl等等,只要能使用fastcgi模式)。
不过有一点兼容问题,否则PATH_INFO环境变量不太对,php会报告no input file,你需要在php fcgi server配置文件里增加
\"broken-scriptfilename\" => \"enable\" 然后在php.ini里面增加cgi.fix_pathinfo=1

lighttpd虽然不错,但大多情况下我还是推荐使用apache。
1,如果你不介意性能,或者有很充足的硬件,那就用apache吧,维护成本,bug成本,安全成本都会降低
2,你不希望出错,每一个连接都很重要,使用apache吧
3,你懒得配置lighttpd(确实要比apache复杂,如果你只是简单应用)
4,你要用php,但是fastcgi模式的php不兼容你的web应用。
5,你是apache fans

下面的情况,你需要选择lighttpd
1,你认为简单就是美,apache太庞大了
2,你没有充足的硬件,性能又很重要
3,你要尝试新事物
当然,你还可以选择同时运行apache和lighttpd,一个做前台,一个做后台。至于谁前谁后,似乎网上都有实例

下面是lighttpd官方网站给出的lighttpd特点,你可以参考参考;)

virtual hosts
virtual directory listings
URL-Rewriting, HTTP-Redirects
automatic expiration of files
Large File Support (64bit fileoffsets)
Ranges (start-end, start-, -end, multiple ranges)
on-the-fly output-compression with transparent caching
deflate, gzip, bzip2
authentication
basic, digest
backends: plain files, htpasswd, htdigest, ldap
fast and secure application controlled downloads
Server Side Includes
User Tracking
FastCGI, CGI, SSI
PHP-Support:
same speed as or faster than apache + mod_php4
includes a utility to spawn FastCGI processes (neccesary for PHP 4.3.x)
via FastCGI and CGI interface
support Code Caches like Turckmm, APC or eaccelarator
load-balanced FastCGI
(one webserver distibutes request to multiple PHP-servers via FastCGI)
Security features:
chroot(), set UID, set GID
protecting docroot
strict HTTP-header parsing