Nginx : enabling gzip compression based on hostname

gzipnginx

I have a single machine running nginx serving http traffic for multiple domains(using same Lua codebase). For some of the domains I would like to enable gzip compression. Is it possible to enable gzip compression for specific set of domains? How?

Best Answer

Absolutely if you take a look at the documentation you can see that it is acceptable to place gzip in any of the following levels http, server, location, if in location and as the domain is defined at the server level using server_name we can place gzip along side it.

First check your nginx.conf file for gzip on; if it is present remove it.

You then need to create a conf file and define where you want to use gzip.

server {
  server_name www.myexample.com;
  listen 80;
  gzip off;
}

server {
  server_name www.myotherexample.com;
  listen 80;
  gzip on;
}