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"
加载模块
配置文件的最前面加载了5个模块,分别是:
-
mod_access:访问控制,可以控制某个目录、文件是否能被符合特定条件(如IP)的浏览者浏览。
-
mod_compress:压缩文件,可以有效的减低页面的大小,加快网页的下载速度
-
mod_expire:设置内容过期
-
mod_evasive:限制并发连接数
-
mod_auth:权限认证
基本设置
server.document-root:网站的根目录。这里设置为默认目录/var/www
server.port:网站端口,默认为80端口,如果需要非标准端口,可以在这里设置。
server.bind:服务器监听地址,可以是IP、网址或者目录。这里应该设置为服务器的IP地址。当然,如果是在自己机子上自己用,可以绑定到127.0.0.1。
server.upload-dirs:上传目录,不用修改,用默认的
index-file.names:首页文件的名字
static-file.exclude-extensions:通过扩展禁止进入一些类型文件的源代码。
server.pid-file:lighttpd的pid文件,不用修改,用默认的
server.username:运行lighttpd的用户,Debian/Ubuntu下默认为www-data。
server.groupname:运行lighttpd的用户组,Debian/Ubuntu下默认为www-data。
ssl.engine:我不用SSL,所以关掉
server.dir-listing:目录索引,关掉。注意默认可能是打开的
server.max-request-size:通过POST方法能接受的最大文件大小(单位KB),默认为2GB。如果不需要上传,可以设置为1
server.follow-symlink:为了安全最好关掉(默认是打开的)
server.range-requests:默认是开启的(所以没有写),断点续传支持。注意开启此项必须要关闭对pdf的支持,否则可能崩溃。即
$HTTP["url"] =~ "\.pdf$" {\\ server.range-requests = "disable"\\ }
性能
server.max-keep-alive-requests:在一个常连接中最大请求数。对于忙碌的服务器应该不小于每个单独页面最大对象数。不过对于非并发情况,可以设置小些,比如为4,甚至可以设置为0。0意味着客户端对页面上每个对象都需要连接一次。
server.max-keep-alive-idle:一个常连接的最大持续时间(秒)。同样对于非并发可以设置小些。
server.max-read-idle:一个等待的,并非常连接的read调用超时并关闭连接前的最大时间(秒)。
server.max-write-idle:一个等待的,并非常连接的write调用超时并关闭连接前的最大时间(秒)。
server.event-handler:事件处理,按下表取
| 操作系统 | 方法 | 设置值 |
|---|---|---|
| all | select | select |
| Unix | poll | poll |
| Linux 2.4+ | rt-signals | linux-rtsig |
| Linux 2.6+ | epoll | linux-sysepoll |
| Solaris | /dev/poll | solaris-devpoll |
| FreeBSD, … | kqueue | freebsd-kqueue |
| NetBSD | kqueue | kqueue |
server.network-backend:网络处理,按下表取
| 操作系统 | 方法 | 设置值 |
|---|---|---|
| all | write | write |
| Unix | writev | writev |
| Linux 2.4+ | sendfile | linux-sendfile |
| Linux 2.6+ | sendfile64 | linux-sendfile |
| Solaris | sendfilev | solaris-sendfilev |
| FreeBSD | sendfile | freebsd-sendfile |
server.stat-cache-engine:对stat()调用进行缓存,可以是simple(默认)、fam和disable。除非你的文件在大量改变,否则不要disable。使用fam需要安装FAM或者gamin
mod_compress
使用gzip压缩可以节省带宽并且让页面看起来更快。
compress.cache-dir:压缩缓存目录,用默认的。
compress.filetype:要压缩的文件类型。注意,JPG原本就是压缩的,如果用gzip再次压缩反而会使之更大。
Note:要压缩PHP,还需要进一步修改。打开php.ini,激活下面两个选项:zlib.output_compression = On、zlib.output_handler = On
mod_expire
让一些内容,如图片,在客户端存留更长的时间也可以节省带宽和加快浏览。
expire.url:设置失效时间。示例中设置为6小时。
mod_evasive
evasive.max-conns-per-ip:限制单独IP能连接到服务器的并发连接数量。当到达限制数量后,客户端将会得到404错误页面。
mod_access
url.access-deny = ( ”~”, ”.inc”)
禁止访问“~”和“.inc”结尾的文件。也可以添加更多,如:url.access-deny = ( ”~”, ”.inc”,”.db”)
Note:如果使用虚拟主机,此项最好在虚拟主机配置里面再添加一次,否则可能会不工作。
$HTTP["useragent"] =~ "(GouGou|sogou spider)" {\\ url.access-deny = ( "" )\\ }
限制GouGou和sogou spider这两个搜索引擎蜘蛛访问。
$HTTP["referer"] !~ "^http://(|[^/]+|\.)(easyboxlite\.com)" {\\ url.access-deny = ( ".jpg", ".jpeg", ".png", ".gif",\\ ".JPG", ".JPEG", ".PNG", ".GIF" )\\ }
防图片盗链设置。
$HTTP["host"] !~ "(^|\.)(easyboxlite\.com)$" {\\ url.access-deny = ( "" )\\ }
使仅通过限定域名访问网站。
包含文件
有时候lighttpd.conf变得很长,可以通过include来更好地组织配置文件。即将文件的一部分写到另一个文件里,再用:include “文件路径”,来引用。例子中分别建立了三个引用文件(位于/etc/lighttpd/conf-enable目录下):
-
vhost.conf:虚拟主机配置文件。配置见lighttpd的mod_simple_vhost虚拟主机配置
-
fastcgi.conf:fastcgi配置文件。配置见lighttpd的mod_fastcgi配置
-
mimetype.conf:mimetype配置文件。这个只需要把用现成的就行了。可以直接将lighttpd的wiki上给出的配置文件示例(Configuration Sample)复制过来。
Note:Debian/Ubuntu中在lighttpd.conf后面添加有脚本,可以通过lighttpd-enable-mod来激活相应模块。脚本会自动将配置文件复制到/etc/lighttpd/conf-enable目录下(如fastcgi的为10-fastcgi.conf),并实现引用,mimetype也通过脚本定义。所以你可以在激活模块后直接编辑conf-enable目录下的文件。

浙公网安备 33010602011771号