Question
Multiple node apps for multiple subdomains
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.
×