Nginx Ignoring Server Blocks (Virtual Hosts) in Sites-Enabled

Nginx

Posted on May 7, 2016 in Development, Web

I’ve been setting up a web server for my latest project – a Node.js / Express app. All was going well – I spun up a droplet on DigitalOcean, installed NVM, Node, Git, MongoDB and deployed and configured my app. Then I decided to add Nginx into the stack to act as a reverse proxy and serve my static files, and I hit a roadblock. For some reason Nginx was not picking up the server blocks I configured in /etc/nginx/sites-available/ (and enabled in /etc/nginx/sites-enabled/). I knew Nginx was working as it was correctly serving the ‘Welcome to Nginx’ page, but regardless of what I tried or how I adjusted the virtual hosts files, it wouldn’t send the requests to my Node app. It was baffling.

I started going through all the different Nginx files and when I opened the /etc/nginx/nginx.conf file, I noticed that at the bottom it included this line:

include /etc/nginx/conf.d/*.conf;

That pushed me in the right direction and made me realize that the sites-enabled directory was not read by Nginx at all and as a result the server blocks were ignored. Simply adding

include /etc/nginx/sites-enabled/*;

as the last line in the nginx.conf file fixed the problem and the virtual hosts were finally being correctly recognized. Here’s what the entire nginx.conf file looks like (before any other configuration and tweaks):

I find it strange that the sites-enabled folder is not included by default since that is the location where you are supposed to configure your server blocks. But there you have it.

Enjoyed this post? Share it with others.
Email this to someoneShare on FacebookShare on Google+Tweet about this on Twitter

Leave a reply

Your email address will not be published. Required fields are marked *