设为首页 收藏本站
查看: 878|回复: 0

[经验分享] apache 支持gzip

[复制链接]

尚未签到

发表于 2018-11-27 09:25:20 | 显示全部楼层 |阅读模式
  首先查看apache是否加载了mod_deflate.so模块,如果没有需要安装加载。找到并下载和当前apache版本相同的源码文件,解压缩到/home目录下,在apache安装目录下执行:
/usr/local/apache2/bin/apxs -i -c /usr/local/httpd-2.0.63/modules/filters/mod_deflate.c  在 httpd.conf添加
LoadModule deflate_module modules/mod_deflate.so#对text/html text/php text/png text/jpg text/plain text/css text/xml text/javascript  #启用GZIP压缩
  AddOutputFilterByType DEFLATE text/html text/php text/png text/jpg text/plain text/css text/xml text/javascript
  #压缩级别 9 性能最佳
  DeflateCompressionLevel 9
  #启用deflate模块对本站点的所有输出进行GZIP压缩
  SetOutputFilter DEFLATE
  最后重启Apache就可以了。。。
  ########################
  以上红字 添加gzip类型 有一下 橘色字的其他解决方法
  
  
  #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.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
  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
  SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
  SetEnvIfNoCase Request_URI .(?:pdf|doc)$ no-gzip dont-vary
  AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css
  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
  
  
  但是本人没有添加header模块,在重启httpd时候提示
Invalid command 'Header', perhaps misspelled or defined by a module not included in the server configuration 需要操作1:/usr/local/apache2/bin/apxs -i -c /home/httpd-2.0.63/modules/metadata/mod_headers.c或者/usr/local/apache2/bin/apxs -i -c /home/httpd-2.0.63/modules/filters/mod_headers.c2:增加LoadModule headers_module modules/mod_headers.so  下面摘抄一下网上介绍的在linux下开启gzip压缩功能的文章内容:
  而如果是 Linux/Unix 操作系统,如果你没有在编译安装的过程中将所需要的两个模块 mod_deflate 和 mod_headers 编译进去的话,那就稍微有点麻烦,首先我们先看如何在编译安装 Apache 过程中也同时编译这两个模块,请在执行 configure 程序时增加两个参数分别是:
  # ./configure –enable-deflate –enable-headers
  这样在编译完 Apache 后就可以直接在 httpd.conf 中启用并配置 deflate 模块了,配置的方法跟 Windows 平台下是相同的。
  如果说您的 Apache 已经在运行了,不想再重新编译一次,那也可以选择只编译 deflate 模块所需的文件 mod_deflate.c 和 mod_headers.c。这两个文件位于 {apache-src}/modules/filters/ 目录下(其中 {apache-src} 为 apache 源文件所在的目录)。使用如下命令来单独编译这两个源文件。
  # {apache-bin}/apxs -i -a -c {apache-src}/modules/filters/mod_deflate.c
  # {apache-bin}/apxs –i –a –c {apache-src}/modules/filters/mod_headers.c
  When install the header module the src directory is {apache-src}/modules/metadata/mod_headers.c
  So we need change the command as follows:{apache-bin}/apxs -i -a -c {apache-src}/modules/metadata/mod_headers.c
  其中 {apache-bin} 为 Apache 安装目录下的 bin 目录,接下来在 httpd.conf 直接配置该模块即可。
  很多时候你在单独编译 deflate 模块的时候可能会碰到编译错误,提示是:
  Cannot load /opt/apache/modules/mod_deflate.so into server: /opt/apache/modules/mod_deflate.so: undefined symbol: deflate
  解决的方法如下:
  编辑 /usr/local/apache2/bin/apr-config 文件修改其中的 LDFLAGS 值为 “-lz”,然后再重新编译 mod_deflate 模块,apxs -ica mod_deflate.c 即可。
  为了省却不必要的麻烦,请尽量在编译安装时直接加上 –enable-deflate –enable-headers 参数。
  检验压缩效果:
  http://gzip.zzbaike.com/
检测结果是否压缩?是压缩类型:gzip源文件大小:77.6KB压缩后大小:15.67KB压缩率:  79.8%
Header信息DateFri, 07 Jan 2011 06:05:42 GMTServerApache/2.2.17 (Unix) PHP/5.2.14Accept-RangesbytesCache-Controlmax-age=300ExpiresFri, 07 Jan 2011 06:10:42 GMTVaryAccept-Encoding,User-AgentContent-EncodinggzipContent-Length16082Content-Typetext/html

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-640131-1-1.html 上篇帖子: Apache之CGI 下篇帖子: apache log分析
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表