Problem with defaults on NGINX
Be wary of default settings. They can cause confusing behaviour if you don’t fully understand the consequences, debugging can lead to wild goose chases and the solution is sometimes so trivial that the waste of time hurts.
Be wary of default settings. They can cause confusing behaviour if you don’t fully understand the consequences, debugging can lead to wild goose chases and the solution is sometimes so trivial that the waste of time hurts.
When I set this Ghost powered blog up initially within a Digital Ocean droplet, I hit a configuration speed bump. It was straightforward to get things up and running when accessing the main URL, GHOST_URL/, but when using the address with www
, things did not work so well. The page served was the standard Digital Ocean page that gives you the message that you have not finished configuration and should connect with the console and set up Ghost. This was a bit annoying as I thought it was a DNS problem for ages. Rather it was a problem with the NGINX configuration.
The server that hosts the blog is setup by default with two virtual web sites. A listing of the /var/www
shows the ghost
installation and a html
, the latter containing the digital ocean default page one and the former the actual ghost blog. When you hit the www.failedtofunction.com address it picks up the wrong one!
My first reaction was the following fix; just a quick hack to get it working. I simply inserted a HTML redirect into the header of the default page. I connected to the server using the console and edited the following file with vi: /var/www/html/index.html
I inserted the following line just inside the header tag to move the page to the url without the “www”:
<meta http-equiv="refresh" content="0; url=https://failedtofunction.com/" />
This solution is not satisfactory. The real fix is to get the configuration right.
A step was omitted when setting up NGINX. I should have removed the default configuration which is what is responsible for this redirection.
In simple terms, NGINX receives the requests and serves the appropriate page based on the url passed in. You can have different domains served by a single server by some simple configuration. Of course there’s a lot more that NGINX can do but I'm not going into that here - all I’m using it for is this interception and for help with SSL certificates.
The default settings within NGINX map calls on port 80 to a hosted site based on the folder, in this case the html
folder. The settings for my blog mapped from the domain name to the ghost
folder. Simple enough to fix.
NGINX has two folders for configuration. The /etc/nginx/sites-available
contains the actual configuration (or links to files elsewhere on the file system). Another folder, /etc/nginx/sites-enabled
contains links to the configuration files mentioned in the previous folders. To fix the issue just delete the default linked file - it’s original is still preserved in the sites-available
folder if needed in the future.
sudo rm /etc/nginx/sites-enable/default
Before the system picks up the changes you need a restart of the engine. That’s pretty quick with:
sudo service nginx reload
Because the redirect hack is pretty quick, it’s not easy to see if the fix worked. When you open up the web page in a browser it flicks quickly to the real site. An easier way to test it is using curl
which does not carry out the redirect.
curl https://www.failedtofunction.com
The output no longer shows the redirect code from the html
folder but instead the correct blog homepage.