GZIP Compression Not Working?

· 2 min read

Recently, a site was loading extremely slowly, and analysis revealed that one reason was that GZIP compression wasn’t enabled. However, I encountered issues during configuration, so I’m documenting this here.

Benefits of GZIP

  1. For text files like JS/HTML, compressed size is about 1/3 of the original, so GZIP should generally be enabled unless there are specific reasons not to.

  2. Note that Nginx doesn’t enable GZIP by default, and by default only compresses text/html. Caddy enables it by default.

Enabling GZIP

Using Nginx as an example, the following configuration is needed to enable compression for target file types:

gzip on;
gzip_types text/plain application/xml text/css application/javascript application/json text/javascript text/html;  # Enable compression for specified types

When I added this configuration to our service, I found it didn’t work. It turned out the changes weren’t applied to the gateway service.

Service Architecture

Let me explain the service architecture, simplified as follows. If GZIP is only configured on Service B, communication between Service A and Service B will use GZIP for faster transmission, but since Service A doesn’t have GZIP enabled, Service A will still deliver uncompressed content to the browser client. Therefore, Service A must also have GZIP enabled for it to work.

https://static.1991421.cn/2024/2024-12-14-230259.jpeg

When Service A also enables GZIP, actual testing shows that the browser receives compressed content. So does Service B still need to enable it? Personally, I think enabling it is still better since it speeds up communication between A and B, but enabling Service A is more critical.

Factors Determining GZIP Effectiveness

  1. The client request headers include accept-encoding: gzip, which browsers include by default.
  2. The server supports and has GZIP enabled.

Final Thoughts

While enabling GZIP is simple, it’s important to understand how it actually works.

Authors
Developer, digital product enthusiast, tinkerer, sharer, open source lover