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):

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

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.
Share on email
Email
Share on facebook
Facebook
Share on google
Google
Share on twitter
Twitter

Responses (2)

  1. Marcos
    January 25, 2022 at 4:36 am · Reply

    Hi!
    My configuration have that line but nginx still ignore any site enabled!
    What can i do now?

    Thanks!

    • Richard
      January 25, 2022 at 1:43 pm ·

      Hi Marcos,

      Hard to tell – NGINX setup is finicky, there could be a lot of things that are causing the issue on your side. One quick thing, though – did you restart NGINX after you edited the config and added the include line?

Leave a reply

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


The reCAPTCHA verification period has expired. Please reload the page.