NGINX reverse proxy

Learn what an NGINX reverse proxy does in front of an Axum server, how proxy_pass forwards requests, which headers to pass so the app sees the real client, and how to test and reload the configuration

An NGINX reverse proxy is a server that sits in front of an Axum server and forwards requests to it. A browser connects to NGINX, NGINX opens its own second connection to the Axum app, sends the request on, waits for the answer, and returns it to the browser. The browser never talks to the Axum process directly. It talks to NGINX, and NGINX talks to Axum.

Where This Fits

The previous idea put encryption on the Axum server itself, with the server holding the certificate and doing the TLS work. A reverse proxy is the other place that work can live, and it is the place most production deployments choose. NGINX takes the public connection, ends the encryption there, and forwards plain HTTP to Axum on the same machine. This page is about that forwarding: how it is configured, what information NGINX must pass on so the app is not blind, and how to check the configuration before it goes live.

Think of a reception desk in an office building. Visitors speak to the receptionist, who checks who they are, writes down where they came from, and sends them up to the right office. The people in the office never meet the visitor at the front door. They meet them after the receptionist has done the checking and the directing. NGINX is the receptionist, and the Axum app is the office.

The Smallest Working Version

The smallest version forwards every request on port 80 to an Axum server on port 3000. Create a file called axum-proxy.conf with this content, and keep the Axum server from the earlier pages running on port 3000.

# axum-proxy.conf
events {}

http {
    server {
        listen 80;
        server_name localhost;

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

Check the configuration before using it. The nginx -t command reads the file, checks its syntax, and reports whether it is valid, without starting anything. For a standalone file, point nginx -t at it directly:

nginx -t -c /path/to/axum-proxy.conf -p /path/to/

On a system where NGINX is installed normally, the configuration lives in /etc/nginx/sites-available/ and is linked into /etc/nginx/sites-enabled/, and the check is simply:

sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Two lines, both saying the same thing in different words: the file parses, and the test passed. That output is the green light to reload. Start or reload NGINX:

sudo systemctl reload nginx

Now ask for a page through the proxy:

curl http://localhost/

The answer comes from the Axum app, but the connection went to NGINX first. The Axum server's log shows a request from 127.0.0.1, which is NGINX, not the browser. That detail is the first problem with the smallest version, and the rest of this page fixes it.

Working Setup:

If nginx -t prints "syntax is ok" and "test is successful", the configuration is valid. If curl through port 80 returns the Axum app's answer, the proxy is forwarding. Both checks must pass before the configuration is trusted.

The Concept Behind a Reverse Proxy

A reverse proxy is called "reverse" because it works in the opposite direction from the proxy most people know. A forward proxy sits next to the client and fetches things on the client's behalf. A reverse proxy sits next to the server and receives things on the server's behalf. The client does not know the Axum server exists. It knows NGINX, and NGINX decides which server gets the request.

That position gives the proxy four jobs. It is the public face, so it takes the connections from the internet and the Axum server never sees them. It ends TLS, so it holds the certificate and decrypts the traffic. It routes, so it can send one path to one app and another path to a different app. And it protects, so it can limit how fast requests arrive and how large they may be, before they reach the application. The Axum app, behind the proxy, does one job only: answer requests. Everything about the public internet is the proxy's problem.

A beginner can picture a castle with a gatehouse. Visitors stop at the gatehouse, where guards check them and note their names, and only then are they let into the courtyard. The people inside the castle never stand at the outer wall. The gatehouse is the proxy, the castle is the application, and the guards' notes are the headers this page is about.

Understanding the Smallest Configuration Piece by Piece

The configuration file has a shape that is worth learning once, because every NGINX configuration uses it. It is made of blocks, which are named sections with braces, and directives, which are single lines that end in a semicolon.

The events and http blocks

events {}

http {

events is a block that configures how NGINX handles connections, and an empty one is fine for learning. http is the block that holds everything about HTTP serving. Every server block lives inside http, and every location block lives inside a server. The nesting is the structure: a file holds an http block, the http block holds servers, and each server holds locations.

The server block

server {
    listen 80;
    server_name localhost;

listen 80 says this server answers on port 80, the default port for plain HTTP. server_name localhost says this server answers for requests addressed to localhost. When a request arrives, NGINX looks at the name in the request and picks the server block whose server_name matches. A server with several names, like a domain and its www form, lists them all on one line.

The location block

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

location / matches every path, because every path starts with /. proxy_pass is the directive that does the forwarding: it names the address NGINX should open a connection to, which is the Axum server on the local machine, port 3000. When a request for /about arrives, NGINX opens a connection to 127.0.0.1:3000, sends the request for /about on that connection, waits for the answer, and returns it to the browser.

The Headers the App Needs

The smallest version forwards the request, but it loses information on the way. The Axum app sees the connection coming from 127.0.0.1, which is NGINX, and it has no idea who the real client was, what name they asked for, or whether they used HTTP or HTTPS. Four headers fix that, and they belong in every location block that proxies to an application.

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

Each line is explained below, because each one repairs a different piece of lost information. The variables on the right, $host, $remote_addr, $scheme, are values NGINX knows about the incoming request. The header names on the left are the names the Axum app will read.

Host

proxy_set_header Host $host;

Without this line, NGINX rewrites the Host header to the address in proxy_pass, so the app sees 127.0.0.1:3000 as the name the client asked for. With it, the app sees the name the client actually typed, like example.com. The app needs the real name to build correct links, like the link in a password reset email, and to match requests to the right site when several sites share one server.

X-Real-IP

proxy_set_header X-Real-IP $remote_addr;

$remote_addr is the IP address of the client that connected to NGINX. Without this header, the app sees every visitor as 127.0.0.1, because every connection to the app comes from NGINX. With it, the app sees the real client address, which is what logging, rate limiting, and abuse handling all depend on.

X-Forwarded-For

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

This header carries the chain of addresses a request passed through. $proxy_add_x_forwarded_for takes any X-Forwarded-For the client already sent and appends the client's address to it. The result is a comma-separated list, where the last address is the one NGINX added. An app that reads this header must read it from the right, because a client can send a fake value of its own, and the only entry the app can trust is the one its own NGINX added.

X-Forwarded-Proto

proxy_set_header X-Forwarded-Proto $scheme;

$scheme is http or https, depending on how the client connected to NGINX. Without this header, the app receives a plain HTTP connection from NGINX and cannot tell whether the client used HTTPS. With it, the app knows the original protocol, which matters for marking cookies as secure and for redirecting plain requests to HTTPS. An app that does not know the client used HTTPS may send a plain redirect back to a user who is already on HTTPS, which loops forever.

proxy_http_version

proxy_http_version 1.1;

NGINX speaks HTTP/1.0 to the upstream by default, which opens a fresh connection for every request and cannot do protocol upgrades. Version 1.1 keeps connections open between requests and allows upgrades, which WebSocket connections need. The line is one word of configuration, and without it the proxy is slower and WebSockets do not work through it.

The Headers Must Sit in the Same Location Block:

A proxy_set_header line only applies to the location block it sits in. A header set in one location does not carry over to another, and a header set at the server level is inherited only by locations that do not set the same header themselves. Put all four headers in every location that has a proxy_pass, or the app will see the real client on one path and 127.0.0.1 on another.

The Upstream Block

When the app runs on more than one port, or when NGINX should spread requests across several copies of the app, the addresses are named once in an upstream block and the proxy_pass refers to the name.

http {
    upstream axum_app {
        server 127.0.0.1:3000;
        server 127.0.0.1:3001;
    }

    server {
        listen 80;
        server_name localhost;

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

The upstream block names a group of servers, here two copies of the same app on ports 3000 and 3001. proxy_pass http://axum_app sends each request to one of them, alternating by default, so the load is shared. If one copy stops answering, NGINX marks it down and sends its traffic to the other. The name axum_app is arbitrary: it is a label the configuration invents, and it only has to match between the upstream block and the proxy_pass line.

Adding HTTPS to the Proxy

The proxy is where TLS usually terminates. The HTTPS server block listens on port 443, holds the certificate and key, and forwards decrypted traffic to the Axum app on the local network. A second server block on port 80 redirects plain requests to the HTTPS address.

server {
    listen 80;
    server_name example.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

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

The first block answers every plain request with a 301 redirect to the same address over HTTPS. $host is the name the client asked for, and $request_uri is the path and query string, so the redirect preserves exactly where the user was going. The second block is the real server: it listens on 443 with ssl, reads the certificate and key from the paths shown, and proxies to the app. The certificate paths shown are the ones Let's Encrypt uses by default, and the Axum app itself never sees a certificate or a key.

The App Sees Plain HTTP Behind the Proxy:

When NGINX terminates TLS, the connection from NGINX to Axum is plain HTTP on the local network. That is expected, and it is why the X-Forwarded-Proto header matters: it is the only way the app knows the client used HTTPS. The local hop is trusted because it never leaves the machine.

Proxying WebSockets

A WebSocket connection starts as an HTTP request that asks to upgrade to a persistent two-way connection. NGINX needs two extra headers to pass that upgrade through to the app, and without them the upgrade fails and the connection stays plain HTTP.

location /ws {
    proxy_pass http://127.0.0.1:3000;
    proxy_http_version 1.1;
    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;
}

$http_upgrade is the value of the client's Upgrade header, which says websocket. Passing it through tells the app the client wants an upgrade. The Connection header set to upgrade is the other half of the handshake. Both lines must be present, and proxy_http_version 1.1 must be set, because HTTP/1.0 cannot do upgrades. The location is /ws, which is the path the app's WebSocket route lives on, so only WebSocket traffic takes this block and ordinary pages take the plain one.

The Request Flow

The sequence below shows one request travelling from the browser to the app and back through the proxy.

Browser sends GET /about to NGINX on port 80 or 443
        |
        v
NGINX matches the server_name and the location /
        |
        v
NGINX opens a connection to 127.0.0.1:3000
        |
        v
NGINX sends the request with the four forwarding headers
        |
        v
Axum routes /about and builds the response
        |
        v
NGINX returns the response to the browser
        |
        v
Browser shows the page, never knowing Axum exists

The browser's whole experience is with NGINX. The Axum app's whole experience is with NGINX. Neither side sees the other, and that separation is the point: the proxy can change, move, or be replaced without the app or the browser noticing.

Testing and Reloading

Every change to the configuration must pass nginx -t before it is loaded. A configuration with a syntax error that is reloaded anyway stops NGINX from starting, and the site goes down. The check is fast and free, and there is no reason to skip it.

sudo nginx -t
sudo systemctl reload nginx

reload tells the running NGINX to re-read its configuration without dropping connections. Existing connections finish on the old configuration, and new connections use the new one. That is a graceful reload, and it is the reason a proxy can be reconfigured in the middle of the day without anyone noticing. restart, by contrast, stops NGINX entirely and starts it again, which drops every connection in flight. Use reload for configuration changes and restart only when the NGINX binary itself has been updated.

To prove the proxy forwards the headers, add a route to the Axum app that echoes the headers it received, and call it through the proxy:

// src/main.rs
use axum::{http::HeaderMap, routing::get, Router};

async fn show_headers(headers: HeaderMap) -> String {
    format!(
        "host={:?} x-real-ip={:?} x-forwarded-for={:?} x-forwarded-proto={:?}",
        headers.get("host"),
        headers.get("x-real-ip"),
        headers.get("x-forwarded-for"),
        headers.get("x-forwarded-proto"),
    )
}
curl http://localhost/headers

Through the proxy, the output shows the real values: the host the client asked for, the client address, the forwarding chain, and the original protocol. Without the four header lines in the configuration, the same route shows 127.0.0.1 for the address and nothing for the forwarding headers. That before-and-after is the test that the configuration is complete.

The configuration on this machine was checked live with NGINX 1.24.0: nginx -t reported the syntax as ok, and a request through the proxy on a test port reached the Axum server and returned its answer with the forwarding headers set. The test used a standalone configuration file on a high port rather than the system port 80, so the check did not disturb any other service on the machine.

Common Beginner Mistakes

Forgetting the headers and wondering why every visitor is 127.0.0.1

# Wrong - the app sees NGINX as the only client, forever
location / {
    proxy_pass http://127.0.0.1:3000;
}

This configuration forwards traffic correctly, and the site works. The damage is invisible: every log line says the client was 127.0.0.1, rate limiting counts all visitors as one person, and abuse from one address cannot be distinguished from normal traffic. The fix is the four header lines from the section above, in every location block.

Reloading without testing first

# Wrong - a typo here stops NGINX from starting
sudo systemctl restart nginx

A configuration with a missing semicolon or a wrong brace makes NGINX refuse to start, and a restart leaves the site down until the error is fixed. The fix is nginx -t before every reload, and reload instead of restart, so a bad configuration is caught while the old one is still serving.

A Missing Semicolon Stops the Whole Server:

NGINX directives end with a semicolon, and a missing one is a syntax error that prevents the entire configuration from loading. nginx -t names the file and line where the error is. Fix that line, run nginx -t again, and only then reload.

The trailing slash in proxy_pass

# These two lines behave differently, and the difference matters
location /api/ {
    proxy_pass http://127.0.0.1:3000;
}

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

When the address in proxy_pass has no path after the port, NGINX forwards the request path unchanged: /api/users arrives at the app as /api/users. When the address ends with a slash, NGINX replaces the part of the path that matched the location, so /api/users arrives at the app as /users. Neither is wrong on its own. The mistake is using one when the app expects the other, and getting 404s on every request under the prefix. Pick the form that matches what the app's routes expect, and keep it consistent.

Proxying to localhost instead of 127.0.0.1

localhost can resolve to an IPv6 address on some machines, and an app listening only on IPv4 then refuses the connection. 127.0.0.1 always means IPv4 on the local machine, and it is the safer address in a proxy configuration. Use the numeric address unless the app is known to listen on IPv6 as well.

Final Complete Example

Here is the complete configuration for a production-shaped setup: plain requests redirected to HTTPS, TLS terminated at the proxy, headers forwarded, and the app reached on its local port.

# /etc/nginx/sites-available/axum-app
upstream axum_app {
    server 127.0.0.1:3000;
}

server {
    listen 80;
    server_name example.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

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

    location /ws {
        proxy_pass http://axum_app;
        proxy_http_version 1.1;
        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;
    }
}

Enable it and check it:

sudo ln -s /etc/nginx/sites-available/axum-app /etc/nginx/sites-enabled/axum-app
sudo nginx -t
sudo systemctl reload nginx
curl https://example.com/
curl https://example.com/health

The first curl returns the app's home page through the proxy, encrypted. The second returns the health answer through the same proxy. The Axum server never sees a browser, never holds a certificate, and never knows the public internet exists. It only answers requests from NGINX, which is all it was ever asked to do.

Summary

The decision framework this page leaves behind is a question to ask about every request the app receives: where did it really come from, and how did it arrive? A proxy that forwards the request without the headers leaves the app blind, and a blind app cannot log honestly, limit fairly, or build correct links. The four headers are the proxy's answer to that question, and they cost four lines. Everything else, the upstream block, the TLS termination, the WebSocket upgrade, is the proxy doing jobs the app should not have to do: facing the public internet, managing certificates, and spreading load. An app behind a well-configured proxy is simpler than an app that faces the internet alone, and that simplicity is the reason the proxy exists.