lighttpd
在error.log看到“(mod_fastcgi.c.2866) backend is overloaded, we disable
it for a 2 seconds and send the request to another backend instead:
reconnects: 3 load: 4147
/tmp/php.socket”时,说明lighttpd+fastcgi的load较高了。
可按如下步骤优化:
1) 首先检查swap使用情况。如果使用了swap,一般说明php进程数偏多,适当减少php进程。调整max-procs和
PHP_FCGI_CHILDREN的数值,根据公式num-procs = max-procs * ( 1 +
PHP_FCGI_CHILDREN
),启动时php5进程大概占13M内存,如果num-procs超过200,高峰期php就会占去2G内存,不大合适
2) 安装PHP加速软件。有XCache, eAccelerator和Zend Optimizer。推荐公开源代码的XCache,
eAccelerator
3) 调整Keep Alive参数。对于动态程序来说,Keep Alive的意义不大,可以关闭。
server.max-keep-alive-requests = 0
4) 优化程序代码。呵呵,一般也是最有效的;)
CODE:
#### fastcgi module
## read fastcgi.txt for more info
fastcgi.server
= ( ".php"
=>
(
"localhost" =>
"socket"
=> "/tmp/php-fcgi.sock",
"bin-path"
=> "/opt/bin/php-fcgi",
"max-procs"
=> 2,
"bin-environment"
=> (
"PHP_FCGI_MAX_REQUESTS" => "100"
"PHP_FCGI_CHILDREN" => "3",
)
)
CODE:
#### compress module
compress.cache-dir
=
"/opt/tmp/lighttpd/compress/"
compress.filetype
=
("text/plain", "text/html")
CODE:
#### expire module
expire.url = ( "/pic/" => "access 90 days", "/js/"
=> "access 31 days")
$HTTP["url"] =~ ".css$" {
expire.url = ( "" => "access 90 days" )
$HTTP["url"] =~ ".js$" {
expire.url = ( "" => "access 90 days" )
}
server.event-handler
设置时间处理方式。Default:
"poll"。Bsd上默认就可以,使用kqueue反而影响了效率。原因不明。以下是各种操作系统对应的方式:
OS Method Config-Value
all select select
Unix poll poll
- Linux 2.4+ rt-signals linux-rtsig
- Linux 2.6+ epoll linux-sysepoll
Solaris /dev/poll solaris-devpoll
FreeBSD, ... kqueue freebsd-kqueue
server.max-request-size
maximum size in kbytes of the request (header + body)
Default:
server.max-worker
lighttpd默认只启动一个进程工作,但也支持apache那样启动多个进程,我的实验显示启动多个进程同时工作时并不能提高性能。
server.max-keep-alive-requests
这一条比较关键,对性能的影响比较大。在一个keep-alive会话终止连接前能接受处理的最大请求数。Default:
128,对一个高负载的应用来说是不够的。我用了4000。
server.max-keep-alive-idle
一个空闲keep-alive连接被丢弃前,存在的的最大秒数。Default: 30。
server.error-handler-404
server.error-handler-404 = "/error-404.php"
server.max-fds
因为lighttpd是一个单线程(single-threaded)服务器,它的主要资源限制是文件描述符数目,默认值是1024。如果在一个高负载的站点上,可能你需要通过下面的设定增加这个限定值
server.max-fds = 2048 但这个限定只有在lighttpd做为root身份运行时才能生效。
connection.kbytes-per-second,server.kbytes-per-second
还有值得一提的时lighttpd可以限制每个连接或则特定虚拟机的流量。
compress.cache-dir,compress.filetype
compress.cache-dir = "/var/www/cache/"
compress.filetype = ("text/plain", "text/html")
server.stat-cache-engine = "fam"
这一条能明显提升性能,stat()
cache。一个stat调用代价可能是昂贵的。为它设一个cache能省时间又避免上下文切换。替代每次都stat()来确定文件的存在,它只stat
()一次,然后会监视目录,如果目录不变,他里面的文件也就没有变化。我们的内存文件系统中有没必要保留,依情况而定。
server.stat-cache-engine = "fam" # either fam, simple or
disabled1
lighttpd.conf中有以下几个选项可考虑调整:
server.use-ipv6 = 0 # 缺省为禁用
server.event-handler = "linux-sysepoll" #
Linux环境下epoll系统调用可提高吞吐量
server.max-worker = 10 # 如果你的系统资源没跑满,可考虑调高
server.max-fds = 4096 # 默认的,应该够用了,可根据实际情况调整
server.max-connections = 4096 # 默认等于 server.max-fds
server.network-backend = "linux-sendfile" #
可考虑writev系统调用,不过没测过