Skip to content
Sniptide

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

Files

example.com.confRaw
# 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;
    }
}

Save with your agent

Run this command to add Sniptide to Claude Code in the current project. Open /mcp in Claude Code and sign in to Sniptide. Once the server shows as connected, use the prompt below to save a private copy in your library.

Terminal
claude mcp add --scope local --transport http sniptide https://app.sniptide.com/api/mcp
Prompt
Read https://sniptide.com/templates/nginx-reverse-proxy.json and use create_snippet to save its files as a private Sniptide snippet titled "Nginx reverse proxy config". Keep the file names and contents from the JSON.

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:

    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.

    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

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

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

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 does the same in a few lines.

All templates
  • Caddy
    Docker and deployCaddyfile reverse proxy

    A Caddyfile that reverse proxies a domain to your app with automatic HTTPS, compression and security headers, plus Docker, path and load balancing variants.

  • Docker and deployNext.js Dockerfile

    A multi-stage Next.js Dockerfile that builds the standalone output into a small image running as a non-root user, with .dockerignore and variants.

  • Docker and deployPython Dockerfile with uv

    A multi-stage Python Dockerfile that installs dependencies with uv from a lockfile, caches them between builds and runs the app as a non-root user.

Pick where your library lives.

Use sniptide.com

Save your first pattern in the app, then connect Claude Code, Cursor or Codex with one sign-in in the browser.

Run it on your own server

One image and a database give you the app, the API and the MCP endpoint. Sniptide is open source under AGPL-3.0.

$docker compose up -d