By selll
As I was following this tutorial: https://www.digitalocean.com/community/questions/setting-up-multiple-nodejs-applications-using-nginx-vitual-hosts
It doesn’t seem to work, as when i completed all steps and went to the sub domain it says this site cannot be reached, but if i just go to the ip it just shows this: https://imgur.com/a/CeY6iiu
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 @selll,
I would recommend following the steps from this tutorial here instead:
Then once you want to start a second Node.js application keep the following things in mind:
/etc/nginx/sites-available/default to /etc/nginx/sites-available/example2.conf and adjust the values there to match your second application- ln -s /etc/nginx/sites-available/example2.conf /etc/nginx/sites-example/example2.conf
Finally, restart Nginx as explained in the tutorial.
Let me know how it goes. Regards, Bobby
To set up Nginx as a reverse proxy for multiple Node.js applications, each serving a different domain, you need to configure Nginx to listen on different server blocks for each domain and then proxy the requests to the respective Node.js applications running on different ports.
Here’s a basic guide on how to achieve this:
Let’s assume you have two Node.js applications you want to serve on domain1.com and domain2.com. Start each Node.js application on a different port. For example:
domain1.com) on port 3001domain2.com) on port 3002If Nginx is not already installed on your server, you can install it using your package manager:
For Ubuntu/Debian:
sudo apt update
sudo apt install nginx
For CentOS/RHEL:
sudo yum install nginx
Create separate Nginx server blocks for each domain. This involves editing or creating files in /etc/nginx/sites-available/ and then creating a symbolic link to them in /etc/nginx/sites-enabled/.
Create Nginx Server Block for Domain 1
Create a new configuration file:
sudo nano /etc/nginx/sites-available/domain1.com
Add the following configuration:
server {
listen 80;
server_name domain1.com www.domain1.com;
location / {
proxy_pass http://localhost:3001;
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;
}
}
Create another configuration file:
sudo nano /etc/nginx/sites-available/domain2.com
Add a similar configuration, but change the server_name and proxy_pass:
server {
listen 80;
server_name domain2.com www.domain2.com;
location / {
proxy_pass http://localhost:3002;
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;
}
}
Enable the Server Blocks
Create symbolic links to enable these configurations:
sudo ln -s /etc/nginx/sites-available/domain1.com /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/domain2.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
Make sure the DNS records for domain1.com and domain2.com are correctly pointing to the IP address of your server where Nginx is running.
If you have a firewall enabled, ensure that it allows HTTP (and HTTPS, if you’re using SSL) traffic:
For Ubuntu/Debian with UFW:
sudo ufw allow 'Nginx Full'
For CentOS/RHEL:
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
Consider securing your applications with SSL certificates. You can use Let’s Encrypt to obtain free certificates. Certbot is a tool that can automate this process for you.
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.