【问题标题】:Why does gzip compression not work for files processed by PHP?为什么 gzip 压缩不适用于 PHP 处理的文件?
【发布时间】:2016-11-03 14:26:16
【问题描述】:

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

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

  TEXT

</body>
</html>

除此之外,我还有一个 .htaccess-files,其内容如下:

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

内容一模一样,header一模一样,但是为什么PHP返回的时候内容没有被压缩?

【问题讨论】:

  • Apache 不会处理 PHP 输出压缩,因为这是 PHP 的责任。是否启用了 php 输出压缩? (php.net/manual/en/…)

标签: php html .htaccess compression gzip


【解决方案1】:

在您的 .htaccess

中添加以下行
php_value zlib.output_compression 1

启用 PHP 输出压缩。

更多信息http://php.net/manual/en/zlib.configuration.php#ini.zlib.output-compression

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-23
    • 2016-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多