I need to pre-compress some very large html/xml/json files (large data dumps) using either gzip or deflate. I never want to serve the files uncompressed. They are so large and repetitive that compression will probably work very very well, and while some older browsers cannot support decompression, my typical customers will not be using them (although it would be nice if I could generate some kind of 'hey you need to upgrade your browser' message)
我需要使用gzip或deflate预压缩一些非常大的html / xml / json文件(大数据转储)。我从不想要提供未压缩的文件。它们是如此庞大和重复,以至于压缩可能会非常好地工作,虽然一些旧的浏览器不能支持解压缩,但我的典型客户不会使用它们(尽管如果我能产生某种'嘿你需要的话会很好升级浏览器的消息)
I auto generate the files and I can easily generate .htaccess files to go along with each file type. Essentially what I want is some always on version of mod_gunzip. Because the files are large, and because I will be repeatedly serving them, I need a method that allows me to compress once, really well, on the command line.
我自动生成文件,我可以轻松生成.htaccess文件以与每种文件类型一起使用。基本上我想要的是一些总是在mod_gunzip版本上。因为文件很大,并且因为我将重复提供它们,所以我需要一种方法,允许我在命令行上压缩一次,非常好。
I have found some information on this site and others about how to do this with gzip, but I wondered if someone could step me through how to do this with deflate. Bonus points for a complete answer that includes what my .htaccess file should look like, as well as the command line code I should use (GNU/Linux) to obtain optimal compression. Super bonus points for an answer that also addresses how to send "sorry no file for you" message to un-compliant browsers.
我在这个网站上找到了一些关于如何用gzip做这个的信息,但我想知道是否有人可以指导我如何用deflate来做这件事。奖励指向一个完整的答案,包括我的.htaccess文件应该是什么样的,以及我应该使用的命令行代码(GNU / Linux)来获得最佳压缩。答案的超级奖励积分也解决了如何向不兼容的浏览器发送“抱歉没有文件”消息。
would be lovely if we could create a "precompression" tag to cover questions like this.
如果我们可以创建一个“预压缩”标签来涵盖这样的问题,那将是可爱的。
-FT
-FT
4 个解决方案
#1
8
Edit: Found AddEncoding in mod_mime
编辑:在mod_mime中找到AddEncoding
This works:
这有效:
<IfModule mod_mime.c>
<Files "*.html.gz">
ForceType text/html
</Files>
<Files "*.xml.gz">
ForceType application/xml
</Files>
<Files "*.js.gz">
ForceType application/javascript
</Files>
<Files "*.gz">
AddEncoding gzip .gz
</Files>
</IfModule>
The docs make it sound like only the AddEncoding
should be needed, but I didn't get that to work.
文档听起来似乎只需要AddEncoding,但我没有让它工作。
Also, Lighttpd's mod_compression can compress and cache (the compressed) files.
此外,Lighttpd的mod_compression可以压缩和缓存(压缩的)文件。
#2
3
If I were you, I would look at inbuilt filesystem compression instead of doing this at the apache layer.
如果我是你,我会看看内置的文件系统压缩而不是在apache层执行此操作。
On solaris zfs has transparent compression, use zfs compress to just compress the filesystem. Similarly, windows can compress folders, apache will serve the content oblivious to the fact it's compressed on disk. Linux has filesystems that do transparent compression also.
在solaris zfs上有透明压缩,使用zfs compress来压缩文件系统。同样,Windows可以压缩文件夹,apache将服务于内容,而不知道它在磁盘上压缩的事实。 Linux的文件系统也可以进行透明压缩。
#3
2
For the command line, compile zlib's zpipe: http://www.zlib.net/zpipe.c and then
对于命令行,编译zlib的zpipe:http://www.zlib.net/zpipe.c然后
zpipe < BIGfile.html > BIGfile.htmlz
for example.
例如。
Then using Zash's example, set up a filter to change the header. This should provide you with having RAW deflate files, which modern browsers probably support.
然后使用Zash的示例,设置一个过滤器来更改标题。这应该为您提供RAW deflate文件,现代浏览器可能支持这些文件。
For another way to compress files, take a look at using pigz with zlib (-z) or PKWare zip (-K) compression options. Test if these work coming through with Content-Encoding set.
有关压缩文件的另一种方法,请查看使用带有zlib(-z)或PKWare zip(-K)压缩选项的pigz。测试这些工作是否通过Content-Encoding设置完成。
#4
0
A quick way to compress content without dealing directly with moz_gzip/mod_defalte is using ob_gzhandler and modifying headers (before any output is send to the browser).
在不直接处理moz_gzip / mod_defalte的情况下压缩内容的一种快速方法是使用ob_gzhandler并修改标头(在任何输出发送到浏览器之前)。
<?php
/* Replace CHANGE_ME with the correct mime type of your large file.
i.e: application/json
*/
ob_start ('ob_gzhandler');
header('Content-type: CHANGE_ME; charset: UTF-8');
header('Cache-Control: must-revalidate');
$offset = 60 * 60 * 2 ;
$ExpStr = 'Expires: ' . gmdate('D, d M Y H:i:s',time() + $offset) . ' GMT';
header($ExpStr);
/* Stuff to generate your large files here */
#1
8
Edit: Found AddEncoding in mod_mime
编辑:在mod_mime中找到AddEncoding
This works:
这有效:
<IfModule mod_mime.c>
<Files "*.html.gz">
ForceType text/html
</Files>
<Files "*.xml.gz">
ForceType application/xml
</Files>
<Files "*.js.gz">
ForceType application/javascript
</Files>
<Files "*.gz">
AddEncoding gzip .gz
</Files>
</IfModule>
The docs make it sound like only the AddEncoding
should be needed, but I didn't get that to work.
文档听起来似乎只需要AddEncoding,但我没有让它工作。
Also, Lighttpd's mod_compression can compress and cache (the compressed) files.
此外,Lighttpd的mod_compression可以压缩和缓存(压缩的)文件。
#2
3
If I were you, I would look at inbuilt filesystem compression instead of doing this at the apache layer.
如果我是你,我会看看内置的文件系统压缩而不是在apache层执行此操作。
On solaris zfs has transparent compression, use zfs compress to just compress the filesystem. Similarly, windows can compress folders, apache will serve the content oblivious to the fact it's compressed on disk. Linux has filesystems that do transparent compression also.
在solaris zfs上有透明压缩,使用zfs compress来压缩文件系统。同样,Windows可以压缩文件夹,apache将服务于内容,而不知道它在磁盘上压缩的事实。 Linux的文件系统也可以进行透明压缩。
#3
2
For the command line, compile zlib's zpipe: http://www.zlib.net/zpipe.c and then
对于命令行,编译zlib的zpipe:http://www.zlib.net/zpipe.c然后
zpipe < BIGfile.html > BIGfile.htmlz
for example.
例如。
Then using Zash's example, set up a filter to change the header. This should provide you with having RAW deflate files, which modern browsers probably support.
然后使用Zash的示例,设置一个过滤器来更改标题。这应该为您提供RAW deflate文件,现代浏览器可能支持这些文件。
For another way to compress files, take a look at using pigz with zlib (-z) or PKWare zip (-K) compression options. Test if these work coming through with Content-Encoding set.
有关压缩文件的另一种方法,请查看使用带有zlib(-z)或PKWare zip(-K)压缩选项的pigz。测试这些工作是否通过Content-Encoding设置完成。
#4
0
A quick way to compress content without dealing directly with moz_gzip/mod_defalte is using ob_gzhandler and modifying headers (before any output is send to the browser).
在不直接处理moz_gzip / mod_defalte的情况下压缩内容的一种快速方法是使用ob_gzhandler并修改标头(在任何输出发送到浏览器之前)。
<?php
/* Replace CHANGE_ME with the correct mime type of your large file.
i.e: application/json
*/
ob_start ('ob_gzhandler');
header('Content-type: CHANGE_ME; charset: UTF-8');
header('Cache-Control: must-revalidate');
$offset = 60 * 60 * 2 ;
$ExpStr = 'Expires: ' . gmdate('D, d M Y H:i:s',time() + $offset) . ' GMT';
header($ExpStr);
/* Stuff to generate your large files here */