配置gzip压缩

首先查看apache是否加载了mod_deflate.so模块,如果没有需要安装加载。找到并下载和当前apache版本相同的源码文件,解压缩到/home目录下,在apache安装 目录下执行:

/usr/local/apache2/bin/apxs -i -c /home/httpd-2.0.63/modules/filters/mod_deflate.

会自动在 httpd.conf添加

LoadModule deflate_module modules/mod_deflate.so

或者

LoadModuledeflate_modulemodules/mod_deflate.so
LoadModuleheaders_modulemodules/mod_headers.so

打开httpd.conf后,先将上面两行配置前面的#号去掉,这样apache就会启用这两个模块,其中mod_deflate是压缩模块,就是对要 传输到客户端的代码进行gzip压缩;mod_headers模块的作用是告诉浏览器页面使用了gzip压缩,如果不开启mod_headers那么浏览 器就会对gzip压缩过的页面进行下载,而无法正常显示。

在httpd.conf中加入以下代码,可以加到任何空白地方,如果担心加错地方,就放到http.conf文件的最后一行,如果是虚拟服务器可以写.htaccess文件里面,然后放在项目下即可。

添加如下设置:

<IfModule mod_deflate.c>
<Location />
#Insert filter
SetOutputFilter DEFLATE   #必须的,就像一个开关一样,告诉apache对传输到浏览器的内容进行压缩
# Netscape 4.x has some problems…        Netscape 4.x 有一些问题,所以只压缩文件类型是text/html的
BrowserMatch ^Mozilla/4 gzip-only-text/html  
# Netscape 4.06-4.08 have some more problems    Netscape 4.06-4.08 有更多的问题,所以不开启压缩
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.59
# the above regex won’t work. You can use the following
# workaround to get the desired effect:
# BrowserMatch bMSIE !no-gzip !gzip-only-text/html force-gzip

#IE浏览器会伪装成 Netscape ,但是事实上它没有问题
BrowserMatch bMSIE !no-gzip !gzip-only-text/html

# Don’t compress images and other
SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary          #设置不对后缀gif,jpg,jpeg,png的图片文件进行压缩
SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary      #同上,就是设置不对exe,tgz,gz。。。的文件进行压缩
SetEnvIfNoCase Request_URI .(?:pdf|doc)$ no-gzip dont-vary           #同上,不对pdf,doc文件压缩
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css          #设置对文件是文本的内容进行压缩,例如text/html text/css text/plain等
AddOutputFilterByType DEFLATE application/x-javascript
# Make sure proxies don’t deliver the wrong content
Header append Vary User-Agent env=!dont-vary

#DeflateFilterNote ratio ratio_info
#LogFormat '"%v %h %l %u %t “%r" %>s %b "%{Referer}i" "%{User-Agent}i"" (%{ratio}n)' deflate
#CustomLog logs/deflate_log deflate
</Location>

DeflateCompressionLevel 9   设置压缩比率 9是最高压缩率但是很占cpu资源,不设置的话系统默认设置的是6,具体设置看服务器性能和网站业务访问

停止apache服务在命令行模式下输入 apachectl stop 停止apach服务, apachectl restart 重启服务

如果报错如下:

Cannot load /usr/local/apache2/modules/mod_deflate.so into server: /usr/local/apache2/modules/mod_deflate.so: undefine d symbol: deflate

解决如下:

vi /usr/local/apache2/bin/apr-config
修改LDFLAGS=" " 为 LDFLAGS="-lz"

停止启动服务:仍提示:

DeflateFilterNote not allowed here
CustomLog not allowed here

注释掉后,正常。

在火狐浏览器中使用yslow查看是否正常启动gzip压缩,需要注意的是要将浏览器缓存删除,否则会一直显示不成功

【http缓存的配置方法】

第1步

LoadModule expires_module modules/mod_expires.so

在httpd.conf中,找到上面的代码,然后将前面的#去掉

第2步

在httpd.conf中加入以下代码,可以加到任何空白地方,不了解apache的朋友,如果担心加错地方,就

放到http.conf文件的最后一行。

 <IfModule mod_expires.c>
    ExpiresActive On #激活http缓存,也就是个开关,必须有的一段代码
    ExpiresDefault A604800 #默认缓存时间为604800秒,也就是7天,A表示以客户端时间为准
    ExpiresByType text/css A3600 #对css文件缓存3600秒,也就是1小时,A表示以客户端时间为准
    ExpiresByType text/html A3600 #对html文件缓存3600秒,也就是1小时,A表示以客户端时间为准
    ExpiresByType application/x-javascript A3600 #对javascript文件缓存3600秒,也就是1小时,

A表示以客户端时间为准
    ExpiresByType image/gif "access plus 2 month" #对gif图片缓存2个月,以客户端时间为准
    ExpiresByType image/jpeg "access plus 2 month" #对jpeg和jpg图片缓存2个月,以客户端时间

为准
    ExpiresByType image/png "access plus 2 month" #对png图片缓存2个月,以客户端时间为准
    ExpiresByType image/x-icon "access plus 2 month" #对浏览器小图标缓存2个月,以客户端时间

为准
    ExpiresByType application/x-shockwave-flash A2592000 #对flash文件缓存2592000秒,也就是1

个月,A表示以客户端时间为准 @ itxyz.net
    #特别注意也可以使用 ExpiresByType text/css M3600  这里的M表示以文件最后修改时间为准缓存

1小时,例如有些时候,我们的HTML是大批量定时更新的,这个时候就可以用到M
  Header unset Pragma #删除掉http头信息中的Pragma,不懂的可以google一下Pragma,他也是控制浏

览器缓存的,不过是用于http1.0标准
  FileETag None
  Header unset ETag #这段代码和上面一段的作用是不使用http1.1标准中的ETag属性
  <FilesMatch "\.(js|css|ico|pdf|flv|jpg|jpeg|png|gif|mp3|mp4|swf)$"> #针对js|css|ico等后缀

的文件进行单独设置
  #Header set Expires "Thu, 15 Apr 2013 20:00:00 GMT"
  Header unset Last-Modified #不使用http头信息中的Last-Modified属性,Last-Modified是指文件

最后修改时间
  Header append Cache-Control "public" #设置为可被任何缓存区缓存
  </FilesMatch>
</IfModule>

转载于:https://www.cnblogs.com/wangyueren/archive/2012/08/06/2624585.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值