To get gzip configured for nginx, first make sure the flag is set when you compile, or that the binary you’ve installed was compiled with it on (seems to be the default). The flag is in the source at the path:
Set it as follows:
Once you’ve built nginx, you’ll need to turn gzip on in your nginx configuration, by default in nginx.conf.
There are some optional settings; we’ll get to those in a second. For now, let’s use curl with some request headers to test out our setup.
~$ curl --silent "http://localhost" -H "Accept-Encoding:gzip" -D headers.txt >> out.out && cat headers.txt | grep Content-Encoding Content-Encoding: gzip
Sure enough, if we curl localhost with the right Accept-Encoding header, we get a gzipped response with the accompanying Content-Encoding header as proof.
Let’s try the negative case. We’ll avoid sending the Accept-Encoding request header.
~$ curl --silent "http://localhost" -D headers.txt >> out.out && cat headers.txt | grep Content-Encoding ~$
Sure enough, no Content-Encoding response header to be found. Nginx is making sure it doesn’t send out gzipped content to clients that don’t accept it.
Nginx also provides an option to test the User Agent against a regular expression to further avoid gzipping content for clients known not to support it, but who may still send inappropriate accept headers. For example, to turn off gzipping for Internet Explorer 6, add the following to your nginx.conf.
Let’s try it out. Note you’ll need to restart nginx to have configuration changes applied.
~$ curl --silent "http://localhost" -H "Accept-Encoding:gzip" -A "msie" -D headers.txt >> out.out && cat headers.txt | grep Content-Encoding ~$
Ta da. Even thought Accept-Encoding:gzip was sent, the result was not gzipped, thanks to gzip_disable.
Learn more about nginx’s HttpGzipModule here.