IT技术博客大学习 共学习 共进步
全部 移动开发 后端 数据库 AI 算法 安全 DevOps 前端 设计 开发者

关于一个gzip压缩问题的定位解决

Vimer 2010-09-11 09:48:13 累计浏览 3,036 次
本机暂存

    今天在进行CGI外网部署的时候,出现一个很奇怪的问题。

    先说明一下背景:

    在webserver上的一个CGI-A会通过libcurl来访问另一个webserver上的CGI-B,并将调用的结果返回给前台。

    问题的表现:

    前台看到是CGI-A超时,抓包发现在调用CGI-B的时候,http请求正常返回,但是时延达到1s左右。

    但是当在浏览器直接输入CGI-B的链接的时候,就在40ms之内即可返回。

    具体抓包如下:

    命令:

sudo tcpdump -s 0 -nX host [ip] -i eth1 -w data

    libcurl调用CGI-B:

    http://www.vimer.cn/wp-content/uploads/2010/09/data_err

    浏览器直接访问CGI-B:

    http://www.vimer.cn/wp-content/uploads/2010/09/data_suc

    在wireshark中仔细的对比了请求包,发现只有两个地方不同:

     libcurl调用CGI-B:

    1

    浏览器直接访问CGI-B:

    2

    但是当时怎么也没有想到居然真的就出在了gzip的问题上。

    后来总监过来看了一眼,注意,就一眼!(果然姜还是老的辣啊),就说,是不是gzip的问题啊。然后杯具的想起来CGI-B所在的webserver是默认打开gzip压缩的。

    于是关掉gzip压缩,测试,果然OK了!

    果然,是libcurl默认是不认识gzip压缩的!而这也解释了为什么在浏览器中直接输入CGI-B的url可以正常访问了~

    为了根查,特意看了一下libcurl的文档,发现果然如果要libcurl支持gzip压缩是需要设置的:

curl_easy_setopt(curl, CURLOPT_ENCODING, "gzip");            // Accept-Encoding

    说明如下:

CURLOPT_ENCODING 

    Sets the contents of the Accept-Encoding: header sent in an HTTP request, and enables decoding of a response when a Content-Encoding: header is received. Three encodings are supported: identity, which does nothing, deflate which requests the server to compress its response using the zlib algorithm, and gzip which requests the gzip algorithm. If a zero-length string is set, then an Accept-Encoding: header containing all supported encodings is sent. 

    This is a request, not an order; the server may or may not do it. This option must be set (to any non-NULL value) or else any unsolicited encoding done by the server is ignored. See the special file lib/README.encoding for details.

    参考链接:

    http://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTENCODING

    http://blog.chinaunix.net/u2/61797/showart_520044.html

    杯具啊,查了一下午,搞到最后我都要去直接strace webserver的接入进程了……,结果却是这么简单的原因。。。2010年9月11日

同分类推荐文章

  1. 等了十年的 Go 链式管道,终于来了:seq 让你像写 Scala 一样写 Go (2026-06-25 18:38:18)
  2. Go 实验特性详解 (2026-06-21 10:05:27)
  3. amd64 微架构级别对 Go 程序性能提升多少? (2026-06-21 09:38:49)

查看更多 后端 文章 →

建议继续学习

  1. 浅析http协议、cookies和session机制、浏览器缓存 (累计阅读 17,446)
  2. 从输入 URL 到页面加载完成的过程中都发生了什么事情? (累计阅读 15,933)
  3. libcurl的使用总结(二) (累计阅读 15,083)
  4. 使用python爬虫抓站的一些技巧总结:进阶篇 (累计阅读 13,302)
  5. HTTP协议Keep-Alive模式详解 (累计阅读 12,104)
  6. 你必须了解的Session的本质 (累计阅读 11,441)
  7. curl 命令使用cookie (累计阅读 10,016)
  8. Hello! 404 (累计阅读 9,385)
  9. 解决 nginx 反向代理网页首尾出现神秘字符的问题 (累计阅读 9,096)
  10. 解决IE6从Nginx服务器下载图片不Cache的Bug (累计阅读 8,357)