简体   繁体   English

apache mod_deflate用于CSS和JS

[英]apache mod_deflate for css and js

.htaccess 的.htaccess

<ifmodule mod_deflate.c>
# compress the files
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

# removes some bugs
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
Header append Vary User-Agent
</ifmodule>

And for the php files 对于PHP文件

 <?php if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip'))
ob_start("ob_gzhandler"); else ob_start(); ?>

And for the php files everything is OK but no for css,js files. 对于php文件,一切正常,但对于css,js文件则没有。 Any tips ? 有小费吗 ?

To test that mod_deflate is ACTUALLY working, try putting this at the top of your httpd.conf file: 要测试mod_deflate是否可以正常工作,请尝试将其放在httpd.conf文件的顶部:

LoadModule deflate_module modules/mod_deflate.so

and in the bottom of your httpd.conf file: 并在您的httpd.conf文件的底部:

##############################################
#Mod Deflate Config:
SetOutputFilter DEFLATE
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
DeflateFilterNote Input input_info
DeflateFilterNote Output output_info
DeflateFilterNote Ratio ratio_info
LogFormat '(%{ratio_info}n%%) "%r" %{output_info}n/%{input_info}n' deflate
CustomLog logs/compression.log deflate
###############################################

This will compress just about anything ONLY if the browser sends the gzip-ready header. 仅当浏览器发送gzip-ready标头时,它才会压缩几乎所有内容。

After you get some hits on your site, check "compression.log" in the apache logs folder. 在您的网站上获得一些点击后,请检查apache日志文件夹中的“ compression.log”。 Post the output. 发布输出。

In httpd.conf make sure this line is not commented 在httpd.conf中,确保未注释此行

LoadModule deflate_module modules/mod_deflate.so

if it is then uncomment it and restart the apache service 如果取消注释,然后重新启动apache服务

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM