VPS搭建(七)——轻量级服务器之lighttpd配置

通过一个配置文件的例子说明lighttpd的基本配置方法。内容包括了fastcgi配置(mod_fastcgi)、虚拟主机配置(mod_simple_vhost)、权限认证配置(mod_auth),访问控制(mod_access)、反盗链等。因为比较长,需要分成多个部分。本文严重参考calomel.org的"How to"。

配置方法

/etc/lighttpd/lighttpd.conf是lighttpd的配置文件,将lighttpd的配置写在里面,执行

sudo /etc/init.d/lighttpd force-reload

即可使改动生效。

lighttpd添加模块很容易,可以在lighttpd.conf文件开头看到一个模块的加载列表,例如:

server.modules = (
                  "mod_access",
                  "mod_compress"
)

如果需要添加什么模块,只需要将其写到里面即可,例如添加mod_auth,则为:

server.modules = (
                  "mod_access",
                  "mod_compress",
                  "mod_auth"
)

除了统一写在前面外,也可以在需要的地方再添加,比如,配置文件最后添加上mod_fastcgi模块,则写成:

server.modules   += ( "mod_fastcgi" )

基本配置文件

Note:Debian/Ubuntu的lighttpd默认的配置文件内容丰富,可以直接在其上修改,不过可能结构上不太合意,我是完全删除后重新写的。
  server.modules = (
                    "mod_access",
                    "mod_compress",
                    "mod_expire",
                    "mod_evasive",
  		  "mod_auth"
  )

  ################ 基本设置  ################
  server.document-root	       = "/var/www/"
  server.port		       = 80
  server.bind		       = "服务器IP地址"
  server.upload-dirs             = ( "/var/cache/lighttpd/uploads" )
  index-file.names               = ( "index.php", "index.html", "index.htm")
  static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" )
  server.pid-file                = "/var/run/lighttpd.pid"
  server.username                = "www-data"
  server.groupname               = "www-data"
  ssl.engine		       = "disable"
  server.dir-listing	       = "disable"
  server.max-request-size	       = 1024
  server.follow-symlink          = "disable"
  $HTTP["url"] =~ "\.pdf$" {
  	server.range-requests = "disable"
  }

  ################  性能 ################
  server.max-keep-alive-requests		= 4
  server.max-keep-alive-idle		= 4
  server.max-read-idle			= 15
  server.max-write-idle			= 15
  server.event-handler			= "linux-sysepoll"
  server.network-backend			= "linux-sendfile"
  server.stat-cache-engine		= "simple"

  ################  mod_compress  ################
  compress.cache-dir	= "/var/cache/lighttpd/compress/"
  compress.filetype	= ( "text/plain", "text/html",
                              "text/css", "application/x-javascript",
                              "image/png" )

  ################  mod_expire  ################
  $HTTP["url"] =~ "\.(gif|GIF|jpg|JPG|png|PNG|jpeg|JPEG|css|CSS|js|JS)$" {
  	expire.url = ( "" => "access 6 hours" )
  }

  ################  mod_evasive  ################
  evasive.max-conns-per-ip	= 25

  ################  mod_access  ################
  url.access-deny = ( "~", ".inc")
  $HTTP["useragent"] =~ "(GouGou|sogou spider)" {
  	url.access-deny = ( "" )
  }
  $HTTP["referer"] !~ "^http://(|[^/]+|\.)(easyboxlite\.com)" {
  	url.access-deny = ( ".jpg", ".jpeg", ".png", ".gif",
  			    ".JPG", ".JPEG", ".PNG", ".GIF" )
  }
  $HTTP["host"] !~ "(^|\.)(easyboxlite\.com)$" {
  	url.access-deny = ( "" )
  }

  ################  包含文件  ################
  include "conf-enabled/vhost.conf"
  include "conf-enabled/fastcgi.conf"
  include "conf-enabled/mimetype.conf"

评论




评论支持Dokuwiki语法,参见http://www.dokuwiki.org/zh:syntax