lighttpd使用小记

最近在测试lighttpd,正好就顺手写篇关于它的博客。说实话接触lighttpd后,发现现在真心没必要用这货了,为了点资料谷歌了很久,还不一定是对的,间接的证明了不如nginx火。具体一些使用上,个人感觉跟nginx很相似,不多说,上实例。

需求:

对我的博客,做一个反向代理加缓存。nginx里是proxy_pass配合proxy_cache;而到了lighttpd里是mod_proxy 配合mod_cache。
测试服务器是218.9.147.201。

一:安装lighttpd且enable mod_mem_cache模块

a. 安装

[root@cnc peiqiang]#svn checkout http://lighttpd-improved.googlecode.com/svn/trunk/ lighttpd-improved-read-only
[root@cnc peiqiang]# cd lighttpd-improved-read-only
[root@cnc lighttpd-improved-read-only]# ./configure --prefix=/usr/local/lighttpd --with-fam --with-pcre --with-zlib --with-bzip2 --with-openssl 
[root@cnc lighttpd-improved-read-only]# make && make install

参考资料:
http://hoball.wordpress.com/2010/05/28/enabling-mod_mem_cache-in-lighttpd-1-4-26/ (不成功,仅供参考)
https://code.google.com/p/lighttpd-improved/
http://redmine.lighttpd.net/projects/1/wiki/Docs_ModMemCache

b.配置文件如下:

[root@cnc peiqiang]# cat  /usr/local/lighttpd/etc/lighttpd.conf

#Load the modules
server.modules = (
    #mod_mem_cache
#        "mod_mem_cache",
    #mod_cache
        "mod_cache",
    #基础模块
        "mod_access",
    #访问纪录
        "mod_accesslog",
    #fastcgi支持
    #    "mod_fastcgi",
    #cgi 支持
    #    "mod_cgi",
    #路径绑定
        "mod_alias",
    #代理 (转发页面)
        "mod_proxy",
    #虚拟主机
        "mod_evhost",
    #输出压缩
    #    "mod_compress",
    #mod_rewrite
        "mod_rewrite",
    #用户认证
    #    "mod_auth",
    #    "mod_redirect",
    #    "mod_cml",
    #    "mod_trigger_b4_dl",
        "mod_status",
    #    "mod_setenv",
    #    "mod_simple_vhost",
    #    "mod_userdir",
    #    "mod_ssi",
    #    "mod_usertrack",
    #    "mod_expire",
    #    "mod_secdownload",
    #    "mod_rrdtool",
)

#Basic Configuration
#默认主机根目录
server.document-root = "/usr/home/peiqiang/web"
#将/var/www/icons/ 映射到 http://host:8000/icons/
alias.url = ("/icons/" => "/var/www/icons/")
#绑定到地址 218.9.147.201
server.bind = "218.9.147.201"
#绑定到端口 8000
server.port = 80
#Lighttpd 进程的归属用户/群组
server.username = "peiqiang"
server.groupname = "peiqiang"
#访问日志路径
accesslog.filename = "/var/log/lighttpd/access.log"
#错误日志路径
server.errorlog = "/var/log/lighttpd/error.log"
#禁止访问以下文件
url.access-deny = ( "~", ".inc" )
#如果网站目录中出现以下文件名,不用指定文件名便可直接访问
index-file.names = ("index.php", "index.html","index.htm", "default.htm","index.lighttpd.html")
#与目录列表相关的设置
dir-listing.activate = "enable"
dir-listing.encoding = "utf8"
dir-listing.show-readme = "enable"

#lighttpd can utilize FAM/Gamin to cache stat call
server.stat-cache-engine = "fam"
#linux-sendfile
server.network-backend = "linux-sendfile"
#时间模型使用epoll
server.event-handler = "linux-sysepoll"
#长连接相关
server.max-keep-alive-requests = 128
server.max-keep-alive-idle = 3000
#mimetype mapping
mimetype.assign = (
  ".html" => "text/html",
  ".txt" => "text/plain",
  ".jpg" => "image/jpeg",
  ".png" => "image/png"
)

#查看lighttpd状态信息
status.status-url = "/server-status"
status.config-url = "/server-config"
status.statistics-url = "/cache-status"

#虚拟主机绑定的网络接口
$HTTP["host"] == "peiqiang.net" {
    server.name = "peiqiang.net"

    cache.enable = "enable"
    cache.bases=("/data0/lighttpd/cache")
    cache.support-queries = "enable"
    cache.debug = "enable"
    cache.refresh-pattern = (
        "\.(?i)(flv)$" => "0 fetchall-for-range-request flv-streaming",
        "\.(?i)(js|css|xml)$" => "240 ignore-cache-control-header ignore-vary ignore-set-cookie override-expire",
        "\.(?i)(htm|html|shtml)$" => "30 ignore-cache-control-header ignore-vary ignore-set-cookie override-expire fetchall-for-range-request",
        "\.(?i)(jpg|bmp|jpeg|gif|png)$" => "2880 ignore-cache-control-header ignore-vary ignore-set-cookie override-expire",
        "\.(?i)(rar|zip|wmv|avi|mp3|ape|rm|mpeg|mpg|wma|asf|rmvb|flv)$" => "0 fetchall-for-range-request",
        ".(?i)php$" => "5" # update php request every 5 minutes
    )

    proxy.worked-with-mod-cache = "enable"
    proxy.server = ("" =>
        ((
              "host" => "204.232.175.78",
              "port" => 80
        ))
    )

}

b.检查语法

[root@cnc peiqiang]# /usr/local/lighttpd/sbin/lighttpd -t -f /usr/local/lighttpd/etc/lighttpd.conf
Syntax OK

c.启动lighttpd:

[root@cnc peiqiang]# /usr/local/lighttpd/sbin/lighttpd -D -f /usr/local/lighttpd/etc/lighttpd.conf

二:测试

a.客户端加如下hosts进行测试

218.9.147.201 peiqiang.net

b.http://peiqiang.net/pictures/gitlab/gitlab_1.png 放到浏览器里刷新两次

c.打开谷歌开发者工具可以看到图片已经被缓存了

3.查看磁盘上的缓存文件如下:

[root@cnc ~]# stat /data0/lighttpd/cache/peiqiang.net/pictures/gitlab/gitlab_1.png
  File: `/data0/lighttpd/cache/peiqiang.net/pictures/gitlab/gitlab_1.png'
  Size: 481454          Blocks: 952        IO Block: 4096   regular file
Device: 807h/2055d      Inode: 37814279    Links: 1
Access: (0644/-rw-r--r--)  Uid: (  601/peiqiang)   Gid: (  601/peiqiang)
Access: 2014-10-12 22:41:34.000000000 +0800
Modify: 2014-09-05 15:40:56.000000000 +0800
Change: 2014-10-12 22:40:41.000000000 +0800

自言自语:打好基础,学些底层的东西如c、tcp/ip是关键,沉迷于这些应用层的东西,如何进步?

参考资料:

台湾一哥的博客: http://wiki.weithenn.org/cgi-bin/wiki.pl?LigHttpd-輕量級_Web_Server

linuxtoy.org站点: http://i.linuxtoy.org/docs/guide/ch23s03.html

freebsdchina.org配置支持php: https://wiki.freebsdchina.org/howto/n/php_fastcgi_lighttpd

官方配置模板: http://redmine.lighttpd.net/projects/lighttpd/repository/entry/branches/lighttpd-1.4.x/doc/config/lighttpd.conf

mod_proxy: http://redmine.lighttpd.net/projects/lighttpd/wiki/Docs_ModProxy

mod_cache: http://redmine.lighttpd.net/projects/lighttpd/wiki/Docs_ModCache

lighttpd-improved: https://code.google.com/p/lighttpd-improved/