Apache两个模块mod-expires和mod_deflate的使用

mod_expires 模块的主要作用是自动生成页面头部信息中的 Expires 标签和 Cache-Control 标签,从而降低客户端的访问频率和次数,达到减少不必要流量和增加访问速度的目的。

mod_expires 是 apache 众多模块中配置比较简单的一个,它一共只有三条指令:

ExpiresActive
ExpiresByType
ExpiresDefault

 

ExpiresActive 指令:打开或关闭产生”Expires:”和”Cache-Control:”头的功能。

ExpiresByType 指令:指定MIME类型的文档(例如:text/html)的过期时间。
ExpiresDefault 指令:默认所有文档的过期时间。

 

过期时间的写法共有五种:access、now、A、modification及M。 示例如下:

“access plus 1 month”
“access plus 4 weeks”
“now plus 30 days”
“modification plus 5 hours 3 minutes”
A2592000
M604800

 

access、now及A 三种写法的意义相同,指过期时间从访问时开始计算。
modification及M 的意义相同,指过期时间是以被访问文件的最后修改时间开始计算。
所以,后一种写法只对静态文件起作用,而由脚本生成的动态页面不受它的作用。
一般情况下,我们都使用access方式,下面是一个我使用的配置:

<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresDefault “access plus 12 hours”
  ExpiresByType text/html “access plus 3 days”
  ExpiresByType text/plain “access plus 3 days”
  ExpiresByType image/gif “access plus 30 days”
  ExpiresByType image/png “access plus 30 days”
  ExpiresByType image/jpeg “access plus 30 days”
  ExpiresByType image/x-icon “access plus 30 days”
</IfModule>

需要提醒您的是,经过Expires处理的URL地址,可以被浏览器及代理服务器缓存,请只在需要的地方使用。

 

参考文档:
Apache 1.3.x http://httpd.apache.org/docs/1.3/mod/mod_expires.html
Apache 2.0.x http://httpd.apache.org/docs/2.0/mod/mod_expires.html

 

mod_deflate 模块作用是用来对网页文件进行压缩,已减少网页文件的大小,节省带宽。

简单指定压缩固定格式文件的方法如下:

AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript

 

压缩出图片之外的任意文件方法如下:

<Location />

# Insert filter
SetOutputFilter DEFLATE

# Netscape 4.x has some problems...
BrowserMatch ^Mozilla/4 gzip-only-text/html

# Netscape 4.06-4.08 have some more problems
BrowserMatch ^Mozilla/4/.0[678] no-gzip

# MSIE masquerades as Netscape, but it is fine
# BrowserMatch /bMSIE !no-gzip !gzip-only-text/html

# NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48
# the above regex won't work. You can use the following
# workaround to get the desired effect:
BrowserMatch /bMSI[E] !no-gzip !gzip-only-text/html

# Don't compress images
SetEnvIfNoCase Request_URI /

/.(?:gif|jpe?g|png)$ no-gzip dont-vary


# Make sure proxies don't deliver the wrong content
Header append Vary User-Agent env=!dont-vary

</Location>

 

上面两个模块配置均可用在: location、vatralhost、.haccess 中。

你可能感兴趣的:(apache,浏览器,文档,regex,Access,insert)