【问题标题】:NGINX and PHP Gzip compression not working in browser but works in cURLNGINX 和 PHP Gzip 压缩在浏览器中不起作用,但在 cURL 中起作用
【发布时间】:2016-01-15 05:59:25
【问题描述】:

在我的 NGinx 和 PHP 设置中发生了一个特殊问题。

我的测试网址:http://104.194.26.13:2002/a.php

我正在使用带有 NGinx (FCGI) 的 PHP。压缩我正在使用的数据:

<?php

ob_start('ob_gzhandler');

phpinfo();

?>

通过浏览器访问时显示:

Vary: Accept-Encoding

但是没有Content-Encoding,Firebug中显示的下载数据大小是非压缩数据的大小。

使用 curl 从 CLI 访问时:

curl -H "Accept-Encoding: gzip" "http://104.194.26.13:2002/a.php"

有一些 gbiresh 字符表明它确实进行了编码。如果您使用上述命令保存输出,则大小为 17.5 KB,而不是通过浏览器访问时的 75 KB。

这是从我的 a.php 文件收到的完整标题:

Connection: keep-alive
Content-Length: 75550
Content-Type: text/html
Date: Fri, 15 Jan 2016 05:37:43 GMT
Server: nginx
Vary: Accept-Encoding
X-Powered-By: PHP/5.5.27

可能有什么问题?

【问题讨论】:

  • 为什么你在php 端而不是nginx 配置中处理压缩?
  • 对于我的应用程序,我不能在 nginx 层使用 gzip。

标签: php nginx compression gzip


【解决方案1】:

您使用的是哪个浏览器,因为 Chrome 47.0.2526.106 m 显示如下:

现在,正如 cmets 中所述,您为什么不尝试直接从 Nginx 压缩它呢?通过在 nginx 配置中利用 gzip 压缩:

# enable gzip compression
gzip on;
gzip_min_length  1100;
gzip_buffers  4 32k;
gzip_types    text/plain application/x-javascript text/xml text/css;
gzip_vary on;
# end gzip configuration

Source


这是我的请求标头:

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Host:104.194.26.13:2002
Referer:http://stackoverflow.com/questions/34805168/nginx-and-php-gzip-compression-not-working-in-browser-but-works-in-curl/34805312?noredirect=1
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36

【讨论】:

  • 我正在使用带有 Firebug 的 Firefox 43.0.4。此时我无法在我的应用程序中使用 nginx gzip。奇怪的是,即使在我的 chrome 中,它也没有显示你的结果:Connection:keep-alive Content-Length:75821 Content-Type:text/html Date:Fri, 15 Jan 2016 06:31:44 GMT Server:nginx Vary:Accept-Encoding X-Powered-By:PHP/5.5.27
  • 另外,giftofspeed.com/gzip-test 处的实用程序也确认它不起作用。
  • Hrm 这很奇怪,我得到完全相同的结果 Firefox 41.0.1,ps,你有 zlib 扩展加载吗?正如ob_gzhandler 所要求的那样。
  • 是的。它已加载。如果您看到文件的输出,它是一个 phpinfo() 转储。它显示 zlib 已加载并注册为流。
  • 这太奇怪了,你为什么不试试nginx的配置?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-19
  • 1970-01-01
  • 1970-01-01
  • 2022-01-20
  • 2012-08-09
  • 1970-01-01
相关资源
最近更新 更多