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.
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;
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.
Enjoyed this? Follow me on Mastodon or add the RSS, euh ATOM feed to your feed reader.
Dit artikel van murblog van Maarten Brouwers (murb) is in licentie gegeven volgens een Creative Commons Naamsvermelding 3.0 Nederland licentie .