Using Gzip for REST/JSON:API responsesfor Drupal 11 , 10 , 9 , and 8

Last updated :  

Everything else in this section reduces how much data gets computed and included. Compression is the last step: shrinking whatever's actually left before it travels over the network at all — a JSON response, being highly repetitive text (the same field names repeated across every item in a collection), compresses exceptionally well, often 70-80% smaller over the wire.

What you'll learn in this lesson

  • Why compression is a web-server concern, same as HTTP/2 earlier in this tutorial
  • Enabling gzip for API responses specifically
  • Why compression and page caching (Varnish) need to cooperate, not conflict

Enabling gzip: web-server, not Drupal

gzip on;
gzip_types application/vnd.api+json application/json text/html;
gzip_min_length 256;
gzip_comp_level 6;

Like HTTP/2, this is configured at the web-server layer, not inside Drupal — gzip_types needs to explicitly include JSON:API's actual content type (application/vnd.api+json), which is easy to forget since it's not the same as plain application/json.

The one gotcha worth knowing: gzip_min_length exists because compressing a very small response can actually make it larger once compression headers and overhead are accounted for — the default 256-byte-ish threshold is a reasonable floor. Don't set it to 0 expecting "more compression is always better"; tiny responses genuinely don't benefit.

Compression and Varnish: order matters

If you're running Varnish (from the Server-Side Optimization section) in front of your application, compression can happen at either layer — but doing it at your web server (behind Varnish) rather than in Varnish itself is usually simpler, since Varnish then just caches and re-serves the already-compressed response without needing its own gzip configuration. Verify which layer is actually doing the compressing with response headers rather than assuming.

Verifying it's actually working

curl -H "Accept-Encoding: gzip" -I https://example.com/jsonapi/node/article | grep -i content-encoding
# Should show: content-encoding: gzip

Quick check: your nginx config has gzip_types application/json; but JSON:API responses still aren't being compressed. What's the likely cause? If you said the actual content type is application/vnd.api+json, not plain application/json, and it's missing from gzip_types, you've spotted exactly the mismatch this lesson warned about.

Key takeaways

  • Compression is configured at the web-server layer — Drupal has no gzip setting of its own to enable.
  • JSON:API's real content type is application/vnd.api+json — make sure it's explicitly listed in your server's compressible types.
  • Compressing behind Varnish (at the web server) is usually simpler than compressing in Varnish itself.
  • Verify compression is actually active with real response headers, not just by assuming the config is correct.

Coming up next

You've now covered the complete API-performance toolkit — caching, query shaping, sparse fieldsets, and compression. Next, we turn back to configuration itself, and how to keep it lean.