In my previous post, I wrote how to install Ghost and Nginx with SSL on Ubuntu 14.04. Now, I would like to share about how to speed up Ghost with tweaking some Nginx configurations.
Speed is a must. Nobody likes slow website. Search engine sometimes fails to crawl website because of website speed. So, how to choose server (provider, location, and type of server), determining to choose which CMS, and how to tweak server are several things we have to pay attention.
To tweak Nginx we only edit 2 files: first satudua.conf (or yourdomain.conf and nginx.conf.
Tweaking Nginx.conf to cache static content
All static content (js, css, image, etc) will be cached by nginx.
-
Open nginx.conf in directory /etc/nginx
-
Insert this code between http { }:
proxy_cache_path /etc/nginx/cache levels=1:2 keys_zone=STATIC:10m inactive=24h max_size=512m;
Don't forget to save it.
Create folder /cache inside /etc/nginx
`# cd /etc/nginx
mkdir cache
chown -R root:root cache`
Tweaking Nginx Vhost.conf
Open satudua.conf (or yourdomain.conf) from directory /etc/nginx/conf.d
Delete all contents and replace with this:
server {
server_name satudua.xyz www.satudua.xyz;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;
server_name www.satudua.xyz;
location /content/images {
alias /var/www/ghost/content/images;
}
location /assets {
alias /var/www/ghost/content/themes/casper/assets;
}
location /public {
alias /var/www/ghost/core/built/public;
}
location /ghost {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:2368;
}
location / {
proxy_cache STATIC;
proxy_cache_valid 200 60m;
proxy_ignore_headers X-Accel-Expires Expires Cache-Control;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:2368;
}
}
Now you can reboot server. After restarted server, type this command to start Ghost:
su -u ghost
pm2 start /var/www/ghost/index.js --name ghost
Note: for deleting cache, use this command:
rm -r /etc/nginx/cache/*
Finish, now your ghost will fly and ready to serve thousands of visitors.
Comments