简体   繁体   中英

apache mod_deflate for css and js

.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 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. Any tips ?

To test that mod_deflate is ACTUALLY working, try putting this at the top of your httpd.conf file:

LoadModule deflate_module modules/mod_deflate.so

and in the bottom of your httpd.conf file:

##############################################
#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.

After you get some hits on your site, check "compression.log" in the apache logs folder. Post the output.

In httpd.conf make sure this line is not commented

LoadModule deflate_module modules/mod_deflate.so

if it is then uncomment it and restart the apache service

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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