【发布时间】:2015-08-04 12:30:39
【问题描述】:
如何在 Openshift 上禁用 gzip 压缩(针对特定请求)?更具体:对于 php 提供的文件下载 - 即 fpassthrough。
我尝试了几件事:
-
ini_set('zlib.output_compression', 'Off');在 php 文件中 -
apache_setenv('no-gzip', '1');和apache_setenv('dont-vary', '1');在 php 文件中 -
SetEnv no-gzip dont-vary在 .htaccess 文件中
仍然是通过curl -v -H 'Accept-Encoding: gzip,deflate' http://downloadtest-***.rhcloud.com 进行的简单测试:
> GET http://downloadtest-***.rhcloud.com/ HTTP/1.1
> User-Agent: curl/7.41.0
> Host: downloadtest-***.rhcloud.com
> Accept: */*
> Proxy-Connection: Keep-Alive
> Accept-Encoding: gzip,deflate
>
< HTTP/1.1 200 OK
< Date: Fri, 22 May 2015 12:47:41 GMT
< Server: Apache/2.2.15 (Red Hat)
< Content-Disposition: attachment; filename="myfile.tmp"
< Content-Lenght: 54
< Content-Type: application/octet-stream
< Vary: Accept-Encoding
< Content-Length: 77
< Proxy-Connection: Keep-Alive
< Connection: Keep-Alive
< Content-Encoding: gzip
背景: 我需要禁用 gzip 压缩,因为它会损坏已经 gzip 压缩的文件 - 参见例如https://magento.stackexchange.com/questions/3528/downloadable-zip-files-are-corrupt 或 http://www.heath-whyte.info/david/computers/corrupted-zip-file-downloads-with-php。
完整的测试代码:
index.php:
$file = __DIR__ . '/test.txt.gz'; //This file is acutally 54 bytes
ini_set('zlib.output_compression', 'Off');
apache_setenv('no-gzip', '1');
apache_setenv('dont-vary', '1');
header('Content-type: application/octet-stream');
header('Content-Disposition: attachment; filename="myfile.tmp"');
header('Content-Lenght: '.filesize($file));
if(!file_exists($file)) {
echo "file does not exist!\n";
exit(0);
}
$handle = fopen($file, 'rb');
fpassthru($handle);
fclose($handle);
exit(0);
.htaccess:
RewriteEngine On
SetEnv no-gzip dont-vary
【问题讨论】:
标签: php apache gzip openshift openshift-php-cartidges