Edit config file
Enabling Gzip in Nginx is very easy, and will save us bandwidth transfer and also takes less time to load the pages.
Edit the file /etc/nginx/nginx.conf
and add the following code to the http section:
# enable gzip gzip on; gzip_min_length 1100; gzip_buffers 4 32k; gzip_types text/plain application/x-javascript text/xml text/css; gzip_vary on; |
- gzip on – turns on/off gzip compression
- gzip_min_length 1100 – the minimum file-size to be compressed
- gzip_buffers 4 32k – specify buffer size of gzip
- gzip_types text/plain application/x-javascript text/xml text/css – specify which file types should be compressed
- gzip_vary on – enables response header
Save the config file and reload Nginx:
/etc/init.d/nginx reload |
Testing Gzip
To test Gzip compression is working on Nginx run:
[root@atran72 ~]# curl --header "Accept-Encoding: gzip,deflate,sdch" -I https://www.anhsblog.com/blog HTTP/1.1 200 OK Server: nginx/1.4.3 Date: Sun, 23 Feb 2014 21:19:02 GMT Content-Type: text/html; charset=UTF-8 Connection: keep-alive Vary: Accept-Encoding X-Powered-By: PHP/5.3.3 X-Pingback: https://www.anhsblog.com/blog/xmlrpc.php Content-Encoding: gzip |
You should see ‘Content-Encoding: gzip
‘.