How do setup gzip & leverage browser caching properly

March 5, 2014 16k views
Running Ubuntu 12.0.4 Nginx Is there anything else recommended for speeding sites up?
6 Answers
you can make sure Nginx.conf has the following:

gzip on;
gzip_vary on;
gzip_disable "MSIE [1-6]\.";
gzip_proxied any;
gzip_http_version 1.1;
gzip_min_length 0;
gzip_comp_level 4;
gzip_buffers 16 8k;

and you can use x-cache to cache php mainly...

you can use some nginx modules to compress or combine css and js files.

you can compress images as well through many ways to speed up the site load time
Thank you! Do you know the steps required to leverage caching?
you just need to add this to your nginx vhost block:

location ~* \.(js|css|png|jpg|jpeg|gif|ico)(\?ver=[0-9.]+)?$ {
expires 1y;

or add this for more caching to other things you have on your site:

location ~* ^.+\.(jpg|jpeg|gif|png|svg|js|css|mp3|ogg|mpeg|avi|zip|gz|bz2?|
rar|swf)$ {
expires 7d;
location ~* \.(js|css|png|jpg|jpeg|gif|ico)(\?ver=[0-9.]+)?$ { 
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ { 
would probably be a better option because you surely don't want ALL CSS files cached for a year in case you want to make changes without a way to force the browser to reload the file (which would be adding a version string).

Can someone tell me where this goes in nginx.conf? I'm having trouble getting this to work.

  • It needs to be inside of a server block. Usually that will be in /etc/nginx/site-enabled/default not nginx.conf

Have another answer? Share your knowledge.