# Nginx reverse proxy config

> This Nginx config sends traffic for your domain to an app on 127.0.0.1:3000 over HTTPS, redirects HTTP and www to the bare domain, and passes WebSocket upgrades through. Replace example.com with your domain, get a certificate with Certbot, then save the file in /etc/nginx/conf.d and reload Nginx.

Updated November 30, 2026. Canonical: https://sniptide.com/templates/nginx-reverse-proxy
Open it in the Sniptide editor: https://app.sniptide.com/new?template=nginx-reverse-proxy
Files as JSON: https://sniptide.com/templates/nginx-reverse-proxy.json

## Files

### example.com.conf

```
# Reverse proxy for an app listening on 127.0.0.1:3000, with HTTPS from Let's Encrypt.
# Save as /etc/nginx/conf.d/example.com.conf and replace example.com with your domain.

# Sends Connection: upgrade only when the client asks for a WebSocket.
map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

upstream app {
    server 127.0.0.1:3000;
    keepalive 32;
}

# Redirect HTTP to HTTPS, but keep the path Certbot uses to renew certificates.
server {
    listen 80;
    listen [::]:80;
    server_name example.com www.example.com;

    location /.well-known/acme-challenge/ {
        root /var/www/certbot;
    }

    location / {
        return 301 https://example.com$request_uri;
    }
}

# Redirect www to the bare domain.
server {
    listen 443 ssl;
    listen [::]:443 ssl;
    http2 on;
    server_name www.example.com;

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

    return 301 https://example.com$request_uri;
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    http2 on;
    server_name example.com;

    ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_protocols       TLSv1.2 TLSv1.3;
    ssl_session_cache   shared:SSL:10m;

    add_header Strict-Transport-Security "max-age=31536000" always;

    client_max_body_size 10m;

    location / {
        proxy_pass http://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;
        proxy_set_header Upgrade           $http_upgrade;
        proxy_set_header Connection        $connection_upgrade;

        proxy_read_timeout 60s;
    }
}
```

## Set it up [#set-it-up]

You need a Linux server running Nginx 1.25.1 or later, your app listening on `127.0.0.1:3000`, ports 80 and 443 open, and DNS records for `example.com` and `www.example.com` pointing at the server. Replace every `example.com` in the file with your domain.

Nginx won't start with a certificate path that doesn't exist yet, so get the certificate first with only the HTTP part in place:

1. Save the first `server` block (the one with `listen 80`) on its own as `/etc/nginx/conf.d/example.com.conf`, together with the `map` and `upstream` blocks.
2. Create the challenge folder and reload Nginx:

   ```bash title="Terminal"
   sudo mkdir -p /var/www/certbot
   sudo nginx -t && sudo systemctl reload nginx
   ```

3. Request the certificate. Certbot reloads Nginx after every renewal with the deploy hook.

   ```bash title="Terminal"
   sudo certbot certonly --webroot -w /var/www/certbot \
     -d example.com -d www.example.com \
     --deploy-hook "systemctl reload nginx"
   ```

4. Replace the file with the full config, then run `sudo nginx -t && sudo systemctl reload nginx` again.

Open `https://example.com` and check that the app loads with a valid certificate, and that `http://www.example.com` redirects to it. Run `sudo certbot renew --dry-run` to confirm that renewal works.

## What each block does [#what-each-block-does]

| Block | What it does | When to change it |
| --- | --- | --- |
| `map $http_upgrade` | Sets the `Connection` header to `upgrade` for WebSocket requests and `close` otherwise. | Rarely. |
| `upstream app` | The app's address, with idle connections kept open for reuse. | Change the port, or add more `server` lines to balance across several app processes. |
| Port 80 server | Serves Certbot's renewal challenge and redirects everything else to HTTPS. | Rarely. |
| www server | Redirects `www.example.com` to the bare domain. | Swap the names to make `www` the main address, or delete it if you don't use `www`. |
| Main server | Terminates TLS and forwards requests to the app. | See the next table. |

| Line | What it does |
| --- | --- |
| `http2 on` | Turns on HTTP/2. On Nginx older than 1.25.1, remove it and write `listen 443 ssl http2` instead. |
| `Strict-Transport-Security` | Tells browsers to use HTTPS for this domain for a year. Add it only once HTTPS works, because browsers remember it. |
| `client_max_body_size` | The largest request body Nginx accepts. Raise it for file uploads. |
| `proxy_set_header Host` and `X-Forwarded-*` | Tell the app the original host, client IP and protocol. Configure your framework to trust the proxy so redirects and secure cookies use `https`. |
| `proxy_read_timeout` | How long Nginx waits for a response, and how long an idle WebSocket stays open. Raise it for long requests or quiet WebSockets. |

## Variant: an app in Docker Compose [#variant-an-app-in-docker-compose]

When Nginx runs in the same Compose project as the app, point the upstream at the service name instead of localhost:

```nginx title="example.com.conf"
upstream app {
    server app:3000;
}
```

The app then needs no published port. For a simpler setup with automatic certificates, the [Caddyfile reverse proxy](/templates/caddyfile-reverse-proxy) does the same in a few lines.
