18

I would like to disable gzip compression for some media files which are already compressed on an Apache server via the .htaccess file.
Reason: as it's written on e.g. jPlayer's site, gzip encoding should be disabled for media files: "Media files are already compressed and the GZIP will just waste CPU on your server. The Adobe Flash Plugin will experience issues if you GZIP the media."

I'm currently having the problem that Content-Length header is not properly set when gzip is enabled - so when playing some mp3-files with a SoundManager2 player, the track's length progress bar doesn't work appropriately (so maybe that's the problem they told about on jPlayer's site).

I can test if a content is served gzipped here.
I do have mod_deflate, mod_mime and mod_rewrite modules enabled on the server.
According to a phpinfo(), here is a list of all the loaded modules:

core mod_log_config mod_logio itk http_core mod_so mod_alias mod_auth_basic mod_authn_file mod_authz_default mod_authz_groupfile mod_authz_host mod_authz_user mod_autoindex mod_cgi mod_dav mod_dav_svn mod_authz_svn mod_deflate mod_dir mod_env mod_mime mod_negotiation mod_php5 mod_reqtimeout mod_rewrite mod_setenvif mod_ssl mod_status

I'm using Drupal 6, so I already have a RewriteRule, which is the following:

# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

I've already tried these to disable gzip, but they didn't work (there are 6 different tries! - maybe some of them would have to be set globally in Apache's httpd.conf?!):

  • # http://www.cyberciti.biz/tips/speed-up-apache-20-web-access-or-downloads-with-mod_deflate.html
    SetOutputFilter DEFLATE
    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$ no-gzip dont-vary
    SetEnvIfNoCase Request_URI \.avi$ no-gzip dont-vary
    SetEnvIfNoCase Request_URI \.mov$ no-gzip dont-vary
    SetEnvIfNoCase Request_URI \.mp3$ no-gzip dont-vary
    SetEnvIfNoCase Request_URI \.mp4$ no-gzip dont-vary
    SetEnvIfNoCase Request_URI \.rm$ no-gzip dont-vary    
    
  • ## Step 2. here: http://www.mydigitallife.info/how-to-enable-mod_deflate-gzip-compression-on-cpanel-web-hosts/
    <IfModule mod_deflate.c>
    SetOutputFilter DEFLATE
    
    RemoveOutputFilter mp3
    # Don’t compress already-compressed files
    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 .(?:avi|mov|mp3|mp4|rm|flv|swf|mp?g)$ no-gzip dont-vary
    SetEnvIfNoCase Request_URI .pdf$ no-gzip dont-vary
    </IfModule>
    
  • RemoveOutputFilter mp3
    
  • # for files that end with ".mp3"
    <FilesMatch \.mp3$>
    SetEnv no-gzip 1
    </FilesMatch>
    
  • RewriteRule \.mp3$ - [NS,E=no-gzip:1,E=dont-vary:1]
    
  • RewriteRule ^((.*)\.mp3)$ $1.mp3 [NS,E=no-gzip:1,E=dont-vary:1]   
    

The only one which works correctly, and disables gzip compression, BUT it is global:

  • RewriteRule ^(.*)$ $1 [NS,E=no-gzip:1,E=dont-vary:1]
    

Response headers for an mp3-file when NOT using this RewriteRule: http://pastebin.com/AkUZ6m5Y
Response headers for an mp3-file when using this RewriteRule: http://pastebin.com/b8j3NF6D

Sk8erPeter
  • 6,899
  • 9
  • 48
  • 67
  • 1
    If the media files are in a different folder, you could put the htaccess with the working line in that folder, and don't affect other parts of the website. – Gerben Feb 12 '12 at 14:38
  • +1, it's a possible solution, but it's not absolutely sure that my media files get into a certain directory, so I would like to make it more "flexible". :) – Sk8erPeter Feb 12 '12 at 14:48
  • @SkBerPeter - Do you have some mod_rewrite rules that use the [L] flag? Making rewriting work in a .htaccess file (as opposed to a .conf file where it would happen much much earlier) is a giant kludge, which among other things changes the action of the [L] slightly. Instead of exiting, the [L] loops back to the top of the .htaccess, and as a side effect all environment variables are cleared! Maybe this is why you set "no-gzip", yet when referenced it doesn't seem to be set any longer. If it's available (2.3.9 and later) perhaps the [END] flag will work better. – Chuck Kollars Dec 02 '13 at 00:59
  • @SkPerPeter rather than the blanket "SetOutputFilter" statement, I turn on compression with "AddOutputFilterByType DEFLATE text/html text/css text/javascript application/x-javascript application/javascript application/x-font-ttf application/vnd.ms-fontobject image/svg+xml" ...by mime type rather than by extension is pretty much the same thing since there's typically a 1:1 mapping. Since it never gets turned on for the mime type of audio files in the first place, there's no need to turn it back off. (Also note it's _not_ on for already-compressed files where it does more harm than good.) – Chuck Kollars Dec 03 '13 at 06:17
  • Any idea what I might be able to do in the global httpd.conf to disable compression for all files? – ajfbiw.s Nov 06 '20 at 01:54

6 Answers6

10

I had to disable compression for odp files for use by external plugin Just added the following rule in .htaccess file

SetOutputFilter DEFLATE
SetEnvIfNoCase Request_URI \.odp$ no-gzip dont-vary

And the server disabled compression for odp files Make sure to clear the browser cache before testing

Lohith
  • 136
  • 1
  • 4
4

Are you not going about this the wrong way round by using the directive SetOutputFilter DEFLATE and then trying to disable this for stream which already include some form of compresstion? Isn't it a lot easier not to use this directive and then compress the stream that are compressible. E.g.

<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/x-javascript application/javascript application/ecmascript application/rss+xml
</IfModule>

And possibly adding a Vary header:

<IfModule mod_headers.c>
    <FilesMatch "\.(js|css|xml|html)$">
        Header append Vary Accept-Encoding
    </FilesMatch>
</IfModule>

OK this may miss the odd type that you've not thought of, but it will achieve 99+% of your compression potential.

Sk8erPeter
  • 6,899
  • 9
  • 48
  • 67
TerryE
  • 10,724
  • 5
  • 26
  • 48
  • Thanks for your answer! I tried it, and it's interesting that when I add the FilesMatch part that you pasted here to my global `.htaccess` file, then I get an "500 Internal Server Error"... BUT which is worse, Content-Length header is still not set when playing mp3-files or visiting any other pages, which is bad when serving media files because of the players. If I use the global `RewriteRule ^(.*)$ $1 [NS,E=no-gzip:1,E=dont-vary:1]` line, Content-Length header is set and every players work correctly. But disabling gzip globally is not a good idea. :( – Sk8erPeter Feb 12 '12 at 15:23
  • Please look at my edited question, I pasted the response headers for an mp3-file when playing it with and without the mentioned working RewriteRule (look at the pastebin links). Thanks! – Sk8erPeter Feb 12 '12 at 15:33
  • These response headers are exactly what I would expect with and without deflate working on the stream. You _didn't_ comment on whether you'd commented out the `SetOutputFilter DEFLATE` line which you would need to do for this to work. – TerryE Feb 12 '12 at 16:13
  • Also on the `Header` directive, this does assume that `mod_headers`is loaded; you will get a 500 if your hosting provider isn't loading this. Sorry. – TerryE Feb 12 '12 at 16:17
  • I completely deleted my own tries and pasted your code into my .htaccess, but unfortunately it didn't solve the problem. Maybe it's too Drupal-related: the audio track playing via a SoundManager2 player (http://goo.gl/gCdbq) is happening so that it goes through the Audio module's audio_play() function which is here: http://drupalcontrib.org/api/drupal/contributions!audio!audio.pages.inc/function/audio_play/6 . As you can see, it sends the appropriate headers to disable compression. Don't really understand how it's getting "overridden" somehow later: there is no Content-Length in the response. – Sk8erPeter Feb 13 '12 at 00:18
  • I edited Your post adding `` tags to prevent 500 Internal Server Errors I got earlier. While investigating the case I realized that the player's "progress bar" does work in Firefox (10.0.2 and earlier versions too) and Opera (11.61), so for these browsers it doesn't matter that the appropriate Content-Length header doesn't get served. But in Chrome, when gzipping media content, the browser doesn't get the Content-Length header and the player's progress bar doesn't work, for that I have to completely turn gzip off in `.htaccess`. How to restrict that to music and video contents? – Sk8erPeter Feb 22 '12 at 10:36
1

To disable gzip compression on just Adobe Flash Player files (SWFs) on my Apache server, I added this to my .htaccess file:

<IfModule mod_headers.c>
    <FilesMatch "\.swf$">
        RewriteRule ^(.*)$ $1 [NS,E=no-gzip:1,E=dont-vary:1]
    </FilesMatch>
</IfModule>

If you wanted to, you could disable gzip compression for other file types as well:

<IfModule mod_headers.c>
    <FilesMatch "\.(js|css|swf)$">
        RewriteRule ^(.*)$ $1 [NS,E=no-gzip:1,E=dont-vary:1]
    </FilesMatch>
</IfModule>
totallytotallyamazing
  • 2,765
  • 1
  • 20
  • 26
0

this seems outdated : https://www.varnish-cache.org/docs/3.0/tutorial/compression.html#gzip-and-esi

GZIP and ESI

If you are using Edge Side Includes you'll be happy to note that ESI and GZIP work together really well. Varnish will magically decompress the content to do the ESI-processing, then recompress it for efficient storage and delivery.

Bruno
  • 1,088
  • 1
  • 13
  • 31
0

I think you are not using compression in your media. Did you check that you are in fact deflating files? The module can be loaded in memory, but that doesn't mean it's compressing files. If your .htaccess only has rewrite rules chances are you are not compressing any kind of content.

Javier Constanzo
  • 443
  • 1
  • 4
  • 16
  • *"If your .htaccess only has rewrite rules chances are you are not compressing any kind of content."* I pasted the RewriteRule here that Drupal is using and is set by default - but why would that mean that if gzip compression is enabled on the server, with the usage of a RewriteRule, I'm not compressing any content? The connection is not clear to me between the two things. Using mod_rewrite doesn't mean compression is getting disabled. But what mostly matters: I tested the compression before asking any questions. As I mentioned, I can test it for example here: http://goo.gl/WCzQZ, it says yes. – Sk8erPeter Feb 12 '12 at 14:45
-1

I know this thread is old, but I have gone through the same path.

Two things I have done.

  1. I enabled .htaccess and disabled gzip for a folder completely.

    <Files "*.gz.asc">
         RemoveEncoding .gz
     </Files>
    
  2. put a reqwrite rule to disable

    RewriteRule "\.js\.gz$" "-" [T=text/javascript,E=no-gzip:1] 
    

Both of these worked for me I would suggest going to Apache documentation first before searching on forums.

for more information please go to Apache website.

https://httpd.apache.org/docs/2.4/mod/mod_deflate.html https://httpd.apache.org/docs/2.4/mod/mod_mime.html#addtype

Margaret Bloom
  • 41,768
  • 5
  • 78
  • 124
Rakesh
  • 35
  • 5