Jquery中文网 www.jquerycn.cn
Jquery中文网 >  服务器技术  >  Lighttpd  >  正文 Lighttpd 配置与性能优化

Lighttpd 配置与性能优化

发布时间:2014-07-20   编辑:www.jquerycn.cn
Lighttpd 配置与性能优化

lighttpd使用了Linux 2.6+使用epoll,而在FreeBSD上使用了kqueue。
默认的都是poll,而我们的系统是freebsd,在bsd上使用kqueue特性时,测试结果显示,效率明显变低。

再一个就是sendfile()技术,他能最小化应用程序所做的工作,直接把一个文件送到网卡。
在Linux 2.6+上使用sendfile64,可见现在好多小的httpd都对Linux 2.6+支持的很好,比如tux。

在针对我们的应用情况下,对配置文件进行一些修改。
Time per request: 能达到0.65 [ms]左右相对于以前测的0.734[ms]左右,性能有很大提高。
如果有大量的时间对每个性能

指标进行测试,去掉不必要的模块和特性,应该在性能上还会有所提高,估计最好能到0.61[ms]左右。
最近测试如下:
lighttpd 配置文件:去掉了很多不必要的特性。
#######lighttpd.conf.
server.port = 7983
server.modules = ("mod_access") }
server.document-root = "/usr/home/yangjian/lighttpd/doc/"
server.pid-file = "/usr/home/yangjian/lighttpd/lighttpd.pid"
mimetype.assign = (
".html" => "text/html",
".js" => "text/javascript",
)
server.max-keep-alive-requests =4000
dir-listing.activate = "disable"

测试强度: ./ab -n 100000 -c 1000 http://192.168.241.109:7983/index.html
Bvh og
Server Software: lighttpd/1.4.7
Server Hostname: 192.168.241.109
Server Port: 7983

Document Path: /index.html
Document Length: 449 bytes

Concurrency Level: 1000
Time taken for tests: 65.180092 seconds
Complete requests: 100000
Failed requests: 0
Write errors: 0
Total transferred: 68281158 bytes
HTML transferred: 44953431 bytes
Requests per second: 1534.21 [#/sec] (mean)
Time per request: 651.801 [ms] (mean)
Time per request: 0.652 [ms] (mean, across all concurrent requests)
Transfer rate: 1023.01 [Kbytes/sec] received

LiteSpeed 的测试都是在默认配置下。它的结果不太稳定,最好的记录是0.603ms,这是lighttpd所没有过的好成绩,但大多数情况下都在0.70左右,有好多次在0.76以上。这里说的都是LiteSpeed的标准版,pro版是要花钱的性能当然要好些。LiteSpeed是同过web 管理的,这是它的一个比较好的特性。如果经过优化我想它和lighttpd应该还是不差上下的。

暂时先选择相对稳定点的lighttpd作为web server,然后对它的配置和优化进行说明。

影响性能的指标

上面的配置文件非常简短,去掉了所有不必要的特性。添加的模块只有mod_access,如果没有它访问是就会出现403错误。除了它之外系统默认启用的模块有
mod_indexfiles ,mod_dirlisting, mod_staticfile。但在默认已经编译进去的模块清单如下:
启用这些已编译进去的模块很简单,enable一下就ok了。
Lighttpd想添加一个功能一般情况下只需要添加一两行指令到配置文件中,比apache易配置多了。
Plugins:
enabled:
mod_access
mod_accesslog
mod_alias
mod_auth
mod_cgi
mod_compress
mod_dirlisting
mod_evhost
mod_expire
mod_fastcgi
mod_indexfiles
mod_proxy
mod_rrdtool
mod_scgi
mod_secdownload
mod_setenv
mod_simple_vhost
mod_staticfile
mod_status
mod_userdir
mod_usertrack
mod_webdav
disabled:
mod_cml
mod_mysql_vhost
mod_redirect
mod_rewrite
mod_ssi
mod_trigger_b4_dl

Features:
enabled:
auth-crypt
compress-bzip2
compress-deflate
compress-gzip
large-files
network-ipv6
disabled:
auth-ldap
network-openssl
regex-conditionals
stat-cache-fam
storage-gdbm
storage-memcache
webdav-properties
每个模块的作用和我上次写的apache的性能优化中提到的大同小异。
所以我想如果只保留核心功能,性能应该会提升一些的。配置选项:
server.chroot
使用chroot提高安全性时,web服务器的root目录。建议使用。

server.username = "..." server.groupname = "..."
放弃root权限。

dir-listing.activate
如果没有找到index文件就列出目录。建议disable。

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 disabled

在FreeBSD上可能提升一些性能同过启用accept-filters仅仅编译你的内核用下面选项:options ACCEPT_FILTER_HTTP。

您可能感兴趣的文章:
Lighttpd 配置与性能优化
lighttpd安装与配置图片服务器
lighttpd与web.py配置web service
Lighttpd搭建Flv可拖拽媒体服务器的教程
lighttpd中配置FastCGI环境
lighttpd配置url重写一例
Ubuntu中安装和配置lighttpd
lighttpd配置https
php的集成开发环境哪个好
lighttpd配置压缩文件(mod_compress)

[关闭]