Compressing JSON Nginx Responses

03 Sep 2013

Simply including gzip on; in your Nginx configuration only compresses responses with the MIME type text/html. If Nginx is returning JSON data with a MIME type application/json, you can enable compression for these responses by doing:

http {
  ...
  gzip             on;
  gzip_comp_level  9;
  gzip_types       application/json;
  ...
}

This is especially important for mobile clients that may download such responses over slow networks.

Afterward, don’t forget to actually examine the HTTP response to ensure that Nginx is compressing its body. If you don’t have access to its raw form, inspect the HTTP headers of the response instead. You should should find the following two lines:

Content-Type: application/json
Content-Encoding: gzip

For more information, see the gzip module documentation.

comments powered by Disqus