Report this

What is the reason for this report?

How To Setup Multiple Node.js On Single Server

Posted on July 15, 2020

I’m currently learning node.js and loving it. I have a single server which is bounded with just 1 ip addresses. But I have a two node.js app on that server. My nginx is like:

  server {
    listen 80;
    server_name domain.comm www.domain.comm;
    location / {
      proxy_pass http://localhost:6300;
      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;
    }
  }


server {
  listen 80;
  server_name lite.domain.com;
  location / {
    proxy_pass http://localhost:5300;
    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;
  }
}

And all my DNS records all set it up. I’d already set lite.domain.com to my server ip. When i try to run that nginx domain.com gives blank page with title and lots of 404 error on inspect page.

lite.domain.com also taking a action same as domain.com

I checked all my apps are working properly at background and clean. So whats wrong with my nginx configuration as reverse proxy for node.js?

Thank you for any input <3



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 @dethnass,

Your Nginx server blocks actually look correct.

Can you share some of the 404 errors that you get?

Also, have you tried visiting your applications on the specific ports just to test if they are running as expected: lite.domain.com:5300?

Regards, Bobby

Hi @bobbyyiliev

When i try to reach lite.domain.com or directly http://www.domain.com it gives me white of death (blank white) screen with that 404 console outputs:

Refused to apply style from 'http://lite.domain.com/css/bootstrap.min.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
lite.domain.com/:1 Refused to apply style from 'http://lite.domain.com/public/css/index.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
jquery.min.js:1 Failed to load resource: the server responded with a status of 404 (Not Found)
bootstrap.min.js:1 Failed to load resource: the server responded with a status of 404 (Not Found)
power.min.js:1 Failed to load resource: the server responded with a status of 404 (Not Found)
index.js:1 Failed to load resource: the server responded with a status of 404 (Not Found)
lite.domain.com/:1 Refused to apply style from 'http://lite.domain.com/css/bootstrap.min.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
lite.domain.com/:1 Refused to apply style from 'http://lite.domain.com/public/css/index.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.

My dns records is like:

DNS records
Type	Hostname	Value	TTL (seconds)	
A	 lite.domain.com directs to  46.101.xxx.xx 3600  
A	 www.domain.com	 directs to  46.101.xxx.xx 3600 

By the way i have an also another droplet that carry just single app on with that configuration:

server {
	listen 80 default_server;
	listen [::]:80 default_server;


	root /var/www/app;


	server_name hellonode;

	location ^~ /assets/ {
		gzip_static on;
		expires 12h;
		add_header Cache-Control public;
  }

	location / {
		proxy_http_version 1.1;
		proxy_cache_bypass $http_upgrade;
		proxy_set_header Upgrade $http_upgrade;
		proxy_set_header Connection 'upgrade';
		proxy_set_header Host $host;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_set_header X-Forwarded-Proto $scheme;
		proxy_pass http://localhost:5300;
	}
}

That droplet works very well on http://ip/ with 0 error but i cant figure out my another droplet with two app.

An update on an older thread

The Nginx configuration for setting up a reverse proxy for two Node.js apps appears mostly correct, but the issues you’re experiencing (blank page and 404 errors) suggest there might be a problem with how the static assets are being served or a potential mismatch in the expected URLs between your Node.js app and the Nginx configuration. Here are a few steps to diagnose and potentially resolve the issue:

1. Check Node.js Application Routes

  • Ensure that your Node.js applications are correctly serving content on the respective ports (6300 and 5300). You can test this by directly accessing http://localhost:6300 and http://localhost:5300 on the server.

2. Nginx Configuration for Static Assets

  • If your Node.js applications are serving static files (like CSS, JavaScript, images), ensure that the paths to these files are correct relative to the root URL.
  • Sometimes, the issue is with the path of the static assets in the HTML being served. For instance, if your app references /styles/main.css, this file must be accessible at http://localhost:6300/styles/main.css (or 5300 for the second app).

3. Inspect Browser Network Tab

  • Use the browser’s Developer Tools (usually F12) and go to the Network tab.
  • Refresh your page and look for any resources that are failing to load (404 errors). This can give you clues about what’s missing or being misrouted.

4. Check Nginx Access and Error Logs

  • Nginx logs can provide valuable information. Check both the access and error logs:
sudo tail -f /var/log/nginx/access.log
sudo tail -f /var/log/nginx/error.log

5. Nginx Configuration Reload

  • After making any changes to your Nginx configuration, ensure you reload Nginx to apply those changes:
sudo nginx -t
sudo systemctl reload nginx

6. Node.js Server Configuration

  • Ensure your Node.js server is not bound to localhost in its listen function. It should be listen(port) or listen(port, '0.0.0.0').

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.