如何对 .htaccess 文件中的某些媒体文件禁用 Apache gzip 压缩?

发布于 2025-01-04 23:24:38 字数 3423 浏览 5 评论 0原文

我想对某些已通过 .htaccess 文件在 Apache 服务器上压缩的媒体文件禁用 gzip 压缩。
原因:正如 jPlayer 网站 上所写,gzip 编码应该对媒体文件禁用:“媒体文件已被压缩,GZIP 只会浪费服务器上的 CPU。如果您对媒体进行 GZIP,Adobe Flash 插件将会遇到问题。”

问题是启用 gzip 时,Content-Length 标头未正确设置 - 因此在使用 SoundManager2 播放器,曲目的长度进度条无法正常工作(所以也许这就是他们在 jPlayer 网站上提到的问题)。

我可以在此处测试内容是否经过 gzip 压缩。
我确实在服务器上启用了 mod_deflatemod_mimemod_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 \.mp3
    gt;
    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

蒲公英的约定 2025-01-11 23:24:38

我必须禁用 odp 文件的压缩以供外部插件使用
刚刚在 .htaccess 文件中添加了以下规则

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

并且服务器禁用了 odp 文件的压缩
测试前请确保清除浏览器缓存

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

剑心龙吟 2025-01-11 23:24:38

您是否没有通过使用指令 SetOutputFilter DEFLATE 来解决这个问题,然后尝试对已经包含某种形式的压缩的流禁用此功能?使用此指令然后压缩可压缩的流不是更容易吗?例如

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

,可能会添加一个 Vary 标头:

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

好吧,这可能会错过您没有想到的奇怪类型,但它将实现 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.

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

春庭雪 2025-01-11 23:24:38

要仅对我的 Apache 服务器上的 Adob​​e Flash Player 文件 (SWF) 禁用 gzip 压缩,我将其添加到我的 .htaccess 文件中:

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

如果您愿意,您也可以对其他文件类型禁用 gzip 压缩:

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

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>
み零 2025-01-11 23:24:38

我认为您没有在媒体中使用压缩。您是否检查过您实际上正在压缩文件?该模块可以加载到内存中,但这并不意味着它会压缩文件。如果您的 .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.

辞旧 2025-01-11 23:24:38

这似乎已经过时了: 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

如果您使用 Edge Side Includes,您会很高兴注意到 ESI
和 GZIP 合作得非常好。 Varnish 会神奇地解压
进行 ESI 处理的内容,然后重新压缩以提高效率
存储和交付。

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.

春风十里 2025-01-11 23:24:38

我知道这个线程很旧,但我也经历过同样的道路。

我做过两件事。

  1. 我完全启用了文件夹的 .htaccess 并禁用了 gzip。

    <文件“*.gz.asc”>
         删除编码.gz
     
    
  2. 设置 reqwrite 规则以禁用

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

这两个规则都对我有用,我建议在论坛上搜索之前先查看 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.

  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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文