How to set up gzip compression for JSON responses in nginx?

Published on Aug. 22, 2023, 12:19 p.m.

To set up gzip compression for JSON responses in nginx, you can use the gzip module in your nginx configuration file. To enable gzip compression for JSON files, you can use the following configuration snippet:

gzip on;
gzip_types application/json;

In this example, gzip on; enables gzip compression on the server, and gzip_types application/json; specifies that JSON files should be compressed.

You can add this configuration snippet within the applicable http, server, or location block in your nginx configuration, depending on where you want the compression to be applied. For example, you could add it to the server block for a specific domain, or to the http block to enable gzip compression across all domains and locations.

Note that to use the gzip module, you need to ensure that it is included and installed on your nginx server. You can check whether the gzip module is included in your nginx build by running the command nginx -V.

Tags: