How to Set Up a Reverse Proxy for Node.js

If you’re building a web app using Node.js, setting up a reverse proxy can really help. It makes your app faster, safer, and easier to manage. I didn’t understand it at first either, but once I set one up myself, it all started to make sense. Let me show you how I did it.

A reverse proxy is a server that sits between your users and your app. Instead of users talking to your app directly, they talk to the reverse proxy. Then the proxy talks to your app. It’s like a middleman or a receptionist. It takes in requests and forwards them to the right place.

The most common reason I use a reverse proxy is to run multiple apps on one server. I can have my Node.js app, a WordPress blog, and maybe something else too, all on the same machine. Each app gets its own path or domain. The reverse proxy figures out which app should respond.

In this guide, I’ll walk you through setting up a reverse proxy for a Node.js app using Nginx. Nginx (pronounced “engine-x”) is a popular web server. It’s fast, light, and works great as a reverse proxy.

Why Use a Reverse Proxy?

Before we start, it helps to know why you might want one. Here are a few solid reasons:

  • Security: Hide your Node.js app behind the proxy. You only expose Nginx to the world, not your app.
  • Performance: Nginx can handle more traffic. It can cache static files and reduce the load on your Node.js app.
  • Flexibility: Run more than one app. Handle different domains or paths.

When I set up my first Node.js app, I just ran it on port 3000 and opened that port on the firewall. It worked, but it felt raw. Anyone could hit that port directly. Once I added Nginx in front of it, things felt more professional. I could use HTTPS, better logging, and easier error pages.

What You Need

Let’s keep it simple. You only need a few things:

  • A server or computer running Linux (I used Ubuntu)
  • A Node.js app already running (can be a basic “Hello World”)
  • Nginx installed

If you don’t have Node.js yet, you can install it with:

sudo apt update
sudo apt install nodejs npm

Check if it’s working:

node -v
npm -v

Then make a small app:

// app.js
const http = require('http');
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello from Node.js');
});

server.listen(port, () => {
  console.log(`Server running at http://localhost:${port}/`);
});

Run it:

node app.js

Visit http://your-server-ip:3000 and you should see the message.

Install Nginx

Now install Nginx:

sudo apt install nginx

Check if it’s running:

systemctl status nginx

Visit http://your-server-ip and you should see the Nginx welcome page.

Configure the Reverse Proxy

Time for the good part. We’ll tell Nginx to forward traffic to your Node.js app.

Open a config file:

sudo nano /etc/nginx/sites-available/myapp

Add this:

server {
    listen 80;
    server_name yourdomain.com; # Or your IP address

    location / {
        proxy_pass http://localhost:3000;
        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;
    }
}

Replace yourdomain.com with your real domain or IP address.

Now link the file:

sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/

Then test Nginx:

sudo nginx -t

If all looks good:

sudo systemctl restart nginx

Now visit http://yourdomain.com and it should show your Node.js app!

Common Mistakes I Made

When I first tried this, I ran into a few bumps. Maybe these tips will save you some time:

  • I forgot to restart Nginx after changing the config. It won’t work until you restart.
  • I had a typo in the proxy_pass line. Make sure the port matches where your app runs.
  • My Node.js app crashed and I didn’t know. Use something like pm2 to keep it alive.

Keeping Node.js Running

When I closed my terminal, my app would stop. That’s annoying. I fixed it by installing pm2.

sudo npm install -g pm2
pm2 start app.js
pm2 startup
pm2 save

This makes your app restart automatically, even after reboots. So you don’t have to keep the terminal open.

Bonus: Add HTTPS with Let’s Encrypt

Want to make your site secure with HTTPS? I did, and it wasn’t that hard. I used Certbot.

Install Certbot:

sudo apt install certbot python3-certbot-nginx

Then run:

sudo certbot --nginx

Follow the prompts, and you’ll get HTTPS with auto-renew.

Summary

Setting up a reverse proxy may sound complicated, but it really isn’t. It just takes a few clear steps. Here’s what we did:

  • Installed Node.js and made a test app
  • Installed Nginx
  • Made a config file to forward traffic
  • Restarted Nginx
  • Used pm2 to keep the app alive

Optional steps:

  • Added HTTPS with Certbot

Final Thoughts

Now that you know how to set up a reverse proxy, you’ll find it useful for more than just Node.js. You can use it for Python apps, PHP apps, even Docker containers.

It made my setup neater and easier to expand. If you ever run into problems, start simple. Just make sure your app works on its own first, then add Nginx step by step.

Have you set up a reverse proxy before? What worked for you? What didn’t? Let me know if you got stuck anywhere and I’ll try to help.

Leave a Reply