首页 > 开发 > Apache > 正文

apache开启gzip压缩方法介绍

2020-10-12 18:49:23
字体:
来源:转载
供稿:网友

gzip压缩就是可以减少你服务器带宽了,同时也可以提高网页的打开速度,这样就提升的网的用户体验了,关于如何正确开启,可以看下面的文章。

第1步:httpd.conf中打开deflate_Module和headers_Module模块

  1. LoadModule deflate_module modules/mod_deflate.so 
  2. LoadModule headers_module modules/mod_headers.so 

把前面的#号去掉,然后我们再在apache的httpd.conf中,加入下面代码

  1. <IfModule mod_deflate.c> 
  2. SetOutputFilter DEFLATE 
  3. DeflateCompressionLevel 5 
  4. AddOutputFilterByType DEFLATE text/html text/css image/gif image/jpeg image/png application/x- 
  5. javascript 
  6. </IfModule> 

或直接这样也行

  1. <IfModule deflate_module>  
  2.     SetOutputFilter DEFLATE  
  3.     # Don’t compress images and other   
  4.     SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary   
  5.     SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary   
  6.     SetEnvIfNoCase Request_URI .(?:pdf|doc)$ no-gzip dont-vary   
  7.     AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css   
  8.     AddOutputFilterByType DEFLATE application/x-javascript  
  9. </IfModule> 

我上面定写的就是一些常用的,如果大家有自己的其它格式文件我们加入进去就好了。

注意事项:mod_deflate是压缩模块,就是对要传输到客户端的代码进行gzip压缩;mod_headers模块的作用是告诉浏览器页面使用了gzip压缩,如果不开启mod_headers那么浏览器就会对gzip压缩过的.页面进行下载。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表