Pie.host

What Is a Reverse Proxy? A Practical Guide to Setting Up Nginx Reverse Proxy

Learn what a reverse proxy is, how it works, and how to configure Nginx on a PieBox VPS for HTTPS, load balancing, and application routing.

Rabikant

8 min read

Imagine you're running a website. Users visit your domain, your application processes their requests, and everything works fine.

But as your application grows, things get a little more complicated.

You might have multiple backend servers. You might want to hide your application server from the public internet. Perhaps you want to handle HTTPS, distribute traffic, or route different domains to different applications.

This is where a reverse proxy comes in.

A reverse proxy is one of those infrastructure concepts that sounds complicated until you understand what it actually does. And chances are, if you've deployed a web application, you've already encountered one.

Let's break it down and build one using Nginx.

What Is a Reverse Proxy?

A reverse proxy is a server that sits between your users and your backend application servers.

When someone visits your website, their request doesn't go directly to your application server. Instead, it first reaches the reverse proxy.

The reverse proxy then forwards that request to the appropriate backend server, receives the response, and sends it back to the user.

The user never needs to know which backend server actually handled the request.

Think of it like a receptionist in an office.

Visitors don't walk into the building and start searching for individual employees. They speak to the receptionist, who directs them to the right person.

A reverse proxy does exactly that, but for internet traffic.

The basic flow looks like this:

         User / Browser
               |
               | HTTP Request
               v
        Reverse Proxy
           (Nginx)
               |
       -----------------
       |               |
       v               v
   App Server 1    App Server 2
       |               |
       -----------------
               |
               v
        Reverse Proxy
               |
               v
         User / Browser

The reverse proxy acts as the entry point for incoming requests.

Reverse Proxy vs Forward Proxy

These two terms are often confused, but they solve different problems.

Forward Proxy: Sits between a client and the internet. It makes requests on behalf of the client.

For example, a company might use a forward proxy to control which websites employees can access.

Reverse Proxy: Sits between internet users and backend servers. It receives requests on behalf of the servers.

For example, a website might use Nginx to receive all incoming traffic and forward it to its application.

The easiest way to remember:

  • Forward proxy protects or represents clients.
  • Reverse proxy protects or represents servers.

Why Do You Need a Reverse Proxy?

A reverse proxy isn't just an extra server sitting in front of your application. It solves several real infrastructure problems.

1. Load Balancing

Imagine your application is getting 50,000 requests per minute.

One server might struggle to handle all that traffic. You can run multiple instances of your application and let the reverse proxy distribute incoming requests between them.

For example:

             Incoming Traffic
                    |
                    v
                  Nginx
                    |
          -------------------
          |        |        |
          v        v        v
        App 1    App 2    App 3

Nginx can distribute requests across these servers using different load-balancing strategies.

2. Improved Security

Without a reverse proxy, your backend application server might be directly exposed to the internet.

With a reverse proxy, you can keep your application running on an internal port, such as 3000, while exposing only Nginx on ports 80 and 443.

This reduces your application's public attack surface.

It also allows you to apply security rules, request limits, and access restrictions at a single entry point.

3. SSL Termination

Your application doesn't necessarily need to handle HTTPS encryption itself.

Nginx can handle incoming HTTPS connections, decrypt requests, and forward them to your application over an internal connection.

This is called SSL termination.

It makes certificate management easier, especially when you're running multiple applications on the same server.

4. Routing Multiple Applications

You can run multiple applications on one VPS and use a reverse proxy to direct visitors to the right application.

For example:

Domain Backend
example.com localhost:3000
api.example.com localhost:4000
dashboard.example.com localhost:5000

One public IP address, one Nginx installation, multiple applications.

How to Set Up Nginx as a Reverse Proxy on a VPS

Enough theory. Let's build one.

For this tutorial, we'll use:

  • A VPS running Ubuntu
  • Nginx as our reverse proxy
  • A Node.js application running on port 3000
  • A domain name pointing to our VPS

We'll use PieBox as our VPS. PieBox provides cloud virtual private servers where you get control over your own Linux environment.

Step 1: Get a VPS

First, create a VPS on PieBox.

Choose an Ubuntu image and deploy your server.

Once your VPS is ready, you'll get a public IP address.

Connect to it using SSH:

ssh root@YOUR_SERVER_IP

You now have access to your Linux server.

Step 2: Install Nginx

Update your package list:

sudo apt update

Install Nginx:

sudo apt install nginx -y

Start Nginx:

sudo systemctl enable nginx
sudo systemctl start nginx

Check its status:

sudo systemctl status nginx

Now open your VPS IP address in a browser:

http://YOUR_SERVER_IP

If everything is working, you should see the default Nginx welcome page.

Congratulations! Nginx is running on your VPS.

Step 3: Run Your Backend Application

Let's assume you have a Node.js application that listens on port 3000.

For this example, your application should be accessible locally:

curl http://127.0.0.1:3000

You should receive a response from your application.

Important: Configure your application to listen on 127.0.0.1 rather than exposing port 3000 publicly.

This ensures that external users access your application through Nginx instead of connecting directly to the backend.

Step 4: Configure Nginx as a Reverse Proxy

Now comes the interesting part.

Create a configuration file:

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

Add the following configuration:

server {
    listen 80;
    server_name example.com www.example.com;

    location / {
        proxy_pass http://127.0.0.1:3000;

        proxy_http_version 1.1;

        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;
    }
}

Replace example.com with your actual domain.

Let's understand what these directives do:

  • listen 80: Accepts incoming HTTP traffic.
  • server_name: Specifies which domain this configuration handles.
  • proxy_pass: Forwards incoming requests to your backend application.
  • proxysetheader Host: Preserves the original hostname.
  • X-Real-IP: Passes the original visitor's IP address.
  • X-Forwarded-For: Preserves the forwarding IP chain.
  • X-Forwarded-Proto: Tells your application whether the original request used HTTP or HTTPS.

Enable the configuration:

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

Test the configuration:

sudo nginx -t

If everything looks good, reload Nginx:

sudo systemctl reload nginx

Step 5: Point Your Domain to Your VPS

Go to your domain's DNS provider and create an A record.

Type Name Value
A @ YOURSERVERIP
A www YOURSERVERIP

Wait for DNS propagation.

Now visit:

http://example.com

Instead of seeing the Nginx welcome page, you should see your Node.js application.

What's happening behind the scenes?

Browser
   |
   | http://example.com
   |
   v
Your PieBox VPS
   |
   v
Nginx (Port 80)
   |
   | Forwards request
   |
   v
Node.js App (Port 3000)
   |
   v
Response to Browser

Your application is running on port 3000, but visitors don't need to know that.

Step 6: Enable HTTPS

You probably don't want your website running on plain HTTP.

Let's configure a free SSL certificate using Let's Encrypt and Certbot.

Install Certbot:

sudo apt install certbot python3-certbot-nginx -y

Run:

sudo certbot --nginx -d example.com -d www.example.com

Follow the instructions.

Certbot will obtain your certificate and configure Nginx to serve HTTPS traffic.

Your website should now be accessible at:

https://example.com

Certbot also configures automatic certificate renewal.

Bonus: Using Nginx as a Load Balancer

Remember how we discussed handling multiple backend servers?

Let's say you have three Node.js application instances:

  • Server 1: 127.0.0.1:3000
  • Server 2: 127.0.0.1:3001
  • Server 3: 127.0.0.1:3002

Nginx can distribute requests between all three.

Add an upstream block:

upstream backend_servers {
    server 127.0.0.1:3000;
    server 127.0.0.1:3001;
    server 127.0.0.1:3002;
}

Then update your location block:

location / {
    proxy_pass http://backend_servers;

    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;
}

By default, Nginx uses a round-robin strategy to distribute requests across the available servers.

You can also configure other strategies, such as least connections and IP-based hashing.

This is one of the simplest ways to start scaling an application without introducing a complicated infrastructure setup.

What About WebSockets?

If you're building real-time applications using WebSockets, Nginx can also proxy WebSocket connections.

You'll need a slightly different configuration:

map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://127.0.0.1:3000;

        proxy_http_version 1.1;

        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;

        proxy_read_timeout 3600s;
        proxy_send_timeout 3600s;
    }
}

The Upgrade and Connection headers allow Nginx to handle the HTTP-to-WebSocket protocol upgrade.

The timeout settings help prevent long-lived connections from being closed prematurely.

If you're building real-time applications, this is particularly important.

Do You Actually Need a Reverse Proxy?

If you're running a small application, you might wonder whether adding Nginx is necessary.

Well, you don't always need one. But it becomes extremely useful when you want:

  • Multiple applications running on one VPS.
  • HTTPS termination and certificate management.
  • Load balancing across backend instances.
  • A single public entry point for your infrastructure.
  • Better control over incoming traffic.
  • WebSocket proxying.

Nginx is one of the most widely used reverse proxies, and it's a great place to start learning server infrastructure.

Deploy Your Own Infrastructure with PieBox

You don't need an expensive cloud architecture to start understanding infrastructure.

With PieBox, you get your own cloud VPS where you can install Nginx, deploy applications, configure reverse proxies, run databases, set up VPNs, and experiment with practically anything supported by your Linux environment.

You get root access and control over your server.

Whether you're deploying your first Node.js application or experimenting with multi-server architectures, PieBox gives you a place to build and learn.

Explore PieBox: https://piehost.com/piebox

Build it. Break it. Learn how infrastructure actually works.

Comments

No comments yet. Questions and corrections are welcome.