Report this

What is the reason for this report?

Multiple node apps for multiple subdomains

Posted on November 22, 2020

I want to host multiple node apps on single droplep using different domains.

I want to achieve something like this:

johndoe.com -> node app 1 (port 5000)
blog.johndoe.com -> node app 2 (port 5001)
statichtml.johndoe.com -> static html from defined path

Right now I have this kind of config in sites-available/default file.

server {
    root /var/www/html;

    index index.html index.htm index.nginx-debian.html;
    server_name www.johndoe.com johndoe.com; # managed by Certbot


    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        proxy_pass http://localhost:5000;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
    }


    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/johndoe.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/johndoe.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

server {
    if ($host = www.johndoe.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = johndoe.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80 ;
    listen [::]:80 ;
    server_name www.johndoe.com johndoe.com;
    return 404; # managed by Certbot
}

Right now on johndoe.com, the app from port:5000 is hosted and it works correct. When I enter subdomain like blog.johndoe.com it also works at the same port. I want to specify another port for this subdomain or even serve static pages. It looks like no matter what subdomain I use, it is always using default “/” location. How to achieve this?



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

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.

Hi there @mrblot,

What you would need to do is create another Nginx server block with the following content:

server {
    root /var/www/html;
    listen 80 ;
    listen [::]:80 ;
    server_name blog.johndoe.com www.blog.johndoe.com;

    index index.html index.htm index.nginx-debian.html;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        proxy_pass http://localhost:5001;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
    }

}

Essentially you need to duplicate the existing server block and update 2 main things:

  • server_name - set this to your subdomain
  • proxy_pass - set the port to your respective node port

Make sure to test your Nginx configuration after each change with:

  1. sudo nginx -t

And only if you get Syntax OK then restart Nginx:

  1. sudo systemctl restart nginx

Hope that this helps. Regards, Bobby

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.