六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 971|回复: 0

配置lighttpd mod_mem_cache 模块做静态资源服务器

[复制链接]
 楼主| 发表于 2013-12-10 18:34:05 | 显示全部楼层 |阅读模式
一   lighttpd 简介
Lighttpd 是一个德国人领导的开源Web服务器软件,其根本的目的是提供一个专门针对高性能网站,安全、快速、兼容性好并且灵活的web server环境。具有非常低的内存开销,cpu占用率低,效能好,以及丰富的模块等特点。
二 下载lighttpd,并编译安装lighttpd
  1.      wget http://lighttpd-improved.googlecode.com/files/lighttpd-1.4.26.modcache.v.1.8.3-2.tar.gz

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

  3.     #cd   lighttpd-1.4.26

  4.     # ./configure --prefix=/usr/local/lighttpd --with-zlib --with-bzip2 --with-memcache

  5.     #make && make install

  6.     #cd  doc

  7.     #cp   rc.lighttpd.redhat    /etc/init.d/lighttpd

  8.     #cp sysconfig.lighttpd  /etc/sysconfig/lighttpd

  9.     #vim /etc/sysconfig/lighttpd  

  10.      修改如下

  11.       LIGHTTPD_CONF_PATH=/usr/local/lighttpd/conf/lighttpd.conf

  12.     #

  13.     #chmod 700   /etc/init.d/lighttpd

  14.     #vim 修改如下选项

  15.       config: /usr/local/lighttpd/conf/lighttpd.conf

  16.       config: /etc/sysconfig/lighttpd

  17.       pidfile: /var/run/lighttpd.pid

  18.       LIGHTTPD_CONF_PATH="/usr/local/lighttpd/conf/lighttpd.conf"

  19.       lighttpd="/usr/local/lighttpd/sbin/lighttpd"

  20.     #mkdir /usr/local/lighttpd/{cache,logs,conf,htdocs}

  21.     #cp  lighttpd.conf /usr/local/lighttpd/conf
复制代码
三 配置详解
  1.   #cd  /usr/local/lighttpd/conf

  2. # cat lighttpd.conf

  3. # lighttpd configuration file

  4. #

  5. # use it as a base for lighttpd 1.0.0 and above

  6. #

  7. # $Id: lighttpd.conf,v 1.7 2004/11/03 22:26:05 weigon Exp $

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

  9. ## modules to load

  10. # at least mod_access and mod_accesslog should be loaded

  11. # all other module should only be loaded if really neccesary

  12. # - saves some time

  13. # - saves memory

  14. #lighttpd 加载的模块

  15. server.modules              = (

  16.                                "mod_rewrite",

  17.                                "mod_redirect",

  18.                                "mod_access",

  19.                                "mod_expire",

  20.                                "mod_mem_compress",

  21.                                "mod_compress",                        

  22.                                "mod_mem_cache",

  23.                                "mod_cache",

  24.                                "mod_dirlisting",

  25.                                "mod_status",,

  26.                                "mod_fastcgi",

  27.                                "mod_proxy",                        

  28.                                "mod_usertrack",

  29.                                "mod_accesslog" )

  30. ## a static document-root, for virtual-hosting take look at the

  31. ## server.virtual-* options

  32. server.document-root        = "/usr/local/lighttpd/htdocs/"

  33. ## where to send error-messages to

  34. server.errorlog             = "/usr/local/lighttpd/logs/error.log"   
复制代码
  1.   # files to check for if .../ is requested

  2. index-file.names            = ( "index.php", "index.html",

  3.                                "index.htm", "default.htm" )

  4. ## set the event-handler (read the performance section in the manual)

  5. # server.event-handler = "freebsd-kqueue" # needed on OS X

  6. # mimetype mapping

  7. mimetype.assign             = (

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  28.     ".gif"          =>      "image/gif",

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

  30.     ".jpg"          =>      "image/jpeg",

  31.     ".jpeg"         =>      "image/jpeg",

  32.     ".png"          =>      "image/png",

  33. #  ".xbm"          =>      "image/x-xbitmap",

  34. #  ".xpm"          =>      "image/x-xpixmap",

  35. #  ".xwd"          =>      "image/x-xwindowdump",

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

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

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

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

  40. #  ".asc"          =>      "text/plain",
复制代码
  1.   #  ".c"            =>      "text/plain",

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  20. # default mime type

  21. #  ""              =>      "application/octet-stream",

  22. )

  23. # Use the "Content-Type" extended attribute to obtain mime type if possible

  24. #mimetype.use-xattr        = "enable"

  25. ## send a different Server: header

  26. ## be nice and keep it at lighttpd

  27. # server.tag                 = "lighttpd"

  28. #### accesslog module

  29. #accesslog.filename          = "/usr/local/lighttpd/log/access.log"

  30. #设置日志切割,当然需要安装cronlog 工具

  31. accesslog.filename = "|/usr/local/cronolog/sbin/cronolog    /usr/local/lighttpd/logs/%w/access.log"

  32. ## deny access the file-extensions

  33. #

  34. # ~    is for backupfiles from vi, emacs, joe, ...

  35. # .inc is often used for code includes which should in general not be part

  36. #      of the document-root

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

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

  39.    server.range-requests = "disable"

  40. }

  41. ##

  42. # which extensions should not be handle via static-file transfer

  43. #

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

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

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

  47. ## bind to port (default: 80)

  48. #server.port                = 81

  49. ## bind to localhost (default: all interfaces)

  50. #server.bind                = "127.0.0.1"

  51. ## virtual directory listings 关闭目录浏览

  52. dir-listing.activate       = "disable"

  53. #或者  server.dir-listing = "disable"

  54. ## select encoding for directory listings 设置字符编码

  55.   dir-listing.encoding        = "utf-8"
复制代码
  1. #### compress module 压缩模块的设置

  2.   #压缩cache 存储路径

  3.   compress.cache-dir         = "/usr/local/lighttpd/cache/"

  4.   #压缩 文件类型

  5.   compress.filetype          = ("text/html;charset=utf-8","text/plain;charset=utf-8","text/css", "text/javascript", "text/xml","application/x-java-archive")

  6. #### status module 设置查看lighttpd 状态

  7. #设置允许查看lighttpd 状态的主机

  8. $HTTP["remoteip"] == "192.168.1.0/24" {

  9. status.status-url = "/server-status"

  10. status.config-url = "/server-config"

  11. status.statistics-url = "/cache-status"

  12. }

  13. #### expire module

  14. #expire.url                 = ( "/buggy/" => "access 2 hours", "/asdhas/" => "access plus 1 seconds 2 minutes")

  15.   #设置文件类型,在浏览器中缓存的时间

  16. $HTTP["url"] =~ ".(jpg|gif|bmp|jpeg)$" {

  17.       expire.url = ( "" => "access 5 minutes")

  18. }

  19. $HTTP["url"] =~ ".(html|htm|shtml)$" {

  20.      expire.url = ( "" => "access 5 minutes")

  21. }

  22. $HTTP["url"] =~ ".(png)$" {

  23.      expire.url = ( "" => "access 5 minutes")

  24.   }

  25. $HTTP["url"] =~ ".(css|js|xml)$" {

  26.     expire.url = ( "" => "access 5 minutes")

  27. }

  28. $HTTP["url"] =~ ".(ico)$" {

  29.       expire.url = ( "" => "access 262800 minutes")

  30. }

  31. #memcache for static web page server  设置 开启cache

  32. cache.support-queries = "enable" #ignore '?' in url

  33. cache.dynamic-mode = "enable"

  34. #设置cache 存储的路径

  35.   cache.bases = ("/usr/local/lighttpd/cache") #write cached files in      /usr/local/lighttpd/cache directory

  36.   #设置不同的文件类型的cache 刷新的时间

  37.   cache.refresh-pattern = (

  38. #         ".(?i)(flv)$" => "0 fetchall-for-range-request flv-streaming", # to   work with mod_flv_streaming for flv files

  39.        ".(?i)(js|css|xml)$" => "10080", # update js/css/xml every 1 week and on refresh requests

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

  41.        ".(?i)(jpg|bmp|jpeg|gif|png)$" => "10080", # update graphics files every 1 week

  42. #        ".(?i)(png)$" => "5", #update png every 5 minutes and on refresh requests

  43.        ".(?i)(ico)$" => "262800", #update ico every 6 months and on refresh requests

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

  45.   #        "." => "30 update-on-refresh" # default to update every 30 minutes and on refresh requests

  46. )

  47. #purge cache  设置允许清除cache  的主机

  48. cache.purge-host = "192.168.1.0/24"

  49.   #it's important to enable proxy.worked-with-mod-cache or mod_proxy will not   cooperate with modcache

  50.   #enable or disable mod_mem_cache  设置 开启基于mod_mem_cache 模块

  51.   mem-cache.enable = "enable"

  52.   #content-type arrays which want to put into memory 设置允许在mem_cache 存储的类型

  53.   mem-cache.filetypes = ("application/x-javascript","image/gif","image/jpg","image/jpeg","image/png","text/css","text/html","text/javascript","text/plain")

  54.   #maxium memory in Mbytes mod_mem_cache can use 设置mem-cache  允许使用的最大内存

  55.   mem-cache.max-memory = "1024"

  56.   #maxium file size in Kbytes of single file to cache in memory  设置在内存中存储的最大文件大小

  57.   mem-cache.max-file-size = "1024"

  58.   #number of cached memory items to remove when used memory reached maxmemory by LRU algorthim #设置当使用的内存达到最大值时,通过LRU 算法删除的内存cache 的大小
复制代码
  1. mem-cache.lru-remove-count = "500"

  2.   #memory cache`s expire time in minutes.Default: 0 (means to check file update for every request) 设置memory cache 的过期时间

  3.   mem-cache.expire-time = "1440"

  4.   #adjusts slru threshold (against hit counter)

  5.   mem-cache.slru-thresold = "0"

  6.    #stat() 这样的系统调用,开销也是相当明显的。缓存能够节约时间和环境切换次数(context switches),此项有三个选项:fam、simple(缓存1秒内的stat())和disabled。一般我们较多使用前两项

  7.   server.stat-cache-engine = "simple"

  8.   #usertrack.cookie-domain

  9.   #usertrack.cookie-domain = ".example.com"

  10.   #sets the maximum duration in seconds of the cookie.if not specified it will not add a 'max-age' to the cookie,meaning it will not expire

  11. #usertrack.cookie-max-age = "60"

  12.   #this option allows the setting of the name of the cookie.the default is 'TRACKID'.currently it only allow a-z

  13.   #usertrack.cookie-name = "trackid"

  14.   # set lighttpd open file num 设置允许的最大文件描述符个数。lighttpd是单线程的服务器,其最大的资源限制就是文件描述符的数量,默认为1024。对于高负荷的站点,需要增加该值,对于较为繁忙的服务器设置为2048较好。修改该设置需要root权限。

  15.   server.max-fds = "65535"

  16.   #设置lighttpd 最大连接数

  17.   #Provides fine tuning for request handling.max-connections == max-fds/2 (maybe /3).As in, if you want to support 2048 concurrent connections,

  18. you may also want to set server.max-fds = 4096 (or 6144).Why more file handles than connections? The other file handles are used for fastcgi and/or files.

  19.   server.max-connections = "32767"

  20.   #http Keep-Alive 一般来说,一个系统能够打开的文件个数是有限制的(文件描述符限制)。常连接占用文件描述符,对非并发的访问没有什么意义

  21. 默认情况下lighttpd 做多可以同时承受30 s的长连接,每个连接最多请求128 个文件

  22.   #server.max-keep-alive-requests = 128

  23.   #server.max-keep-alive-idle =30

  24.   #the maximum of keep alive requests enable or disable

  25.   server.max-keep-alive-requests = "1000"  #"0" represent disable

  26.   #sets the linux system support for transaction processing,Lighttpd

  27.   #大量使用了 sendfile() 这样一个高效的系统调用。减少了从应用程序到网卡间的距离。(同时也减少了Lighttpd对cpu的占用,这部分占用转嫁到内核身上了)

  28.   server.event-handler = "linux-sysepoll"

  29.   #such a highly active system call which a lot of sendfile() used by      lighttpd,reduce the distance from application to ethernet

  30.   server.network-backend = "linux-sendfile"

  31.   #禁止etags 功能

  32.   static-file.etags = "disable"

  33.   #配置lighttpd 多进程,一般和CPU 个数相同

  34.   server.max-worker=4

复制代码
注:

      官方lighttpd 优化方案:http://redmine.lighttpd.net/proj ... ki/Docs_Performance

                         http://www.unixvip.com/app/lighttpd/616.html
本文出自 “Gone with  the wind” 博客,请务必保留此出处http://freehat.blog.51cto.com/1239536/989171

该会员没有填写今日想说内容.
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表