This is a step by step guide on how to optimise Ghost and nginx. This guide is part of the Deploy Ghost series and is split into three parts - Deploying, Securing, and Optimising. This is part three and once we’re done, we will have nginx and our Ghost blog optimised to serve content quickly and reduce the server load in the process.
Before you start typing into your console, please take the time to research and understand what it is that you’re actually executing on your server. This process worked for me, and might change as the applications have new patches and versions rolled out.
04/01/2017: The nginx configuration for the location / {} block has been updated to include all of the headers. The original configuration was sending the root document without the correct security headers. The issue was caused by an added header preventing the location / {} block from inheriting the server {} headers.
Our Ghost blog is well and truly up and running. We could put the keyboard away now if we wanted to, but we really should look at fine tuning everything. This should help visitors get served faster, and reduce our server load.
You can get pretty crazy with optimising, so this post definitely doesn’t cover everything. You might consider trying to optimise your fonts, HTML, CSS, even your favicon. Users don’t tend to like waiting, speeding up your sites responsiveness even slightly could be the difference between a purchase or a negative review.
Utilise nginx
So it turns out that nginx is actually really good at serving static content and caching. When a request comes in, we can get nginx to respond instead of sending the request over to Ghost. This is great because we remove the burden on Ghost to generate everything each time a request comes in. This will free up server resources and also reduce the amount of time a client waits before getting a response.
Serve Static Content
Getting nginx to serve static content is as simple as telling it what to serve. To let nginx know what to serve, we want to edit our ghost.conf.
1
2
cd /etc/nginx/sites-available
nano ghost.conf
We’ll ask nginx to serve up our casper theme, and our images. If you ever change your theme, you will need to update nginx.
Getting nginx to cache content is a little more in depth as we need to create the area to hold the cache, configure the area to hold the cache, and then we can tell nginx what we want to be cached. First we need to create an area to hold the cache, we’ll call it ghostcache.
1
2
cd /var/cache/nginx
sudo mkdir ghostcache
Then we want to edit nginx.conf to configure our cache.
Now that we’ve made our cache and configured it, we can tell nginx what to cache. We’ll do this in ghost.conf.
1
2
cd /etc/nginx/sites-available
nano ghost.conf
This is what our root location will look like with our new cache directives added in. There’s some extra stuff in there too such as specifying the cache validity, ignoring some headers that Ghost uses, configuring nginx to serve from cache if there’s an issue, and adding in a header that shows if the cache was used. We’ll need to add in our other headers again, as add_header X-Cache-Status $upstream_cache_status; will prevent the location / {} block from inheriting the server {} headers. This could be done more cleanly by adding the headers into their own configuration file and including them where they’re needed, but this is easier to show.
We definitely don’t want to cache our Ghost administrator panel, so we will need to add in an exception for that. If the administrator panel is hit, just pass through to Ghost.
We’ve now got a functioning, secure, optimised blog. There’s always going to be more we can do to keep getting enhanced security and further optimisation, but this is a very solid start.