Elton's Blog

Tag: lighttpd

使用ETag和Expires调优web服务器性能

by on 八.04, 2009, under Linux

正确使用Etag和Expires标识处理,可以使得页面更加有效被Cache。

在客户端通过浏览器发出第一次请求某一个URL时,根据 HTTP 协议的规定,浏览器会向服务器传送报头(Http Request Header),服务器端响应同时记录相关属性标记(Http Reponse Header),服务器端的返回状态会是200,格式类似如下:

1
2
3
4
5
6
7
8
9
HTTP/1.1 200 OK
Date: Tue, 03 Mar 2009 04:58:40 GMT
Content-Type: image/jpeg
Content-Length: 83185
Last-Modified: Tue, 24 Feb 2009 08:01:04 GMT
Cache-Control: max-age=2592000
 
Expires: Thu, 02 Apr 2009 05:14:08 GMT
Etag: “5d8c72a5edda8d6a:3239″

客户端第二次请求此URL时,根据 HTTP 协议的规定,浏览器会向服务器传送报头(Http Request Header),服务器端响应并记录相关记录属性标记文件没有发生改动,服务器端返回304,直接从缓存中读取:

1
2
3
4
5
6
7
8
HTTP/1.x 304 Not Modified
Date: Tue, 03 Mar 2009 05:03:56 GMT
Content-Type: image/jpeg
Content-Length: 83185
Last-Modified: Tue, 24 Feb 2009 08:01:04 GMT
Cache-Control: max-age=2592000
Expires: Thu, 02 Apr 2009 05:14:08 GMT
Etag: “5d8c72a5edda8d6a:3239″

其中Last-Modified、Expires和Etag是标记页面缓存标识

一、Last-Modified、Expires和Etag相关工作原理

1、Last-Modified
在浏览器第一次请求某一个URL时,服务器端的返回状态会是200,内容是你请求的资源,同时有一个Last-Modified的属性标记(Http Reponse Header)此文件在服务期端最后被修改的时间,格式类似这样:
Last-Modified: Tue, 24 Feb 2009 08:01:04 GMT
客户端第二次请求此URL时,根据 HTTP 协议的规定,浏览器会向服务器传送 If-Modified-Since 报头(Http Request Header),询问该时间之后文件是否有被修改过:
If-Modified-Since: Tue, 24 Feb 2009 08:01:04 GMT
如果服务器端的资源没有变化,则自动返回 HTTP 304 (NotChanged.)状态码,内容为空,这样就节省了传输数据量。当服务器端代码发生改变或者重启服务器时,则重新发出资源,返回和第一次请求时类似。从而保证不向客户端重复发出资源,也保证当服务器有变化时,客户端能够得到最新的资源。
注:如果If-Modified-Since的时间比服务器当前时间(当前的请求时间request_time)还晚,会认为是个非法请求

2、Etag工作原理
HTTP 协议规格说明定义ETag为“被请求变量的实体标记” (参见14.19)。简单点即服务器响应时给请求URL标记,并在HTTP响应头中将其传送到客户端,类似服务器端返回的格式:
Etag: “5d8c72a5edda8d6a:3239″
客户端的查询更新格式是这样的:
If-None-Match: “5d8c72a5edda8d6a:3239″
如果ETag没改变,则返回状态304。
即:在客户端发出请求后,Http Reponse Header中包含 Etag: “5d8c72a5edda8d6a:3239″
标识,等于告诉Client端,你拿到的这个的资源有表示ID:5d8c72a5edda8d6a:3239。当下次需要发Request索要同一个 URI的时候,浏览器同时发出一个If-None-Match报头( Http RequestHeader)此时包头中信息包含上次访问得到的Etag: “5d8c72a5edda8d6a:3239″标识。
If-None-Match: “5d8c72a5edda8d6a:3239“
,这样,Client端等于Cache了两份,服务器端就会比对2者的etag。如果If-None-Match为False,不返回200,返回304 (Not Modified) Response。

3、Expires
给出的日期/时间后,被响应认为是过时。如Expires: Thu, 02 Apr 2009 05:14:08 GMT
需和Last-Modified结合使用。用于控制请求文件的有效时间,当请求数据在有效期内时客户端浏览器从缓存请求数据而不是服务器端. 当缓存中数据失效或过期,才决定从服务器更新数据。

4、Last-Modified和Expires
Last-Modified标识能够节省一点带宽,但是还是逃不掉发一个HTTP请求出去,而且要和Expires一起用。而Expires标识却使得浏览器干脆连HTTP请求都不用发,比如当用户F5或者点击Refresh按钮的时候就算对于有Expires的URI,一样也会发一个HTTP请求出去,所以,Last-Modified还是要用的,而 且要和Expires一起用。

5、Etag和Expires
如果服务器端同时设置了Etag和Expires时,Etag原理同样,即与Last-Modified/Etag对应的HttpRequest Header:If-Modified-Since和If-None-Match。我们可以看到这两个Header的值和WebServer发出的 Last-Modified,Etag值完全一样;在完全匹配If-Modified-Since和If-None-Match即检查完修改时间和 Etag之后,服务器才能返回304.

6、Last-Modified和Etag
Last-Modified 和ETags请求的http报头一起使用,服务器首先产生 Last-Modified/Etag标记,服务器可在稍后使用它来判断页面是否已经被修改,来决定文件是否继续缓存
过程如下:
1. 客户端请求一个页面(A)。
2. 服务器返回页面A,并在给A加上一个Last-Modified/ETag。
3. 客户端展现该页面,并将页面连同Last-Modified/ETag一起缓存。
4. 客户再次请求页面A,并将上次请求时服务器返回的Last-Modified/ETag一起传递给服务器。
5. 服务器检查该Last-Modified或ETag,并判断出该页面自上次客户端请求之后还未被修改,直接返回响应304和一个空的响应体。
注:
1、Last-Modified和Etag头都是由Web Server发出的Http Reponse Header,Web Server应该同时支持这两种头。
2、Web Server发送完Last-Modified/Etag头给客户端后,客户端会缓存这些头;
3、客户端再次发起相同页面的请求时,将分别发送与Last-Modified/Etag对应的Http RequestHeader:If-Modified-Since和If-None-Match。我们可以看到这两个Header的值和 WebServer发出的Last-Modified,Etag值完全一样;
4、通过上述值到服务器端检查,判断文件是否继续缓存;

二、Apache、Lighttpd和Nginx中针配置Etag和Expires,有效缓存纯静态如css/js/pic/页面/流媒体等文件。

A、Expires
A.1、Apache Etag
使用Apache的mod_expires 模块来设置,这包括控制应答时的Expires头内容和Cache-Control头的max-age指令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
ExpiresActive On
ExpiresByType image/gif “access plus 1 month”
ExpiresByType image/jpg “access plus 1 month”
ExpiresByType image/jpeg “access plus 1 month”
ExpiresByType image/x-icon “access plus 1 month”
ExpiresByType image/bmp “access plus 1 month”
ExpiresByType image/png “access plus 1 month”
ExpiresByType text/html “access plus 30 minutes”
ExpiresByType text/css  “access plus 30 minutes”
ExpiresByType text/txt  “access plus 30 minutes”
ExpiresByType text/js   ”access plus 30 minutes”
ExpiresByType application/x-javascript   ”access plus 30 minutes”
ExpiresByType application/x-shockwave-flash     ”access plus 30 minutes”
或
<ifmodule mod_expires.c>
<filesmatch “.(jpg|gif|png|css|js)$”>
ExpiresActive on
ExpiresDefault “access plus 1 year”
</filesmatch>
</ifmodule>

当设置了expires后,会自动输出Cache-Control 的max-age 信息
具体关于 Expires 详细内容可以查看Apache官方文档。
在这个时间段里,该文件的请求都将直接通过缓存服务器获取,
当然如果需要忽略浏览器的刷新请求(F5),缓存服务器squid还需要使用 refresh_pattern 选项来忽略该请求

1
2
3
4
5
6
7
8
9
10
11
12
refresh_pattern -i .gif$ 1440 100% 28800 ignore-reload
refresh_pattern -i .jpg$ 1440 100% 28800 ignore-reload
refresh_pattern -i .jpeg$ 1440 100% 28800 ignore-reload
refresh_pattern -i .png$ 1440 100% 28800 ignore-reload
refresh_pattern -i .bmp$ 1440 100% 28800 ignore-reload
refresh_pattern -i .htm$ 60 100% 100 ignore-reload
refresh_pattern -i .html$ 1440 50% 28800 ignore-reload
refresh_pattern -i .xml$ 1440 50% 28800 ignore-reload
refresh_pattern -i .txt$ 1440 50% 28800 ignore-reload
refresh_pattern -i .css$ 1440 50% 28800 reload-into-ims
refresh_pattern -i .js$ 60 50% 100 reload-into-ims
refresh_pattern . 10 50% 60

有关Squid中Expires的说明,请参考Squid官方中refresh_pattern介绍。

A.2、Lighttpd Expires
和Apache一样Lighttpd设置expire也要先查看是否支持了mod_expire模块,
下面的设置是让URI中所有images目录下的文件1小时后过期;

1
expire.url = ( “/images/” => “access 1 hours” )

下面是让作用于images目录及其子目录的文件;

1
2
3
$HTTP["url"] =~ “^/images/” {
expire.url = ( “” => “access 1 hours” )
}

也可以指定文件的类型;

1
2
3
$HTTP["url"] =~ “.(jpg|gif|png|css|js)$” {
expire.url = ( “” => “access 1 hours” )
}

具体参考Lighttpd官方Expires解释

A.3、Nginx中Expires

1
2
3
4
5
6
7
8
location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*.(js|css)?$
{
expires 1h;
}

这类文件并不常修改,通过 expires 指令来控制其在浏览器的缓存,以减少不必要的请求。 expires 指令可以控制 HTTP 应答中的“ Expires ”和“ Cache-Control ”的头标(起到控制页面缓存的作用)。其他请参考Nginx中Expires

B.1、Apache中Etag设置
在Apache中设置Etag的支持比较简单,只用在含有静态文件的目录中建立一个文件.htaccess, 里面加入:

1
FileETag MTime Size

这样就行了,详细的可以参考Apache的FileEtag文档页

B.2、Lighttpd Etag
在Lighttpd中设置Etag支持:
etag.use-inode: 是否使用inode作为Etag
etag.use-mtime: 是否使用文件修改时间作为Etag
etag.use-size: 是否使用文件大小作为Etag
static-file.etags: 是否启用Etag的功能
第四个参数肯定是要enable的, 前面三个就看实际的需要来选吧,推荐使用修改时间

B.3、 Nginx Etag
Nginx中默认没有添加对Etag标识.Igor Sysoev的观点”在对静态文件处理上看不出如何Etag好于Last-Modified标识。”
Note:
Yes, it’s addition,and it’s easy to add, however, I do not see howETag is better than Last-Modified for static files. -Igor Sysoev
A nice short description is here:

http://www.mnot.net/cache_docs/#WORK

It looks to me that it makes some caches out there to cache theresponse from the origin server more reliable as in rfc2616(ftp://ftp.rfc-editor.org/in-notes/rfc2616.txt) is written.
3.11 Entity Tags 13.3.2 Entity Tag Cache Validators 14.19 ETag
当然也有第三方nginx-static-etags 模块了,请参考

http://mikewest.org/2008/11/generating-etags-for-static-content-using-nginx

三、对于非实时交互动态页面中Expires和Etag处理

对数据更新并不频繁、如tag分类归档等等,可以考虑对其cache。简单点就是在非实时交互的动态程序中输出expires和etag标识,让其缓存。但需要注意关闭session,防止http response时http header包含session id标识;
3.1、Expires
如expires.php

1
2
3
4
5
<?php
header(’Cache-Control: max-age=86400,must-revalidate’);
header(’Last-Modified:.gmdate(’D, d M Y H:i:s’) . ‘ GMT’ );
header(”Expires:.gmdate (’D, d M Y H:i:s’, time() +86400). ‘ GMT’);
?>

以上信息表示该文件自请求后24小时后过期。
其他需要处理的动态页面直接调用即可。
3.2、Etag
根据Http返回状态来处理。当返回304直接从缓存中读取
如etag.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
cache();
echo date(”Y-m-d H:i:s”);
function cache()
{
$etag = “http://longrujun.name”;
if ($_SERVER['HTTP_IF_NONE_MATCH'] == $etag)
{
header(’Etag:.$etag,true,304);
exit;
}
else header(’Etag:.$etag);
}
?>

来自: http://longrujun.name/index.php/2009/03/04/etag%E5%92%8Cexpires/

4 Comments :, , , , , , more...

lighttpd配置之用户验证(mod_auth)

by on 七.13, 2009, under Linux

有时候需要对某些web资源做权限验证。 在lighttpd中可以这样做。
1. 激活mod_auth模块,在/etc/lighttpd/lighttpd.conf中去掉mod_auth模块前面的#
2. 下载apache相关模块

1
# emerge -av app-admin/apache-tools

3. 生成密码文件

1
2
# cd /var/lib/lighttpd/
# htdigest -c lighttpd.user.htdigest 'some text' username

其中some text是在密码提示框的标题中要显示的内容,lighttpd.user.htdigest是密码文件的文件名
执行过htdigest后,输入两边这个用户的密码后,就可以生成lighttpd.user.htdigest这个密码文件了
4. 配置lighttpd.conf文件
添加

1
2
3
auth.debug                 = 0 
auth.backend               = "htdigest"
auth.backend.htdigest.userfile = var.statedir + "/lighttpd.user.htdigest"

auth.debug 是调试级别,0表示不调试,1表示记录’auth-ok’验证成功的信息, 2表示所有详细的调试信息
在需要验证的虚机上添加以下内容

1
2
3
4
5
6
7
auth.require = ( "" =>
                         (
                                "method"  => "digest",
                                "realm"   => "some text",
                                "require" => "user=username"
                         )
                        )

auth.require后的空引号表示虚机中所有路径都需要验证,如果只是部分需要验证,那么就指定具体路径
5. 重启lighttpd,使配置生效

Leave a Comment :, more...

lighttpd配置之流量监控(mod_rrdtool)

by on 七.13, 2009, under Linux

lighttpd中带了一个监控模块,可以监控lighttpd服务器的流量。
1. 确保安装lighttpd的时候,带了rrdtool模块,如果没有,将rrdtool加到lighttpd的USE中,重新编译
2. 在/etc/lighttpd/lighttpd.conf中,激活mod_rrdtool模块,并添加以下代码:

1
2
 rrdtool.binary  = "/usr/bin/rrdtool"
 rrdtool.db-name = var.statedir + "/lighttpd.rrd"

rrdtool.binary是rrdtool的安装目录
rrdtool.db-name是rrdtool需要用到的数据库,在第一次启动前,确保没有那个rrd文件。
3. 重启lighttpd,这样lighttpd的流量数据就可以实时的记录到rrd文件中了。
4. 图形化报表
4a. 定义脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
    #!/bin/sh
 
RRDTOOL=/usr/bin/rrdtool
OUTDIR=/data/www/yourdomain.com/rrd/ #你的图片输出目录
INFILE=/var/lib/lighttpd/lighttpd.rrd    #你的rrd数据库文件位置
OUTPRE=lighttpd-traffic                        #图片文件的前缀
WIDTH=600
HEIGHT=350
 
DISP="-v bytes --title TrafficWebserver 
        DEF:binraw=$INFILE:InOctets:AVERAGE 
        DEF:binmaxraw=$INFILE:InOctets:MAX 
        DEF:binminraw=$INFILE:InOctets:MIN 
        DEF:bout=$INFILE:OutOctets:AVERAGE 
        DEF:boutmax=$INFILE:OutOctets:MAX 
        DEF:boutmin=$INFILE:OutOctets:MIN 
        CDEF:bin=binraw,-1,* 
        CDEF:binmax=binmaxraw,-1,* 
        CDEF:binmin=binminraw,-1,* 
        CDEF:binminmax=binmaxraw,binminraw,- 
        CDEF:boutminmax=boutmax,boutmin,- 
        AREA:binmin#ffffff: 
        STACK:binmax#f00000: 
        LINE1:binmin#a0a0a0: 
        LINE1:binmax#a0a0a0: 
        LINE2:bin#efb71d:incoming 
        GPRINT:bin:MIN:%.2lf 
        GPRINT:bin:AVERAGE:%.2lf 
        GPRINT:bin:MAX:%.2lf 
        AREA:boutmin#ffffff: 
        STACK:boutminmax#00f000: 
        LINE1:boutmin#a0a0a0: 
        LINE1:boutmax#a0a0a0: 
        LINE2:bout#a0a735:outgoing 
        GPRINT:bout:MIN:%.2lf 
        GPRINT:bout:AVERAGE:%.2lf 
        GPRINT:bout:MAX:%.2lf 
        "
$RRDTOOL graph $OUTDIR/$OUTPRE-hour.png -a PNG --start -14400 $DISP -w $WIDTH -h $HEIGHT
$RRDTOOL graph $OUTDIR/$OUTPRE-day.png -a PNG --start -86400 $DISP -w $WIDTH -h $HEIGHT
$RRDTOOL graph $OUTDIR/$OUTPRE-month.png -a PNG --start -2592000 $DISP -w $WIDTH -h $HEIGHT
 
OUTPRE=lighttpd-requests
 
DISP="-v req --title RequestsperSecond -u 1 
        DEF:req=$INFILE:Requests:AVERAGE 
        DEF:reqmax=$INFILE:Requests:MAX 
        DEF:reqmin=$INFILE:Requests:MIN 
        CDEF:reqminmax=reqmax,reqmin,- 
        AREA:reqmin#ffffff: 
        STACK:reqminmax#00f000: 
        LINE1:reqmin#a0a0a0: 
        LINE1:reqmax#a0a0a0: 
        LINE2:req#00a735:requests"
 
$RRDTOOL graph $OUTDIR/$OUTPRE-hour.png -a PNG --start -14400 $DISP -w $WIDTH -h $HEIGHT
$RRDTOOL graph $OUTDIR/$OUTPRE-day.png -a PNG --start -86400 $DISP -w $WIDTH -h $HEIGHT
$RRDTOOL graph $OUTDIR/$OUTPRE-month.png -a PNG --start -2592000 $DISP -w $WIDTH -h $HEIGHT

4b. 定义虚拟主机
修改/etc/lighttpd/lighttpd.conf

1
2
3
    $HTTP["host"] == "www.yourdomain.com" {
        server.document-root =var.basedir +  "/yourdomain.com/rrd/"
}

4c. 定义html文件 index.htm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
        <title>Lighttpd traffic &amp; requests</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <meta http-equiv="content-style-type" content="text/css">
        <style type="text/css">
<!--
        div { text-align:center; }
        img { width:693px; height:431px; }
-->
        </style>
</head>
 
<body>
    <div>
        <h2>Lighttpd Traffic</h2>
        <img src="lighttpd-traffic-hour.png"   alt="graph1"><br>
        <img src="lighttpd-traffic-day.png"    alt="graph2"><br>
        <img src="lighttpd-traffic-month.png"  alt="graph3"><br>
    </div>
    <div>
        <h2>Lighttpd Requests</h2>
        <img src="lighttpd-requests-hour.png"  alt="graph4"><br>
        <img src="lighttpd-requests-day.png"   alt="graph5"><br>
        <img src="lighttpd-requests-month.png" alt="graph6"><br>
    </div>
  </body>
</html>

4d. 访问www.yourdomain.com/index.htm就可以看到图片了
5. 加入到cron定时任务中

1
2
3
# crontab -e
输入:
0,20,40 * * * * nice -n 10 /etc/lighttpd/rrdtool.sh >& /dev/null

这样每隔20分钟就会自动执行一下生成图片的脚本,来显示lighttpd的流量情况

2 Comments :, more...

lighttpd配置之代理、负载均衡(mod_proxy)

by on 七.12, 2009, under Linux

使用proxy可以使lighttpd成为一个代理服务器。例如将java的请求全都转向给jboss来处理
mod_proxy有三个标签:
proxy.debug,0或者1. 表示是否启动调试模式。 1表示启动
proxy.balance,使用负载均衡的模式。可以使“hash”,“round-robin”,”fair”三种模式之一。
’round-robin’ 交替轮训, ‘hash’ 根据请求的url产生一个 hash值,来确保同样的请求的url都访问同样的主机
‘fair’ is the normal load-based, passive balancing.

语法结构

1
2
3
4
5
6
7
8
9
 ( <extension> => 
      ( [ <name> => ]
        ( "host" => <string> ,
          "port" => <integer> ),
        ( "host" => <string> ,
          "port" => <integer> )
      ),
      <extension> => ... 
    )

* : 表示请求url的文件扩展名或者文件前缀 (如果以”/”开始); 可以是空 (“”) 表示所有的请求
* : 可选名称
* “host”: 被代理的服务器的ip
* “port”: 被代理服务器的端口,默认是80

如:

1
2
3
4
5
6
proxy.server = ( ".jsp" =>
                       ( ( 
                           "host" => "10.0.0.242",
                           "port" => 8080
                         ) )
                     )

再如:

1
2
3
4
5
6
7
8
$HTTP["host"] == "www.domain.me" {
        proxy.server  = ( "" =>
        ( ( 
                "host" => "127.0.0.1",
                "port"=>"8080"  
        ) ) 
        )
}

负载均衡的例子,例如有8个squid缓存,需要用lighttpd做负载均衡

1
2
3
4
5
6
7
8
9
10
11
  $HTTP["host"] == "www.example.org" {
    proxy.balance = "hash" 
    proxy.server  = ( "" => ( ( "host" => "10.0.0.10" ),
                              ( "host" => "10.0.0.11" ),
                              ( "host" => "10.0.0.12" ),
                              ( "host" => "10.0.0.13" ),
                              ( "host" => "10.0.0.14" ),
                              ( "host" => "10.0.0.15" ),
                              ( "host" => "10.0.0.16" ),
                              ( "host" => "10.0.0.17" ) ) )
  }

当一个服务器宕机后,它上面的请求将被转移给其他设备server

Leave a Comment :, , more...

lighttpd配置之重定向(mod_redirect)

by on 七.12, 2009, under Linux

有时候需要将一个url重定向到另外一个url上。 如最简单的将不带www的域名重定向到www上,例如将domain.com重定向到www.domain.com上。

这时候就可以使用mod_redirect模块。 如上面的例子可以在/etc/lighttpd/lighttpd.conf中使用下面的代码来解决:
1. 激活配置文件中的mod_redirect模块,去掉其前面的#
2. 插入下面代码

1
2
3
4
5
$HTTP["host"] =~ "^([^.]+.[^.]+)$" {
  url.redirect = (
      ".*" => "http://www.%1"
  )
}

其中,%1表示$HTTP["host"] 中正则表达式中括号中匹配的内容。%1表示第一个匹配值,%2表示第二个匹配值。%0表示整个字符串

再如,希望把www.prosight.me/blog/index.php/2009/03/archives/321这样的url跳转到blog.prosight.me/index.php/2009/03/321这个url上的话,就使用如下配置:

1
2
3
4
5
6
7
$HTTP["host"] == "www.prosight.me" {
  url.redirect = (
       "^/blog/index.php/([0-9]+/[0-9]+)/archives/([0-9]+)$"
       => "http://blog.prosight.me/index.php/$1/$2",
       "^/blog(/)?$" => "http://blog.prosight.me"
  )
}

其中  $1表示在url.redirect里正则表达式中第一个括号匹配的内容,$2表示第二个匹配的内容,以此类推。

url.redirect可以放置在任何$HTTP["host"] 块中,与其他模块共同使用。例如与rewrite一同使用,或者跟server.document-root属性一起使用来共同配置一个虚拟主机。

Leave a Comment :, more...

lighttpd配置之缓存静态文件(mod_expire)

by on 七.12, 2009, under Linux

网站中经常有jpg,png,css等静态文件,而且一般也不太变化。可以通过lighttpd的mod_expire模块,让客户端缓存这些静态文件,不必每次都向服务器请求最新的数据。

1
2
3
$HTTP["url"] =~ "(.png|.css|.js|.jpg|.gif)$" {
         expire.url = ( "" => "access 2 months" )
}

在/etc/lighttpd/lighttpd.conf中,激活mod_expire模块,并添加上述代码。 这样png,css,js,jpg,gif等静态文件就会在客户端缓存2个月。可以有效的提高二次访问者的访问速度,降低服务器的流量压力

Leave a Comment :, more...

lighttpd配置之压缩文件(mod_compress)

by on 七.11, 2009, under Linux

在页面中使用gzip可以有效的减低页面的大小,加快网页的下载速度。在lighttpd中对php页面进行压缩,需要两个步骤:

1. 编辑 lighttpd.conf

将 “mod_compress” 设为启用

接著找到

## compress module

在下面加入

1
2
compress.cache-dir         = “/var/tmp/lighttpd/cache/”
compress.filetype          = (”text/plain”, “text/html”, “text/css”, “text/javascript”)

做完上面的动作后,
基本上 .txt .html .css .js 的文件都会被Gzip压缩了。但php此时还没有压缩

对于动态的php文件,还需要在php.ini中做相关设置,否则.php页面还是不使用压缩模式
2. 编辑 php.ini
修改
zlib.output_compression = On
zlib.output_handler = On

重新启动Lighttpd。
这样php也压缩了

Leave a Comment :, , , more...

CentOS环境中lighttpd+php+fastcgi+eAccelerator安装配置

by on 六.21, 2009, under Linux, PHP

Lighttpd 作为新一代的web server,以小巧(不到1M的大小)、快速而著称,因为服务器上安装了rails、java,并以lighttpd为前端代理服务器,不想再部署apache了,所以直接使用lighttpd来部署,顺便看一下性能如何。

本文主要介绍在CentOS下,配置一套用lighttp作为web server的php环境

· 安装Lighttpd
从http://www.lighttpd.net/download/下载源码
安装前先检查pcre是否安装,需要pcre和pcre-devel两个包。 用yum search pcre*检查,如果都是installed就是都安装了。否则安装缺少的包。

1
2
3
4
5
yum install pcre-devel
 
tar xzvf lighttpd-1.4.23.tar.gz
cd lighttpd-1.4.23
./configure --prefix=/usr/local/lighttpd

configure完毕以后,会给出一个激活的模块和没有激活模块的清单,可以检查一下,是否自己需要的模块都已经激活,在enable的模块中一定要有“mod_rewrite”这一项,否则重新检查pcre是否安装。然后编译安装:

1
make && make install

编译后配置:

1
2
3
cp doc/sysconfig.lighttpd /etc/sysconfig/lighttpd
mkdir /etc/lighttpd
cp doc/lighttpd.conf /etc/lighttpd/lighttpd.conf

如果你的Linux是RedHat/CentOS,那么:

1
cp doc/rc.lighttpd.redhat /etc/init.d/lighttpd

如果你的Linux是SuSE,那么:

1
cp doc/rc.lighttpd /etc/init.d/lighttpd

其他Linux发行版本可以自行参考该文件内容进行修改。然后修改/etc/init.d/lighttpd,把

1
lighttpd="/usr/sbin/lighttpd"

改为

1
lighttpd="/usr/local/lighttpd/sbin/lighttpd"

此脚本用来控制lighttpd的启动关闭和重起:

1
2
3
/etc/init.d/lighttpd start
/etc/init.d/lighttpd stop
/etc/init.d/lighttpd restart

如果你希望服务器启动的时候就启动lighttpd,那么:

1
chkconfig lighttpd on

这样lighttpd就安装好了,接下来需要配置lighttpd。

配置Lighttpd

修改/etc/lighttpd/lighttpd.conf
1)server.modules
取消需要用到模块的注释,mod_rewrite,mod_access,mod_fastcgi,mod_simple_vhost,mod_cgi,mod_compress,mod_accesslog是一般需要用到的。

2)server.document-root, server.error-log,accesslog.filename需要指定相应的目录

3)用什么权限来运行lighttpd
server.username = “nobody”
server.groupname = “nobody”
从安全角度来说,不建议用root权限运行web server,可以自行指定普通用户权限。

4)静态文件压缩
compress.cache-dir = “/tmp/lighttpd/cache/compress”
compress.filetype = (“text/plain”, “text/html”,”text/javascript”,”text/css”)
可以指定某些静态资源类型使用压缩方式传输,节省带宽,对于大量AJAX应用来说,可以极大提高页面加载速度。

5)配置ruby on rails

最简单的配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$HTTP["host"] == "www.xxx.com" {
 server.document-root = "/yourrails/public"
 server.error-handler-404 = "/dispatch.fcgi"
 fastcgi.server = (".fcgi" =>
    ("localhost" =>
      ("min-procs" => 10,
       "max-procs" => 10,
       "socket" => "/tmp/lighttpd/socket/rails.socket",
       "bin-path" => "/yourrails/public/dispatch.fcgi",
       "bin-environment" => ("RAILS_ENV" => "production")
      )
    )
 )
}

即由lighttpd启动10个FCGI进程,lighttpd和FCGI之间使用本机Unix Socket通信。

如果想指定www.abc.com以及所有二级域名,则需要把第一行改为
$HTTP[”host”] =~ “(^|.)abc.com” {

}

如果要设置代理,比如lighttpd和tomcat整合,tomcat放在lighttpd后面,则需要通过代理访问tomcat

$HTTP["host"] =~ “www.domain.cn” {
proxy.server = ( “” => ( “localhost” => ( “host”=> “127.0.0.1″, “port”=> 8080 ) ) )
}

则www.domain.cn为主机的网址都交给tomcat处理,tomcat的端口号为8080. 在tomcat的虚拟主机中,需要捕获www.domain.cn这个主机名,设置这个虚拟主机。这里的host都是跟tomcat里面的虚拟主机对应的。

· 安装支持fastcgi的PHP
安装PHP所需的相关类库
curl

1
2
3
4
5
6
7
wget http://curl.cs.pu.edu.tw/download/curl-7.19.5.tar.bz2
 
tar xvjf curl-7.19.5.tar.bz2
cd curl-7.19.5
./configure --prefix=/usr/local/curl
make
make install

gettext

1
2
3
4
5
6
wget ftp://ftp.ntu.edu.tw/pub/gnu/gnu/gettext/gettext-0.17.tar.gz
tar xvzf gettext-0.17.tar.gz
cd gettext-0.17
./configure --prefix=/usr/local/gettext
make
make install

zlib

1
2
3
4
5
wget http://kent.dl.sourceforge.net/sourceforge/libpng/zlib-1.2.3.tar.gz
tar xvzf zlib-1.2.3.tar.gz
cd zlib-1.2.3
./configure --prefix=/usr/local/zlib
make && make install

libpng

1
2
3
4
5
wget http://www.mirrorservice.org/sites/download.sourceforge.net/pub/sourceforge/l/li/libpng/libpng-1.2.9.tar.gz
tar xvzf libpng-1.2.9.tar.gz
cd libpng-1.2.9
./configure --prefix=/usr/local/libpng
make && make install

jpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
wget http://www.ijg.org/files/jpegsrc.v6b.tar.gz
tar xvzf jpegsrc.v6b.tar.gz
cd jpeg-6b/
./configure --prefix=/usr/local/jpeg6
make
 
mkdir /usr/local/jpeg6/bin
mkdir -p /usr/local/jpeg6/bin
mkdir -p /usr/local/jpeg6/man/man1
mkdir -p /usr/local/jpeg6/lib
mkdir -p /usr/local/jpeg6/include
make install-lib
make install

freetype

1
2
3
4
5
6
wget http://download.savannah.gnu.org/releases/freetype/freetype-2.3.9.tar.gz
tar xvzf freetype-2.3.9.tar.gz
cd freetype-2.3.9
./configure --prefix=/usr/local/freetype2
make
make install

gd

1
2
3
4
5
6
7
wget http://www.libgd.org/releases/gd-2.0.35.tar.gz
tar xvzf gd-2.0.35.tar.gz
cd gd-2.0.35
./configure --prefix=/usr/local/gd2 --with-zlib=/usr/local/zlib/ --with-png=/usr/local/libpng/ --with-jpeg=/usr/local/jpeg6/ --with-freetype=/usr/local/freetype2/
make
如果第一次make出错,试着再make一次,我就是这样,第二次就对了。
make install

PHP

1
2
3
4
5
6
7
8
tar xvzf php-5.2.10.tar.gz
cd php-5.2.10
./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-pdo-mysql=/usr/local/mysql --with-jpeg-dir=/usr/local/jpeg6/ --with-png-dir=/usr/local/libpng/ --with-gd=/usr/local/gd2/ --with-freetype-dir=/usr/local/freetype2/  --with-zlib-dir=/usr/local/zlib --with-curl=/usr/local/curl --with-gettext=/usr/local/gettext --enable-fastcgi --enable-zend-multibyte --with-config-file-path=/etc --enable-discard-path --enable-force-cgi-redirect
make
make install
cp php.ini-dist /etc/php.ini
 
可以使用php -m查看你安装的模块

eAccelerator
eAccelerator是一个开源的PHP加速器

1
2
3
4
5
6
7
8
wget http://bart.eaccelerator.net/source/0.9.5.3/eaccelerator-0.9.5.3.tar.bz2
tar xjvf eaccelerator-0.9.5.3.tar.bz2
cd eaccelerator-0.9.5.3
export PHP_PREFIX="/usr/local/php"
$PHP_PREFIX/bin/phpize
./configure --enable-eaccelerator=shared --with-php-config=$PHP_PREFIX/bin/php-config
make
make install

执行好后,会提示安装到的路径,下面会用到,如我的被安装到这里
/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613
编辑php.ini中的内容
vim /etc/php.ini

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cgi.fix_pathinfo = 1
 
zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"
eaccelerator.shm_size="16"
eaccelerator.cache_dir="/tmp/eaccelerator"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="0"
eaccelerator.shm_prune_period="0"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"

如果一切顺利,你可以通过下面命令来验证是否安装成功

1
2
3
4
5
$ php -v
PHP 5.2.10 (cli) (built: Jun 20 2009 23:32:09)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2009 Zend Technologies
    with eAccelerator v0.9.5.3, Copyright (c) 2004-2006 eAccelerator, by eAccelerator

修改/etc/lighttpd/lighttpd.conf文件,添加下面的配置
vim /etc/lighttpd/lighttpd.conf

1
2
3
4
5
6
7
8
fastcgi.server             = ( ".php" =>
                               ( "localhost" =>
                                 (
                                   "socket" => "/tmp/php-fastcgi.socket",
                                   "bin-path" => "/usr/local/php/bin/php-cgi"
                                 )
                               )
                            )

重启lighttpd

1
/etc/init.d/lighttpd restart

写一个php测试文件在lighttpd的网站目录里,测试php是否安装成功

Leave a Comment :, , , , , , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit my friends!

A few highly recommended friends...