如何对 .htaccess 文件中的某些媒体文件禁用 Apache gzip 压缩?
我想对某些已通过 .htaccess
文件在 Apache 服务器上压缩的媒体文件禁用 gzip 压缩。
原因:正如 jPlayer 网站 上所写,gzip 编码应该对媒体文件禁用:“媒体文件已被压缩,GZIP 只会浪费服务器上的 CPU。如果您对媒体进行 GZIP,Adobe Flash 插件将会遇到问题。”
问题是启用 gzip 时,Content-Length
标头未正确设置 - 因此在使用 SoundManager2 播放器,曲目的长度进度条无法正常工作(所以也许这就是他们在 jPlayer 网站上提到的问题)。
我可以在此处测试内容是否经过 gzip 压缩。
我确实在服务器上启用了 mod_deflate
、mod_mime
和 mod_rewrite
模块。
根据 phpinfo(),这里是所有加载模块的列表:
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
我正在使用 Drupal 6,所以我已经有一个 RewriteRule,如下所示:
# 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]
我已经尝试过这些来禁用 gzip,但它们不起作用(有 6 种不同的尝试! - 也许其中一些必须在 Apache 的 httpd.conf 中进行全局设置?!):
# http://www.cyberciti.biz/tips/speed-up-apache-20-web-access-or-downloads-with-mod_deflate.html 设置输出过滤器放气 SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip 不变化 SetEnvIfNoCase Request_URI \.(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip 不变 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 不变化 SetEnvIfNoCase Request_URI \.rm$ no-gzip dont-vary
## 第 2 步:http://www.mydigitallife.info/how-to-enable-mod_deflate-gzip-compression-on-cpanel-web-hosts /
设置输出过滤器放气 删除输出过滤器 mp3 # 不要压缩已经压缩的文件 SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip 不变 SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip 不变 SetEnvIfNoCase Request_URI .(?:avi|mov|mp3|mp4|rm|flv|swf|mp?g)$ no-gzip 不变化 SetEnvIfNoCase Request_URI .pdf$ no-gzip dont-vary RemoveOutputFilter mp3
# 以“.mp3”结尾的文件
; SetEnv no-gzip 1 RewriteRule \.mp3$ - [NS,E=no-gzip:1,E=dont-vary:1]
RewriteRule ^((.*)\.mp3)$ $1.mp3 [NS,E=no-gzip:1,E=dont-vary:1]
唯一一个可以正常工作并禁用 gzip 压缩的规则,但它是全局:
RewriteRule ^(.* )$ $1 [NS,E=无 gzip:1,E=不变化:1]
不使用此 RewriteRule 时 mp3 文件的响应标头:http://pastebin.com/ AkUZ6m5Y
使用此 RewriteRule 时 mp3 文件的响应标头: http://pastebin.com/b8j3NF6D
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 \.mp3gt; 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我必须禁用 odp 文件的压缩以供外部插件使用
刚刚在 .htaccess 文件中添加了以下规则
并且服务器禁用了 odp 文件的压缩
测试前请确保清除浏览器缓存
I had to disable compression for odp files for use by external plugin
Just added the following rule in .htaccess file
And the server disabled compression for odp files
Make sure to clear the browser cache before testing
您是否没有通过使用指令
SetOutputFilter DEFLATE
来解决这个问题,然后尝试对已经包含某种形式的压缩的流禁用此功能?使用此指令然后压缩可压缩的流不是更容易不吗?例如,可能会添加一个 Vary 标头:
好吧,这可能会错过您没有想到的奇怪类型,但它将实现 99+% 的压缩潜力。
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.And possibly adding a Vary header:
OK this may miss the odd type that you've not thought of, but it will achieve 99+% of your compression potential.
要仅对我的 Apache 服务器上的 Adobe Flash Player 文件 (SWF) 禁用 gzip 压缩,我将其添加到我的 .htaccess 文件中:
如果您愿意,您也可以对其他文件类型禁用 gzip 压缩:
To disable gzip compression on just Adobe Flash Player files (SWFs) on my Apache server, I added this to my .htaccess file:
If you wanted to, you could disable gzip compression for other file types as well:
我认为您没有在媒体中使用压缩。您是否检查过您实际上正在压缩文件?该模块可以加载到内存中,但这并不意味着它会压缩文件。如果您的 .htaccess 仅具有重写规则,那么您可能没有压缩任何类型的内容。
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.
这似乎已经过时了: https://www.varnish-cache.org/docs/3.0/tutorial/compression.html#gzip-and-esi" rel="nofollow noreferrer"> varnish-cache.org/docs/3.0/tutorial/compression.html#gzip-and-esi
GZIP 和 ESI
this seems outdated : https://www.varnish-cache.org/docs/3.0/tutorial/compression.html#gzip-and-esi
GZIP and ESI
我知道这个线程很旧,但我也经历过同样的道路。
我做过两件事。
我完全启用了文件夹的 .htaccess 并禁用了 gzip。
设置 reqwrite 规则以禁用
这两个规则都对我有用,我建议在论坛上搜索之前先查看 Apache 文档。
欲了解更多信息,请访问 Apache 网站。
https://httpd.apache.org/docs/2.4/mod/mod_deflate。 html
https://httpd.apache.org/docs/2.4/mod/ mod_mime.html#addtype
I know this thread is old, but I have gone through the same path.
Two things I have done.
I enabled .htaccess and disabled gzip for a folder completely.
put a reqwrite rule to disable
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