1,首先去下载带缓存模块的lighttpd源代码包

lighttpd-1.4.26.modcache.v.1.8.3-2.tar.gz

wget -S http://lighttpd-improved.googlecode.com/files/lighttpd-1.4.26.modcache.v.1.8.3-2.tar.gz

2,安装包

tar zxvf lighttpd-1.4.26.modcache.v.1.8.3-2.tar.gz

cd lighttpd-1.4.26

./configure --prefix=/usr/local/lighttpd

make

make install

3,拷贝配置文件到指定的目录中

cp /usr/local/src/lighttpd-1.4.26/doc/lighttpd.conf /usr/local/lighttpd/lighttpd.conf

4,编辑配置文件

我的配置文件如下:

# lighttpd configuration file

############ Options you really have to take care of ####################

server.modules              = (

                                "mod_rewrite",

                                "mod_redirect",

                                "mod_access",

                                "mod_fastcgi",

                                "mod_cache",  //这个要开启

                                "mod_proxy",  //这个也必须开启,并且要注意顺序

                                "mod_cgi",

                                "mod_compress",//支持压缩

                                "mod_expire",

                                "mod_accesslog" )

server.document-root        = "/var/www/html/"   /配置文件目录

server.errorlog             = "/var/log/lighttpd/error.log" //

index-file.names            = ( "index.php", "index.html",    "index.htm", "default.htm" )

# mimetype mapping

mimetype.assign             = (

  ".pdf"          =>      "application/pdf",

  ".sig"          =>      "application/pgp-signature",

  ".spl"          =>      "application/futuresplash",

  ".class"        =>      "application/octet-stream",

  ".ps"           =>      "application/postscript",

  ".torrent"      =>      "application/x-bittorrent",

  ".dvi"          =>      "application/x-dvi",

  ".gz"           =>      "application/x-gzip",

  ".pac"          =>      "application/x-ns-proxy-autoconfig",

  ".swf"          =>      "application/x-shockwave-flash",

  ".tar.gz"       =>      "application/x-tgz",

  ".tgz"          =>      "application/x-tgz",

  ".tar"          =>      "application/x-tar",

  ".zip"          =>      "application/zip",

  ".mp3"          =>      "audio/mpeg",

  ".m3u"          =>      "audio/x-mpegurl",

  ".wma"          =>      "audio/x-ms-wma",

  ".wax"          =>      "audio/x-ms-wax",

  ".ogg"          =>      "application/ogg",

  ".wav"          =>      "audio/x-wav",

  ".gif"          =>      "p_w_picpath/gif",

  ".jar"          =>      "application/x-java-archive",

  ".jpg"          =>      "p_w_picpath/jpeg",

  ".jpeg"         =>      "p_w_picpath/jpeg",

  ".png"          =>      "p_w_picpath/png",

  ".xbm"          =>      "p_w_picpath/x-xbitmap",

  ".xpm"          =>      "p_w_picpath/x-xpixmap",

  ".xwd"          =>      "p_w_picpath/x-xwindowdump",

  ".css"          =>      "text/css",

  ".html"         =>      "text/html",

  ".htm"          =>      "text/html",

  ".js"           =>      "text/javascript",

  ".asc"          =>      "text/plain",

  ".c"            =>      "text/plain",

  ".cpp"          =>      "text/plain",

  ".log"          =>      "text/plain",

  ".conf"         =>      "text/plain",

  ".text"         =>      "text/plain",

  ".txt"          =>      "text/plain",

  ".dtd"          =>      "text/xml",

  ".xml"          =>      "text/xml",

  ".mpeg"         =>      "video/mpeg",

  ".mpg"          =>      "video/mpeg",

  ".mov"          =>      "video/quicktime",

  ".qt"           =>      "video/quicktime",

  ".avi"          =>      "video/x-msvideo",

  ".asf"          =>      "video/x-ms-asf",

  ".asx"          =>      "video/x-ms-asf",

  ".wmv"          =>      "video/x-ms-wmv",

  ".bz2"          =>      "application/x-bzip",

  ".tbz"          =>      "application/x-bzip-compressed-tar",

  ".tar.bz2"      =>      "application/x-bzip-compressed-tar",

  # default mime type

  ""              =>      "application/octet-stream",

 )

accesslog.filename          = "/var/log/lighttpd/access.log"

url.access-deny             = ( "~", ".inc" )

$HTTP["url"] =~ "\.pdf$" {

  server.range-requests = "disable"

}

# .php, .pl, .fcgi are most often handled by mod_fastcgi or mod_cgi

static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" )

######### Options that are good to be but not neccesary to be changed #######

## bind to port (default: 80)

#server.port                = 81

## bind to localhost (default: all interfaces)

#server.bind                = "127.0.0.1"

//上面这两项可以自己绑定自己需要的ip和端口

## error-handler for status 404

#server.error-handler-404   = "/error-handler.html"

#server.error-handler-404   = "/error-handler.php"

//定义错误的页面

## to help the rc.scripts

#server.pid-file            = "/var/run/lighttpd.pid"

#### compress module

compress.allowed-encodings = ("bzip2", "gzip", "deflate")

compress.cache-dir         = "/var/cache/lighttpd/compress/"

compress.filetype          = ("text/plain", "text/html")

  $HTTP["host"] == "www.xxjob.cn" {

      compress.cache-dir = "/var/www/cache/www.xxjob.cn/" 

      compress.filetype = ("text/plain", "text/html", "text/javascript", "text/css", "text/xml")

    }

上面的这些是支持压缩

下面是cache的一些设置

cache.bases = ("/cache1/cache", "/cache2/cache")  //将缓存的内容存放在哪里

cache.support-queries = "enable"   

cache.dynamic-mode = "enable" 

//带?的url,默认是不缓存的,当cache.support-queries = "enable"

cache.dynamic-mode = "disable"时,缓存模块会把"/uri?q1" 或者"/uri?q2"看成是相同的url而缓存起来。

当两者都为enable时则会把他们看成是不同的url而缓存起来。

cache.purge-host="^127\.0\.0\.1"   //那些机器可以进行推送

下面是一些缓存规则

cache.refresh-pattern = (

        "\.(?i)(flv)$" => "0 fetchall-for-range-request flv-streaming",

        "\.(?i)(js|css|xml)$" => "240", # update js/css/xml every 4 hours

        "\.(?i)(htm|html|shtml)$" => "30 update-on-refresh", #update html/htm/shtml every 30 minutes and on refresh requests

        "\.(?i)(jpg|bmp|jpeg|gif|png)$" => "2880",

        "\.(?i)(rar|zip|wmv|avi|mp3|ape|rm|mpeg|mpg|wma|asf|rmvb|flv)$" => "0 fetchall-for-range-request",

        "." => "30 update-on-refresh"

)

下面设置对那些域名进行缓存

$HTTP["host"] == "www.test.com" {

proxy.server  = ( "/" =>

        (

                ( "host" => "118.194.22.36", "port" => 80 )//host指的是能够提供服务的web服务器的地址

        )

)

}

当然你也可以设置多个

$HTTP["host"] == "www.test1.com" {

proxy.server  = ( "/" =>

        (

                ( "host" => "118.194.22.37", "port" => 80 )

        )

)

}

5,启动lighttpd

/usr/local/lighttpd/sbin/lighttpd -f /usr/local/lighttpd/lighttpd.conf

6,检查是否起来

 netstat -tunlp | grep lighttpd

tcp        0      0 x.x.x.x:80           0.0.0.0:*                   LISTEN      11034/lighttpd  

7,第一次访问的时候是不会被缓存的,第二次访问的时候如果出现下面的信息,证明就缓存住了。

lighttpd缓存的规则的一些简单介绍
ignore-reload
当浏览器中定义有'Cache-Control: no-cache' 的时候,直接忽略此选项,而不去更新缓存中的内容
update-on-refresh
和上面的意思刚好相反,当浏览器中定义有'Cache-Control: no-cache' 的时候,更新缓存中的内容
override-expire
在检查Expires头部之前,先检查min值
ignore-cache-control-heade
忽略请求里面带的Cache-Control信息
ignore-vary
忽略请求里面带的'Vary' 信息
ignore-set-cookie
忽略请求里带的 'Set-Cookie'信息
fetchall-for-range-request
如果浏览器请求中包含Accept-Ranges bytes那么就下载所有关联的页面,支持多线程下载
参考文章: