繁体   English   中英

为什么gzip压缩不适用于PHP处理的文件?

[英]Why does gzip compression not work for files processed by PHP?

我的服务器上有两个名为test.htmltest.php文件,它们具有相同的内容:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8"/>
  <title>TEMPLATE</title>
</head>
<body>

  TEXT

</body>
</html>

除此之外,我还有一个.htaccess文件,内容如下:

RewriteEngine On
RewriteBase /
RewriteRule ^(.*)\.(php3|php4|php5|php6|html|htm) $1\.php

AddType x-mapp-php5.5 .php
AddHandler x-mapp-php5.5 .php

<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html
</Ifmodule>

请求test.html返回以下标头:

Connection: Keep-Alive
Content-Type: text/html
Date: Fri, 01 Jul 2016 08:32:04 GMT
Keep-Alive: timeout=2, max=200
Server: Apache
Transfer-Encoding: chunked
x-powered-by: PHP/5.5.36

取消注释重写部分(因此使用HTML而不进行PHP处理)之后,响应标头为:

Accept-Ranges: bytes
Connection: Keep-Alive
Content-Encoding: gzip
Content-Length: 186
Content-Type: text/html
Date: Fri, 01 Jul 2016 08:42:22 GMT
Etag: "xxxxxxxx-xx-xxxxxxxxxxxxx"
Keep-Alive: timeout=2, max=200
Last-Modified: Fri, 01 Jul 2016 08:42:03 GMT
Server: Apache
Vary: Accept-Encoding

它的内容和标题完全相同,但是当PHP返回内容时,为什么不压缩内容呢?

在您的.htaccess中添加以下行

php_value zlib.output_compression 1

启用PHP输出压缩。

有关更多信息,请参见http://php.net/manual/zh/zlib.configuration.php#ini.zlib.output-compression

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM