【问题标题】:Can't tell if gzip is on无法判断 gzip 是否开启
【发布时间】:2014-05-07 19:46:20
【问题描述】:

我是一名前端开发人员,我正在网站上进行性能测试。当我运行 YSlow 时,它说我应该使用 gzip 压缩组件。

后端人员说压缩已打开,但是当我再次运行 YSlow 时,它仍然显示“F:使用 gzip 压缩组件”。当我检查元素(在 Chrome 上)并查看“网络”中的标题时,它说:

Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
...
Server:nginx/1.4.6 (Ubuntu)

如何判断文件是否被 gzip 压缩?

【问题讨论】:

    标签: http browser request gzip frontend


    【解决方案1】:

    如果浏览器发送Accept-Encoding 标头它告诉服务器:“好的,你可以给我发送gzip”。如果服务器配置为发送 gzip - 发送它。浏览器从 2000-2001 年开始支持 gzip,所以现在每个浏览器都支持 gzip。你只需要配置你的网络服务器。

    如果您想在Apache 网络服务器上配置gzip,请将以下代码添加到.htaccess 文件中:

    <ifModule mod_gzip.c>
    mod_gzip_on Yes
    mod_gzip_dechunk Yes
    mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
    mod_gzip_item_include handler ^cgi-script$
    mod_gzip_item_include mime ^text/.*
    mod_gzip_item_include mime ^application/x-javascript.*
    mod_gzip_item_exclude mime ^image/.*
    mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
    </ifModule>
    

    如果你使用nginx作为网络静态服务器,添加配置文件:

    gzip on;
    gzip_comp_level 2;
    gzip_http_version 1.0;
    gzip_proxied any;
    gzip_min_length 1100;
    gzip_buffers 16 8k;
    gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    
    # Disable for IE < 6 because there are some known problems
    gzip_disable "MSIE [1-6].(?!.*SV1)";
    
    # Add a vary header for downstream proxies to avoid sending cached gzipped files to IE6
    gzip_vary on; 
    

    P.S.:完整浏览器gzip 支持:

    • Netscape 6+(Netscape 4-5 可以,但有一些错误)。
    • Internet Explorer 5.5+(2000 年 7 月)和 IE 4(如果设置为 HTTP/1.1)。
    • Opera 5+(2000 年 6 月)
    • Lynx 2.6+(1999 年之前的某个时间)
    • Firefox 0.9.5+(2001 年 10 月)
    • Chrome 永远
    • Safari 永远

    注意:恕我直言 gipz 很有帮助,然后请求正文超过 3-5 kb 大小。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-05
      相关资源
      最近更新 更多