Must do speed optimalizations on nginx

An article, posted more than 7 years ago filed in performance, nginx, server, configuration & cache.

Nginx’s default configuration needs a bit of extra configuration (at least on Debian systems) to enable gzip and client side caching. Two very quick wins for better performing web-apps.

Enabling gzip for more content types

Compression makes files smaller. By default only HTML is gzipped, but it it makes sense for quite a list of other file types too. This, however, excludes(!) images, which have their own methods of compression: compression over compression delivers you nothing, and costs you and your end-user a few more CPU cycles.

So find the gzip on; line in /etc/nginx/nginx.conf file (make sure it is not turned off or commented), and either uncomment the gzip_types-line or use this, more complete, line (including svg):

gzip_types image/svg+xml text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

Client side caching

Every browser has a cache of its own, but you should instruct your browser how long these files can be cached. It doesn’t help your first time users, but every subsequent request will be significantly faster.

You can override these cache settings per type in your site’s configuration, e.g.:

    location ~* \.(jpg|jpeg|png|gif|ico)$ {
            expires 365d;
    }

    location ~* \.(js|svg|css)$ {
            expires 99d;
    }

While I have reduced the expiry time of js, svg and css here, don’t worry too much about cache expiry when you’re deploying a rails app: rails automatically adds a checksum to each filename, so when its contents changed the name of the resource changes too.

Interested in how to deploy a rails app? Read my post A somewhat secure Debian server with nginx, Passenger, rbenv for hosting Ruby on Rails with mail support and deployment with Capistrano, and if you want to make it secure, earlier I’ve explained how to use Let’s encrypt.

Op de hoogte blijven?

Maandelijks maak ik een selectie artikelen en zorg ik voor wat extra context bij de meer technische stukken. Schrijf je hieronder in:

Mailfrequentie = 1x per maand. Je privacy wordt serieus genomen: de mailinglijst bestaat alleen op onze servers.