【问题标题】:nginx gzip not working on browser but curl worksnginx gzip 不能在浏览器上工作,但 curl 工作
【发布时间】:2013-07-03 07:55:53
【问题描述】:

我的环境是 nginx(1.3.11) + php-fpm

curl -I -H "Accept-Encoding: gzip,deflate" http://www.ihezhu.com/

结果是

HTTP/1.1 200 OK  
Server: nginx  
Date: Wed, 03 Jul 2013 07:47:27 GMT  
Content-Type: text/html; charset=utf-8  
Connection: keep-alive  
Vary: Accept-Encoding  
Set-Cookie: PHPSESSID=st7oa6mero58n6lmitlofa4n70; path=/  
Expires: Thu, 19 Nov 1981 08:52:00 GMT  
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0  
Pragma: no-cache  
Content-Encoding: gzip

但是当我使用像 chrome 这样的浏览器并且响应不包含 gzip
[有一种特殊情况:打开http://www.ihezhu.com/list/md_area-c_beijing/f_2000.3000_0_0_0_0_0_0_0.0.0.0.0_0_0-s_time_asc-lt_list-p_1/时,响应gzip T_T]

怎么了?

我的 nginx 设置是

gzip on;  
gzip_buffers 4 16k;  
gzip_comp_level 3;  
gzip_http_version 1.1;  
gzip_min_length 1k;  
gzip_proxied any;  
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;  
gzip_vary on;  
gzip_disable msie6;

谢谢

我的 nginx 编译选项,nginx virson 现在更新到 1.5.2

./configure \  
    --pid-path=/var/run/nginx.pid \  
    --lock-path=/var/lock/nginx.lock \  
    --user=nginx \  
    --group=nginx \  
    --with-http_realip_module \  
    --with-http_gzip_static_module \  
    --with-http_stub_status_module \  
    --with-pcre=/var/src/pcre-8.33 \  
    --with-zlib=/var/src/zlib-1.2.8 \  
    --http-client-body-temp-path=/var/tmp/nginx/client \  
    --http-proxy-temp-path=/var/tmp/nginx/proxy \  
    --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi \  
    --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \  
    --http-scgi-temp-path=/var/tmp/nginx/scgi

更新信息

urlhttp://www.ihezhu.com/listhttp://www.ihezhu.com/list/md_area-c_shanghai/f_0.0_0_0_0_0_0_0_0.0.0.0.0_0_0-s_time_asc-lt_list-p_1/的响应内容相同,但只有长的有gzip..

【问题讨论】:

  • 这里也一样。如果我从浏览器发出请求,我在 AWS Ubuntu 实例上的 Nginx (nginx/1.8.0) 没有设置 Content-Encoding: gzip 标头。但是如果我发出curl -H "Accept-Encoding: gzip" 请求,则可以设置标题。但似乎 Nginx gzip-s 响应虽然。我使用 Google 的 PageSpeed 进行测试,它没有抱怨没有 gzip。奇怪

标签: nginx gzip


【解决方案1】:
  • 您的curl 命令有效,因为它发送的是HEAD 请求,而不是GET 请求。尝试使用详细模式的 curl:

    curl -Iv -H "Accept-Encoding: gzip,deflate" http://www.ihezhu.com/

你会得到和浏览器一样的结果

curl -i -H "Accept-Encoding: gzip,deflate" http://www.ihezhu.com/
  • “text/html”总是被压缩的。所以它与gzip_types指令无关。

  • 当我的上游服务器使用 http 1.0 而不是 http 1.1 之前,这发生在我身上。您是否尝试过以下方法?

    gzip_http_version 1.0;

[更新] 您的 nginx 编译选项似乎很正常。很难理解 url 长度如何直接影响 gzip 上的 nginx。检查了nginx源代码。 url 上没有任何内容用于确定 gzip。根据源代码,有两种可能的原因:

  1. 您的 php 代码返回一个带有短 url 请求的非空 content-encoding 标头。
  2. 您的 php 代码返回错误的 content-length 标头和短 url 请求,并且该长度小于 1k。

所以最好的方法是找到两个url的php响应头,然后从那里开始。

【讨论】:

  • 改成gzip_http_version 1.0;不行=_=,[有一种特殊情况:打开ihezhu.com/list/md_area-c_beijing/…时,是响应gzip T_T]
  • 这很奇怪。您是否安装了一些 3rd 方 nginx 模块?如果是,也许删除这些模块,看看它是否有效。另外你是使用代理模块来服务器php-fpm还是直接使用fastcgi模块?
  • 我添加了有问题的 nginx 编译选项,我认为原因可能是 url,url ihezhu.com/list> 和 ihezhu.com/list/md_area-c_shanghai/…> 是相同的响应内容,但长的有压缩包..
猜你喜欢
  • 2015-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-02
  • 2020-07-10
  • 1970-01-01
  • 1970-01-01
  • 2018-07-26
相关资源
最近更新 更多