Nginx as a reverse proxy

Intermediate 12 min read

Put a web server in front of your application to handle HTTPS, static files and forwarding.

Why it matters

Application servers are not built to face the internet directly. A reverse proxy terminates HTTPS, serves static files fast, and forwards the rest to your app — and it is where many deployment errors (502, 504) show up first.

Where it fits

  1. Plan & Design
  2. Develop
  3. Version control
  4. Verify
  5. Build & Package
  6. CI/CD
  7. ● Deploy › Web server & release
  8. Operate

What you will learn

  • Explain what a reverse proxy does.
  • Read a server block that forwards requests to an app.
  • Find the cause of a 502 Bad Gateway.

Before you start

The request path

  1. Browser
  2. Nginx :443 (HTTPS)
  3. App :3000
  4. Database

A minimal server block

/etc/nginx/conf.d/app.conf
server {
    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-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}
In production add a listen 443 ssl block with a certificate; port 80 then only redirects to HTTPS.
Example
sudo nginx -t                # check the configuration
sudo systemctl reload nginx  # apply it without dropping connections

Tip: A 502 Bad Gateway almost always means Nginx is fine but the app behind it is not running, or listens on a different address or port. Check the app first, then the Nginx error log.

Install and set up

Official documentation and references

Documentation

Guides

Reference

Learn next

Done with this topic?

Reviewed 2026-10-23. Links go to the official documentation unless marked otherwise.