【问题标题】:gzip file is same as file sizegzip 文件与文件大小相同
【发布时间】:2012-12-14 03:40:30
【问题描述】:
file    size    gzip size     file
js      217.4K  217.4K        js/scripts-0004.min.js
css     53.3K   53.3K         css/style-0004.min.css

为什么gzip 返回相同大小的文件?

他们是否需要在ubuntu 12.04 + python + nginx server 上为gzip 提供额外的软件包。

nginx.conf

http {

##
# Gzip Settings
##

gzip on;
gzip_disable "msie6";

gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

}

>>> r = urllib2.urlopen(urllib2.Request('/style-0004.min.css', ... headers={'Accept-Encoding': 'gzip;q=1.0, *;q=0'})) 
>>> print r.info() 
Server: nginx/1.1.19 Date: Thu, 13 Dec 2012 11:21:53 GMT 
Content-Type: text/css 
Transfer-Encoding: chunked Connection: close Vary: Accept-Encoding 
Expires: Thu, 31 Dec 2037 23:55:55 GMT 
X-UA-Compatible: IE=Edge,chrome=1 
Content-Encoding: gzip 
>>> s = r.read() 
>>> print("comressed size: %d" % len(s)) 
comressed size: 12231 
>>> f = gzip.GzipFile(fileobj=BytesIO(s)) 
>>> print("uncompressed: %d" % len(f.read())) 
uncompressed: 53337

js file. comressed size: 63975 uncompressed: 217473

【问题讨论】:

  • 您为什么认为这些结果是准确的?他们来自哪里?
  • 你有没有禁用压缩:docs.python.org/2/library/gzip.html
  • @IgnacioVazquez-Abrams 我使用 YSlow 插件检查 gzip 文件的大小。来自我服务器的文件没有被压缩,因为来自 google 和 facebook 的其他文件已经减小了大小。
  • 什么是r = urllib2.urlopen(urllib2.Request('http://nginxhost/js/scripts-0004.min.js', headers={'Accept-Encoding': 'gzip;q=1.0, *;q=0'})); print r.info(), len(r.read())?见example
  • 不要将信息放入 cmets。 edit 你的问题。检查您的浏览器发送/接收的标头,例如,您可以使用 Wireshark。

标签: python ubuntu nginx gzip


【解决方案1】:

这可能意味着文件存储在未压缩 (compresslevel == 0) 的 gzip 存档中,或者您可能使用了存储原始(未压缩)数据大小的 gzipfile.size 属性。

【讨论】:

  • 如果客户端浏览器支持接受 gzip 压缩的内容,文件将由 nginx 按需压缩。压缩级别为 6。检查有问题的 nginx.conf
【解决方案2】:

我当前的浏览器有问题。 Ubuntu 12.04 的铬。 我尝试在 mozilla firefox 上检查 gzipped 文件,我收到了压缩文件。

但我不知道为什么这个浏览器会发生这种情况,因为它会从其他域(如 facebook、google)获取压缩文件。而不是我的!

【讨论】:

  • 您可以比较 chromium 和 firefox 发送的 http 标头。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多