How to Set Up Nginx as a Reverse Proxy: A Step-by-Step Guide

A reverse proxy sits in front of your backend servers and forwards client requests to them. Nginx is a popular, high‑performance choice for this task. It can balance load, cache content, and add an extra layer of security. In this guide, you’ll learn the essential steps to configure Nginx as a reverse proxy.

Before you begin, ensure Nginx is installed on your server (most Linux distributions include it in their package manager). You also need a running backend service—such as a Node.js app or a Python web server—that listens on a specific port, for example localhost:3000.

Article illustration

1. Understand the Basic Reverse Proxy Directive

The core of Nginx reverse proxy configuration is the proxy_pass directive. It tells Nginx where to forward incoming requests. A typical block inside a location looks like this:

location / {
    proxy_pass http://localhost:3000;
}

This sends all requests under the root path to your backend at port 3000.

2. Set Up a Server Block

Create a new configuration file in /etc/nginx/sites-available/ (or modify the default). Include a server block that listens on a port (usually 80) and specifies your domain or IP:

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://localhost:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

The proxy_set_header lines pass important client information to your backend.

3. Add Common Headers and Optimizations

For a production setup, include additional headers to preserve original IP addresses and support WebSockets:

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";

These ensure your backend receives the real client IP and protocol, and enables WebSocket connections.

4. Test and Reload Nginx

After saving the configuration, test for syntax errors:

sudo nginx -t

If the test passes, reload Nginx to apply changes:

sudo systemctl reload nginx

Now visit your server’s domain or IP—you should see your backend application responding.

In a few minutes, you’ve turned Nginx into a powerful reverse proxy. Experiment with load balancing and SSL termination to take full advantage of its capabilities.

sarah antaboga
Author: sarah antaboga

Leave a Reply

Your email address will not be published. Required fields are marked *