m2014-architecture-imgserver->配置lighttpd mod_mem_cache 模块做静态资源服务器
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://freehat.blog.51cto.com/1239536/989171
一 lighttpd 简介
Lighttpd 是一个德国人领导的开源Web服务器软件,其根本的目的是提供一个专门针对高性能网站,安全、快速、兼容性好并且灵活的web server环境。具有非常低的内存开销,cpu占用率低,效能好,以及丰富的模块等特点。
二 下载lighttpd,并编译安装lighttpd
wget http://lighttpd-improved.googlecode.com/files/lighttpd-1.4.26.modcache.v.1.8.3-2.tar.gz
#tar xf lighttpd-1.4.26.modcache.v.1.8.3-2.tar.gz
#cd lighttpd-1.4.26
# ./configure --prefix=/usr/local/lighttpd --with-zlib --with-bzip2 --with-memcache
#make && make install
#cd doc
#cp rc.lighttpd.redhat /etc/init.d/lighttpd
#cp sysconfig.lighttpd /etc/sysconfig/lighttpd
#vim /etc/sysconfig/lighttpd
修改如下
LIGHTTPD_CONF_PATH=/usr/local/lighttpd/conf/lighttpd.conf
#
#chmod 700 /etc/init.d/lighttpd
#vim 修改如下选项
config: /usr/local/lighttpd/conf/lighttpd.conf
config: /etc/sysconfig/lighttpd
pidfile: /var/run/lighttpd.pid
LIGHTTPD_CONF_PATH="/usr/local/lighttpd/conf/lighttpd.conf"
lighttpd="/usr/local/lighttpd/sbin/lighttpd"
#mkdir /usr/local/lighttpd/{cache,logs,conf,htdocs}
#cp lighttpd.conf /usr/local/lighttpd/conf
三 配置详解
#cd /usr/local/lighttpd/conf
# cat lighttpd.conf
# lighttpd configuration file
#
# use it as a base for lighttpd 1.0.0 and above
#
# $Id: lighttpd.conf,v 1.7 2004/11/03 22:26:05 weigon Exp $
############ Options you really have to take care of ####################
## modules to load
# at least mod_access and mod_accesslog should be loaded
# all other module should only be loaded if really neccesary
# - saves some time
# - saves memory
#lighttpd 加载的模块
server.modules = (
"mod_rewrite",
"mod_redirect",
"mod_access",
"mod_expire",
"mod_mem_compress",
"mod_compress",
"mod_mem_cache",
"mod_cache",
"mod_dirlisting",
"mod_status",,
"mod_fastcgi",
"mod_proxy",
"mod_usertrack",
"mod_accesslog" )
## a static document-root, for virtual-hosting take look at the
## server.virtual-* options
server.document-root = "/usr/local/lighttpd/htdocs/"
## where to send error-messages to
server.errorlog = "/usr/local/lighttpd/logs/error.log"
# files to check for if .../ is requested
index-file.names = ( "index.php", "index.html",
"index.htm", "default.htm" )
## set the event-handler (read the performance section in the manual)
# server.event-handler = "freebsd-kqueue" # needed on OS X
# 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" => "image/gif",
# ".jar" => "application/x-java-archive",
".jpg" => "image/jpeg",
".jpeg" => "image/jpeg",
".png" => "image/png",
# ".xbm" => "image/x-xbitmap",
# ".xpm" => "image/x-xpixmap",
# ".xwd" => "image/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",
)
# Use the "Content-Type" extended attribute to obtain mime type if possible
#mimetype.use-xattr = "enable"
## send a different Server: header
## be nice and keep it at lighttpd
# server.tag = "lighttpd"
#### accesslog module
#accesslog.filename = "/usr/local/lighttpd/log/access.log"
#设置日志切割,当然需要安装cronlog 工具
accesslog.filename = "|/usr/local/cronolog/sbin/cronolog /usr/local/lighttpd/logs/%w/access.log"
## deny access the file-extensions
#
# ~ is for backupfiles from vi, emacs, joe, ...
# .inc is often used for code includes which should in general not be part
# of the document-root
url.access-deny = ( "~", ".inc" )
$HTTP["url"] =~ "\.pdf$" {
server.range-requests = "disable"
}
##
# which extensions should not be handle via static-file transfer
#
# .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"
## virtual directory listings 关闭目录浏览
dir-listing.activate = "disable"
#或者 server.dir-listing = "disable"
## select encoding for directory listings 设置字符编码
dir-listing.encoding = "utf-8"
#### compress module 压缩模块的设置
#压缩cache 存储路径
compress.cache-dir = "/usr/local/lighttpd/cache/"
#压缩 文件类型
compress.filetype = ("text/html;charset=utf-8","text/plain;charset=utf-8","text/css", "text/javascript", "text/xml","application/x-java-archive")
#### status module 设置查看lighttpd 状态
#设置允许查看lighttpd 状态的主机
$HTTP["remoteip"] == "192.168.1.0/24" {
status.status-url = "/server-status"
status.config-url = "/server-config"
status.statistics-url = "/cache-status"
}
#### expire module
#expire.url = ( "/buggy/" => "access 2 hours", "/asdhas/" => "access plus 1 seconds 2 minutes")
#设置文件类型,在浏览器中缓存的时间
$HTTP["url"] =~ "\.(jpg|gif|bmp|jpeg)$" {
expire.url = ( "" => "access 5 minutes")
}
$HTTP["url"] =~ "\.(html|htm|shtml)$" {
expire.url = ( "" => "access 5 minutes")
}
$HTTP["url"] =~ "\.(png)$" {
expire.url = ( "" => "access 5 minutes")
}
$HTTP["url"] =~ "\.(css|js|xml)$" {
expire.url = ( "" => "access 5 minutes")
}
$HTTP["url"] =~ "\.(ico)$" {
expire.url = ( "" => "access 262800 minutes")
}
#memcache for static web page server 设置 开启cache
cache.support-queries = "enable" #ignore '?' in url
cache.dynamic-mode = "enable"
#设置cache 存储的路径
cache.bases = ("/usr/local/lighttpd/cache") #write cached files in /usr/local/lighttpd/cache directory
#设置不同的文件类型的cache 刷新的时间
cache.refresh-pattern = (
# "\.(?i)(flv)$" => "0 fetchall-for-range-request flv-streaming", # to work with mod_flv_streaming for flv files
"\.(?i)(js|css|xml)$" => "10080", # update js/css/xml every 1 week and on refresh requests
"\.(?i)(htm|html|shtml)$" => "5", # update html/htm/shtml every 5 minutes and on refresh requests
"\.(?i)(jpg|bmp|jpeg|gif|png)$" => "10080", # update graphics files every 1 week
# "\.(?i)(png)$" => "5", #update png every 5 minutes and on refresh requests
"\.(?i)(ico)$" => "262800", #update ico every 6 months and on refresh requests
# "\.(?i)(rar|zip|wmv|avi|mp3|ape|rm|mpeg|mpg|wma|asf|rmvb|flv)$" => "0 fetchall-for-range-request", # cache media file forever
# "." => "30 update-on-refresh" # default to update every 30 minutes and on refresh requests
)
#purge cache 设置允许清除cache 的主机
cache.purge-host = "192.168.1.0/24"
#it's important to enable proxy.worked-with-mod-cache or mod_proxy will not cooperate with modcache
#enable or disable mod_mem_cache 设置 开启基于mod_mem_cache 模块
mem-cache.enable = "enable"
#content-type arrays which want to put into memory 设置允许在mem_cache 存储的类型
mem-cache.filetypes = ("application/x-javascript","image/gif","image/jpg","image/jpeg","image/png","text/css","text/html","text/javascript","text/plain")
#maxium memory in Mbytes mod_mem_cache can use 设置mem-cache 允许使用的最大内存
mem-cache.max-memory = "1024"
#maxium file size in Kbytes of single file to cache in memory 设置在内存中存储的最大文件大小
mem-cache.max-file-size = "1024"
#number of cached memory items to remove when used memory reached maxmemory by LRU algorthim #设置当使用的内存达到最大值时,通过LRU 算法删除的内存cache 的大小
mem-cache.lru-remove-count = "500"
#memory cache`s expire time in minutes.Default: 0 (means to check file update for every request) 设置memory cache 的过期时间
mem-cache.expire-time = "1440"
#adjusts slru threshold (against hit counter)
mem-cache.slru-thresold = "0"
#stat() 这样的系统调用,开销也是相当明显的。缓存能够节约时间和环境切换次数(context switches),此项有三个选项:fam、simple(缓存1秒内的stat())和disabled。一般我们较多使用前两项
server.stat-cache-engine = "simple"
#usertrack.cookie-domain
#usertrack.cookie-domain = ".example.com"
#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
#usertrack.cookie-max-age = "60"
#this option allows the setting of the name of the cookie.the default is 'TRACKID'.currently it only allow a-z
#usertrack.cookie-name = "trackid"
# set lighttpd open file num 设置允许的最大文件描述符个数。lighttpd是单线程的服务器,其最大的资源限制就是文件描述符的数量,默认为1024。对于高负荷的站点,需要增加该值,对于较为繁忙的服务器设置为2048较好。修改该设置需要root权限。
server.max-fds = "65535"
#设置lighttpd 最大连接数
#Provides fine tuning for request handling.max-connections == max-fds/2 (maybe /3).As in, if you want to support 2048 concurrent connections,
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.
server.max-connections = "32767"
#http Keep-Alive 一般来说,一个系统能够打开的文件个数是有限制的(文件描述符限制)。常连接占用文件描述符,对非并发的访问没有什么意义
默认情况下lighttpd 做多可以同时承受30 s的长连接,每个连接最多请求128 个文件
#server.max-keep-alive-requests = 128
#server.max-keep-alive-idle =30
#the maximum of keep alive requests enable or disable
server.max-keep-alive-requests = "1000" #"0" represent disable
#sets the linux system support for transaction processing,Lighttpd
#大量使用了 sendfile() 这样一个高效的系统调用。减少了从应用程序到网卡间的距离。(同时也减少了Lighttpd对cpu的占用,这部分占用转嫁到内核身上了)
server.event-handler = "linux-sysepoll"
#such a highly active system call which a lot of sendfile() used by lighttpd,reduce the distance from application to ethernet
server.network-backend = "linux-sendfile"
#禁止etags 功能
static-file.etags = "disable"
#配置lighttpd 多进程,一般和CPU 个数相同
server.max-worker=4
注:
官方lighttpd 优化方案:http://redmine.lighttpd.net/projects/lighttpd/wiki/Docs_Performance
http://www.unixvip.com/app/lighttpd/616.html
本文出自 “Frank” 博客,请务必保留此出处http://freehat.blog.51cto.com/1239536/989171