简体   繁体   English

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

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

I have two files named test.html and test.php on my server, having the same content: 我的服务器上有两个名为test.htmltest.php文件,它们具有相同的内容:

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

  TEXT

</body>
</html>

Besides that I have a .htaccess -files with the following content: 除此之外,我还有一个.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>

Requesting test.html returns the following headers: 请求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

After uncommenting the rewrite part (thus using HTML without beeing processed by PHP), the response headers are: 取消注释重写部分(因此使用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

It's exactly the same content and exactly the same header, but why is the content not compressed, when it is returned by PHP? 它的内容和标题完全相同,但是当PHP返回内容时,为什么不压缩内容呢?

Add the following line in your .htaccess 在您的.htaccess中添加以下行

php_value zlib.output_compression 1

to enable PHP output compression. 启用PHP输出压缩。

More information at http://php.net/manual/en/zlib.configuration.php#ini.zlib.output-compression 有关更多信息,请参见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