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!
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 subdomainproxy_pass - set the port to your respective node portMake 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
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.