Question

Multiple node apps for multiple subdomains

Posted November 22, 2020 708 views
NginxNode.js

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?

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.

×
Submit an Answer
1 answer

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:

  • sudo nginx -t

And only if you get Syntax OK then restart Nginx:

  • sudo systemctl restart nginx

Hope that this helps.
Regards,
Bobby