— Ghost, NGINX, JavaScript — 2 min read
I went to Google's PageSpeed Insights page the other day to check my score. I am currently using the default Casper theme for my Ghost blog. My baseline score for Mobile was 74/100 and on Desktop 82/100. These scores aren't bad but I know I could get it a lot higher. The goal, of course, is to end up with a perfect 100/100.
Later this month ( April 21, 2015 ), Google will be updating their search algorithm and promoting pages that have better performance especially on mobile devices. This is a good time to make the changes that we need to before they impact rankings.
Here are the items that are being tested:
There is some good news (for me at least). A couple of these are already optimized: Avoid landing page redirects and Minify HTML. That still gives us plenty to do. In this article, I am going to concentrate on what I can do on my server to help the score. In a follow up article, go over how I modified the default theme.
Off we go!
On my host, Nginx routes traffic to my Ghost blog. But, by default, the server configuration isn't as optimal as it could be. We are going to knock out two metrics: Leverage browser caching and Enable compression. To do this we will have to change Nginx's configuration files. For browser caching we need /etc/nginx/conf.d/default.conf
and /etc/nginx/nginx.conf
for gzip compression.
By enabling GZIP compression on the server, web browsers can download the files faster. This kind of compression results in a file size reduction of 50% - 70%. And that will give us an overall increased page speed.
On the server in the /etc/nginx
find the nginx.conf
and add the following code into the http{ }
area of the file. I put mine at the very top of this section and it looks like this:
1http {2 gzip on;3 gzip_comp_level 5;4 gzip_min_length 256;5 gzip_proxied any;6 gzip_vary on;7 gzip_types8 application/atom+xml9 application/javascript10 application/json11 application/ld+json12 application/manifest+json13 application/rdf+xml14 application/rss+xml15 application/schema+json16 application/vnd.geo+json17 application/vnd.ms-fontobject18 application/x-font-ttf19 application/x-javascript20 application/x-web-app-manifest+json21 application/xhtml+xml22 application/xml23 font/eot24 font/opentype25 image/bmp26 image/svg+xml27 image/vnd.microsoft.icon28 image/x-icon29 text/cache-manifest30 text/css31 text/javascript32 text/plain33 text/vcard34 text/vnd.rim.location.xloc35 text/vtt36 text/x-component37 text/x-cross-domain-policy38 text/xml;3940# -- REST OF THE FILE HERE!!
That should give us a good setup for the compression. Now let's set up some expires headers by modifying the default.conf
file. If you want you can restart Nginx ( see below for the command to execute on the server ). This will add these changes and verify that there are no typos, etc. Or you can just go ahead and move to the next section.
I'm not going to go into why this is a good thing here. Other smarter people can explain it much better than I can. Basically, the longer files are in cache the better the performance of the site. You can always bust the cache when you make a change. The default cache time is one hour which is terrible from a performance perspective. So I wanted to make mine last for a much longer time. For Ghost and Nginx, we change the default.conf
file located in the etx/nginx/conf.d/
folder.
Here is that file:
1server {2 listen 80;3 server_name YOURDOMAINNAMEHERE; #replace with your domain45 # Theme assets (CSS, Javascript)6 # Cache these assets for 30 days78 location /assets/ {9 expires 30d;10 add_header Pragma public;11 add_header Cache-Control "public";12 proxy_pass http://localhost:2368/assets/;13 proxy_set_header Host $host; proxy_buffering off;14 }151617 # jQuery (and other vendor files)18 # Cache these assets for 365 days!!1920 location /public/ {21 expires 365d;22 add_header Pragma public;23 add_header Cache-Control "public";24 proxy_pass http://localhost:2368/public/;25 proxy_set_header Host $host;26 proxy_buffering off;27 }282930 # Everything else3132 location / {33 proxy_pass http://localhost:2368/;34 proxy_set_header Host $host;35 proxy_buffering off;36 }37 }
Now, everything in our theme assets folder will stay in cache for thirty days. For those in the public folder (for jquery ), it will stay for 365 days!
We need to restart Nginx for these changes to go into effect.
1[sudo] service restart nginx
Once I rerun the PageSpeed tests, four our of our ten tests passed! Our score has gotten higher as well.
Now, we have taken care of the server configuration. We can start working on customizing the Casper theme. This will be where we will do the majority of our work.
I will do that in another post.
'Til next time!
-G