Report this

What is the reason for this report?

How can I set up a Load Balancer for my Expressjs based application?

Posted on May 24, 2020

I’ve set up a load balancer that is picking up my droplets by tag, however, they are always showing as a status of “down”.

I am pointing the load balancer directly to my nodejs APIs, without Nginx or anything sitting in front. I also have an endpoint set up called “/health” which simply returns a status of 200, and the load balancer set to use that endpoint.

Below is the configuration for my Expressjs server.

app.set('trust proxy', true);

app.use(helmet());
app.use(cors());
app.use(passport.initialize());
app.get('/health', (req, res) => res.sendStatus(200));
app.use('/api', routes);

app.listen(PORT, () => {
    console.log(
        chalk.yellow(`Listening on *:${PORT}`)
    );
});

My load balancer configuration is as follows

Forwarding rules 
HTTP on port 80  HTTP on port 3000
HTTPS on port 443  HTTP on port 3000

Algorithm
Least Connections

Health checks 
http://0.0.0.0:3000/health

Sticky sessions 
Off

SSL
Redirect HTTP to HTTPS

Proxy Protocol
Enabled


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,

Based on the output, it looks like that the DigitalOcean Load Balancer health checks are not reaching your application correctly, which is why it’s showing your droplets as “down”. Here are some potential issues and solutions:

  1. Health check path and port: In your DigitalOcean Load Balancer configuration, your health check is set to http://0.0.0.0:3000/health. The 0.0.0.0 should be replaced with the private IP of the droplet or simply /health if the load balancer and the droplet are in the same VPC network.

  2. Check if the firewall is allowing incoming connections: Ensure that your firewall is configured to allow incoming connections to port 3000 (the port on which your Node.js application is running) from the IP address of the Load Balancer. This also applies to any security group rules if you’re using VPC networks.

  3. Check if the application is running and accessible from the outside: You can SSH into your droplet and use a tool like curl to test if your health check endpoint is accessible. Run curl http://localhost:3000/health to check this.

  4. Trust proxy setting: In Express.js, app.set('trust proxy', true); tells the app to trust the headers set by the proxy (in this case, the load balancer). This is generally required when you want to access protocol (HTTP/HTTPS) or get the real client IP address in your application.

  5. Use a process manager: If you are running your application directly using node app.js, it will stop as soon as the terminal session is closed. It’s recommended to use a process manager like pm2 which will ensure that your app keeps running even after the terminal session is closed or the server is restarted.

  6. Use HTTPS for health checks: If your server is set to redirect all HTTP to HTTPS, then your health check also needs to be in HTTPS, not HTTP. Ensure your load balancer is setup with an SSL certificate (either from DigitalOcean or a custom one) and change the health check to https://localhost:3000/health.

Best,

Bobby

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.