nginx as a reverse proxy for an app: proxy_pass, X-Forwarded-* headers and timeouts

  1. Goal
  2. What a reverse proxy is and why to use one
  3. Preparation
  4. Steps
  5. Step 1: write the configuration file
  6. Step 2: enable the configuration and reload
  7. Step 3: adjust timeouts
  8. Step 4: WebSocket (if the application needs it)
  9. Verification
  10. Common errors
  11. Summary
  12. References

Goal

After this article you can set up nginx on Ubuntu 24.04 LTS (package nginx 1.24.x) in front of an application listening on 127.0.0.1:3000, pass the real user address and protocol to the application, and adjust timeouts when the application answers slowly.

What a reverse proxy is and why to use one

Web applications written in Node.js, Python or Go usually open their own HTTP port such as 3000 or 8080.

You could expose that port straight to the Internet, but putting nginx in front has several benefits: a single port 80 for many applications (told apart by domain name),

the application only needs to listen on 127.0.0.1 so it cannot be reached directly from outside, and nginx takes on heavy chores such as compression,

caching and rate limiting (see Compression,static caching and rate limiting with nginx limit_req).

TLS certificates and HTTPS belong to another planet, so this article uses plain HTTP only.

Preparation

On Ubuntu, nginx reads /etc/nginx/nginx.conf, which includes every file in /etc/nginx/sites-enabled/.

The familiar convention is to write the file in sites-available and then create a symbolic link into sites-enabled.

Steps

Steps

Step 1: write the configuration file

Create the file /etc/nginx/sites-available/app.example.com:

server {
    listen 80;
    listen [::]:80;
    server_name app.example.com;

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

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

What each part does:

Security note: the application should only trust these headers when the request comes from an nginx you control.

Users can send a fake X-Forwarded-For themselves, so configure the application to trust only the proxy at 127.0.0.1.

Step 2: enable the configuration and reload

sudo ln -s /etc/nginx/sites-available/app.example.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

nginx -t checks the syntax and only prints a result; it changes nothing on the running server.

Only reload once it reports syntax is ok and test is successful. reload loads the new configuration without dropping existing connections.

If you are not yet comfortable with systemctl, read systemctl on Ubuntu 24.04: enabling, disabling and restarting services and checking their status.

Step 3: adjust timeouts

Three timeouts affect the connection from nginx to the application, each 60 seconds by default:

location / {
    proxy_pass http://127.0.0.1:3000;
    proxy_connect_timeout 5s;   # time to wait for the connection to be established
    proxy_send_timeout    60s;  # wait between two successive writes of data sent out
    proxy_read_timeout    60s;  # wait between two successive reads of data coming back
}

The last two do not measure the total time of the request; they measure the pause between two successive writes or reads.

If the application takes longer than proxy_read_timeout without sending a single byte, nginx closes the connection and returns a 504 error to the user.

When you see a 504, first find out why the application is slow; raise proxy_read_timeout only for a single location that really does heavy work (such as exporting a report), not globally.

Step 4: WebSocket (if the application needs it)

For WebSocket, nginx must use HTTP/1.1 and pass the Upgrade and Connection headers:

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

Verification

curl -I -H "Host: app.example.com" http://127.0.0.1/
sudo tail -n 5 /var/log/nginx/access.log
sudo tail -n 5 /var/log/nginx/error.log

The curl command simulates the domain name with a Host header, so you do not need to change DNS when testing on the same machine.

If your application prints the headers it receives, you will see X-Forwarded-For carrying the client address.

To check that the application really listens on port 3000, use ss -ltn as in Checking network connectivity on Ubuntu with ip, ss, ping and tracepath.

Common errors

Summary

A reverse proxy lets the application hide behind nginx.

Three things to remember: proxy_pass to point the way, the Host and X-Forwarded-* headers so the application knows the original request, and the timeouts to handle slow applications.

When you have several copies of the application, the next step is Load balancing with nginx: upstream, balancing methods and passive health checks.

References

nginx as a reverse proxy for an app: proxy_pass, X-Forwarded-* headers and timeouts