Am 15.04.2015 um 00:14 schrieb Rainer Jung:
Am 14.04.2015 um 19:06 schrieb rbo...@apache.org:
Author: rbowen
Date: Tue Apr 14 17:06:10 2015
New Revision: 1673470
URL: http://svn.apache.org/r1673470
Log:
Add "pre-compressed content" recipe, as per
https://bz.apache.org/bugzilla/show_bug.cgi?id=51336
Modified:
httpd/httpd/trunk/docs/manual/mod/mod_deflate.xml
Modified: httpd/httpd/trunk/docs/manual/mod/mod_deflate.xml
URL:
http://svn.apache.org/viewvc/httpd/httpd/trunk/docs/manual/mod/mod_deflate.xml?rev=1673470&r1=1673469&r2=1673470&view=diff
==============================================================================
--- httpd/httpd/trunk/docs/manual/mod/mod_deflate.xml (original)
+++ httpd/httpd/trunk/docs/manual/mod/mod_deflate.xml Tue Apr 14
17:06:10 2015
@@ -178,6 +178,51 @@ SetEnvIfNoCase Request_URI \.(?:gif|jpe?
</example>
</section>
+<section id="precompressed"><title>Serving pre-compressed
+content</title>
+
+ <p>Since <module>mod_deflate</module> re-compresses content each
+ time a request is made, some performance benefit can be derived by
+ pre-compressing the content and telling mod_deflate to serve them
+ without re-compressing them. This may be accomplished using a
+ configuration like the following:</p>
+
+ <highlight language="config">
+ <IfModule mod_headers.c>
+ # Serve gzip compressed CSS files if they exist
+ # and the client accepts gzip.
+ RewriteCond %{HTTP:Accept-encoding} gzip
+ RewriteCond %{REQUEST_FILENAME}\.gz -s
+ RewriteRule ^(.*)\.css $1\.css\.gz [QSA]
+
+ # Serve gzip compressed JS files if they exist
+ # and the client accepts gzip.
+ RewriteCond %{HTTP:Accept-encoding} gzip
+ RewriteCond %{REQUEST_FILENAME}\.gz -s
+ RewriteRule ^(.*)\.js $1\.js\.gz [QSA]
+
+
+ # Serve correct content types, and prevent mod_deflate double gzip.
+ RewriteRule \.css\.gz$ - [T=text/css,E=no-gzip:1]
+ RewriteRule \.js\.gz$ - [T=text/javascript,E=no-gzip:1]
+
+
+ <FilesMatch "(\.js\.gz|\.css\.gz)$">
+ # Serve correct encoding type.
+ Header append Content-Encoding gzip
+
+ # Force proxies to cache gzipped &
+ # non-gzipped css/js files separately.
+ Header append Vary Accept-Encoding
+ </FilesMatch>
+ </IfModule>
+</IfModule>
+
+
+ </highlight>
+
+</section>
+
<directivesynopsis>
<name>DeflateFilterNote</name>
<description>Places the compression ratio in a note for
logging</description>
AFAIK mod_rewrite automatically adds headers from RewriteCond to the
Vary header as long as no "NV" flag is used. So the "Header append Vary
Accept-Encoding" part should not be necessary.
I should probably also mention, that the recipe gets easier, once one
accepts a non-standard convention for the names of the compressed
content. If you add the ".gz" before the final suffix ".js", ".css"
etc., then the web server will automatically get the content type
correct. So you can drop the "T=" flags. My variant of the recipe was:
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME} ^(.+)\.(css|js)$
RewriteCond %1.gz%2 -s
RewriteRule ^(.+)\.(css|js)$ $1.gz.$2 [QSA,E=gz:1,E=no-gzip:1]
Header append Content-Encoding gzip env=gz
The additional replacement of FilesMatch with a custom env var (here
named "gz") one can check in the "Header append" part is independent of
the other change.
One could drop the second and third RewriteCond, if one assumes that all
such files have been compressed. To create the compressed files with
non-standard name convention, one can use on Unix/Linux a script like e.g.
CONTENT_DIR=/opt/myapp/static/
PERMS=644
for suffix in css js
do
for file in \
`find $CONTENT_DIR -type f -name "*.$suffix" -a ! -name "*.gz.*"`
do
gzfile=`echo $file | sed -e 's#\.'$suffix'#.gz.'$suffix'#'`
gzip --best -c $file > $gzfile
chmod $PERMS $gzfile
echo === $file $gzfile ===
ls -ld $file $gzfile
done
done
I like the idea of adding this as a feature to mod_deflate. I actually
started doing this a few weeks ago but got side-tracked.
Regards,
Rainer