V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
Livid
V2EX  ›  NGINX

为 Nginx 的 JSON API 输出也打开 gzip 压缩

  •  
  •   Livid · 2012-01-30 14:01:46 +08:00 · 14750 次点击
    这是一个创建于 4460 天前的主题,其中的信息可能已经有所发展或是发生改变。
    Nginx 的 gzip 压缩默认只针对 text/html,如果你用 Nginx 输出 JSON(这应该是目前绝大部分移动应用消耗流量的地方),那么最好是为 JSON 格式也打开 gzip 输出:

    gzip on;
    gzip_min_length 1000;
    gzip_buffers 4 8k;
    gzip_types text/plain application/x-javascript text/css application/json application/xml text/javascript;

    在你的 gzip_types 里加入 application/json 即可,并且 API Server 源在设定 Content-Type 时也要注意。

    对于一些内容复杂的 JSON,gzip 还是非常有效的压缩手段。
    2 条回复    1970-01-01 08:00:00 +08:00
    clowwindy
        1
    clowwindy  
       2012-01-30 21:40:38 +08:00   ❤️ 1
    这也是nginx的好处之一,支持对fastcgi输出进行On the fly压缩
    lighttpd就只能压缩静态文件了
    catsky
        2
    catsky  
       2013-08-09 13:19:12 +08:00
    @clowwindy lighttpd不能压缩json吗? 那如果我要用lighttpd gzip传输json,该怎么做呢?
    自己实现gzip算法,在response的时候直接output 压缩过的json? 这样的话浏览器能自动解压缩吗?多谢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3537 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 11:41 · PVG 19:41 · LAX 04:41 · JFK 07:41
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.