我正在尝试弄清楚为什么我的 JSON 响应没有被 gzip 压缩。在我的主ngninx.conf, 我有:
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_min_length 256;
gzip_types text/plain application/javascript application/x-javascript text/javascript text/xml text/css text/html application/json application/vnd.ms-fontobject application/x-font-ttf font/opentype image/svg+xml image/x-icon;
然后在我的脚本中我将打印出内容类型:
print $IN->header( 'application/json' );
转换为:
Content-Type: application/json \n\n
我在那里看到了正确的内容类型,它没有被压缩:
curl -H "Accept-Encoding: gzip" -I 'https://www.example.com/cgi-bin/links/spots_load_new.cgi?catid=9&linkid=0&t=luna'
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 07 Dec 2017 10:18:58 GMT
Content-Type: application/json
Connection: keep-alive
Keep-Alive: timeout=60
Access-Control-Allow-Origin: *
如果我在 JS 文件上测试它,它运行良好:
curl -H "Accept-Encoding: gzip" -I http://www.example.com/new_design/categories25.min.js
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 07 Dec 2017 10:23:54 GMT
Content-Type: application/javascript
Last-Modified: Wed, 27 Sep 2017 14:21:09 GMT
Connection: keep-alive
Keep-Alive: timeout=60
Vary: Accept-Encoding
ETag: W/"59cbb3d5-323e"
Expires: Thu, 31 Dec 2037 23:55:55 GMT
Cache-Control: max-age=315360000
Content-Encoding: gzip
我遗漏了什么?(我已经重新启动了 nginx,仅供参考:))
更新:为了使其尽可能简单 - 我创建了一个 test.json 文件(有效内容),但它仍然以非 gzip 压缩的形式返回:
curl -H "Accept-Encoding: gzip" -I https://www.example.com/new_design/test.json
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 07 Dec 2017 17:58:25 GMT
Content-Type: application/json
Content-Length: 245
Last-Modified: Thu, 07 Dec 2017 17:55:58 GMT
Connection: keep-alive
Keep-Alive: timeout=60
ETag: "5a2980ae-f5"
Accept-Ranges: bytes
答案1
啊天哪——我真不敢相信这有多愚蠢!!!
有问题的数据是245字节:
Content-Length: 245
...我有这个设置:
gzip_min_length 256k设置。我将其改为:
gzip_min_length 128;
现在它运行良好。啊!