Question

Error while creating subdomain droplet for a Node.js app

Posted February 14, 2020 317 views
Nginx

Hi,

I’m currently creating a new droplet where a Node.js app will live on a subdomain. I setup the droplet using Ubuntu 18.04, installed Nginx and while configuring the subdomain in the “/etc/nginx/sites-available/sub.example.com” and received an error below when running the command “sudo nginx -t”:

nginx: [emerg] unknown directive “listen[::]:80” in /etc/nginx/sites-enabled/example.com:3
nginx: configuration file /etc/nginx/nginx.conf test failed

I’m not sure why I’m receiving this error, this is my first time building a droplet and am a front-end developer who knows very little about back-end processes.

Also, when I found a forum talking about this error, found and ran a command to list off all sites enables in nginx and this is what I have:

/etc/nginx/sites-enabled/sub.example.com:        listen 80;
/etc/nginx/sites-enabled/sub.example.com:        listen [::]:80;
/etc/nginx/sites-enabled/default:   listen 80 default_server;
/etc/nginx/sites-enabled/default:   listen [::]:80 default_server;
/etc/nginx/sites-enabled/default:   # listen 443 ssl default_server;
/etc/nginx/sites-enabled/default:   # listen [::]:443 ssl default_server;
/etc/nginx/sites-enabled/default:#  listen 80;
/etc/nginx/sites-enabled/default:#  listen [::]:80;
/etc/nginx/sites-enabled/example.com:   listen 80;
/etc/nginx/sites-enabled/example.com:   listen[::]:80;

Hope this helps, I’m 110% lost on what to do and how to proceed so any helpful tips and suggestions is greatly appreciated :) Thanks!

edited by bobbyiliev

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
1 answer

Hello,

Can you please share the exact Nginx Server Block that you are using? I could then try to advise you further.

But usually what I would use as a Nginx Server Block is something like this:

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www/html;
        index index.html index.htm index.nginx-debian.html;

        server_name your_sub_domain_here;

        location / {
                try_files $uri $uri/ =404;
        }
}

Regards,
Bobby

  • Hello Bobby,

    Thank you so much for taking the time to help out :)

    As for the Nginx Server Block, here’s what I have

    server {
            listen 80;
            listen [::]:80;
    
            root /var/www/sub.example.com/html;
            index index.html index.htm index.nginx-debian.html;
    
            server_name sub.example.com www.sub.example.com;
    
            location / {
                    try_files $uri $uri/ =404;
            }
    }
    

    Noticed based off of your example snippet that I’m missing the default_server piece, is that needed?

    I also accidentally went through the Nginx steps for setting up server blocks with the main domain (example.com) and went back to delete the contents inside of /ect/nginx/sites-available/example.com and that removed the last two items in the /sites-enabled list from above when using the command grep -r listen /etc/nginx/sites-enabled/*. Below is what I have listing now:

    /etc/nginx/sites-enabled/sub.example.com:        listen 80;
    /etc/nginx/sites-enabled/sub.example.com:        listen [::]:80;
    /etc/nginx/sites-enabled/default:   listen 80 default_server;
    /etc/nginx/sites-enabled/default:   listen [::]:80 default_server;
    /etc/nginx/sites-enabled/default:   # listen 443 ssl default_server;
    /etc/nginx/sites-enabled/default:   # listen [::]:443 ssl default_server;
    /etc/nginx/sites-enabled/default:#  listen 80;
    /etc/nginx/sites-enabled/default:#  listen [::]:80;
    

    Hope this helps, thank you again for taking the time to reply - greatly appreciate it

    Best regards,
    Alysia

    • Hi there @alysiap,

      I was able to replicate the error, if you do not add a space after the listen directive you would get that exact same error:

      # sudo nginx -t
      
      nginx: [emerg] unknown directive "listen[::]:80" in /etc/nginx/sites-enabled/mysite.com:3
      

      So the problem that you are seeing indicates that you just need to add a space between the listen and the [::]:80 part.

      I had to change this line:

              listen[::]:80;
      
      

      To:

              listen [::]:80;
      

      Hope that this helps!

      Regards,
      bobby

Submit an Answer