Generate nginx or Caddy configuration for a reverse proxy, static site, single-page app or load balancer, with the forwarding headers and caching rules right.
nginx · Reverse proxy · 77 lines
The nginx inspector finds no errors in this.
Forwards requests to one application, preserving the client's address and scheme.
nginx sets no forwarding headers by default, so the generated configuration sets all four — without them the upstream sees nginx as the client and nginx’s own name as the Host, which breaks URL generation, rate limiting and audit logs at once. Caddy sets them itself and passes the original Host through, so its output sets none and says so rather than implying they were needed.
# example.com — generated nginx configuration. Run `nginx -t` before reloading;
# nothing here has been validated by nginx.
# Plain HTTP exists only to redirect. Serving the real site on both is how
# a session cookie ends up travelling in clear text.
server {
listen 80;
listen [::]:80;
server_name example.com;
# ACME HTTP-01 challenges must stay reachable over plain HTTP.
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
location / {
return 301 https://$host$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;
# Stated explicitly so an nginx upgrade cannot change what is offered.
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers off;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;
ssl_session_tickets off;
server_tokens off;
# Note: add_header does NOT merge across levels. A location that sets
# any add_header of its own replaces this entire set, so these must be
# repeated in any location that adds a header.
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
gzip on;
gzip_vary on;
# Below about 1 KB the compressed form is often larger than the original.
gzip_min_length 1024;
gzip_types text/plain text/css text/xml application/json application/javascript application/xml+rss image/svg+xml;
# A request body larger than this is rejected with a 413 before the
# upstream sees it. The default is 1 MB.
client_max_body_size 10m;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
# Without these the upstream sees nginx as the client and nginx's own
# name as the Host.
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 X-Forwarded-Host $host;
# An empty Connection header is what allows upstream keepalive.
proxy_set_header Connection "";
proxy_connect_timeout 5s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
}
}
What the nginx inspector says
A WebSocket upgrade is half-configured
Both headers are needed: `proxy_set_header Upgrade $http_upgrade;` and `proxy_set_header Connection "upgrade";`, plus `proxy_http_version 1.1`. With one of the three missing the handshake fails, usually with a 400 from the upstream.
proxy_pass has no trailing slash while the location does
With no path on the proxy_pass target, nginx passes the full original URI through — so `/api/users` reaches the upstream as `/api/users`. With a trailing slash it strips the location prefix, and the same request arrives as `/users`. Both are valid; which you want depends on whether the upstream expects the prefix.
Nothing here has been loaded by a web server. Run nginx -t or caddy validate before reloading — neither this nor its inspector knows the full directive table, and only the server itself can tell you a directive is invalid in the context it appears in. The certificate paths in the nginx output assume a Let’s Encrypt layout and will need changing if yours differs.
Web Server Configuration Generator runs entirely on your device — nothing is reloaded and no origin is contacted.
You’re on the Free plan. Compare plans
The nginx output sets every forwarding header, because nginx sets none of them by default: without `proxy_set_header`, the upstream sees nginx's own name as the Host and nginx's address as the client. That breaks absolute URL generation, rate limiting and audit logs simultaneously, and it is the single most common thing missing from a hand-written reverse proxy.
The Caddy output sets none of them, and says why. Caddy sets X-Forwarded-For, X-Forwarded-Proto and X-Forwarded-Host itself and passes the original Host through, so the common case needs no configuration at all. Emitting them anyway would imply they were required.
Two things it gets right that are easy to get wrong. The nginx rate-limit zone is declared at the http level rather than inside a server block, because `limit_req_zone` is only valid there — a configuration with it in the wrong place fails to load. And Caddy's rate limiting is emitted commented out, because it is not in the standard build: an uncommented unknown directive stops Caddy starting entirely.
The single-page-app role refuses to cache index.html while making hashed assets immutable for a year. Cache both and a deploy leaves browsers asking for asset filenames that no longer exist, which presents as a blank page for exactly as long as the cache lives.
The generated nginx is run through the nginx inspector on the page. The Caddy output is not, because there is no Caddyfile parser here — claiming to have checked it would be the fake validation this project avoids, so it says to run `caddy validate` instead.
Because that is the honest difference. nginx sends its own name as the Host and its own address as the client unless you tell it otherwise; Caddy sets the forwarding headers itself and passes the Host through. Emitting them for Caddy would suggest they were needed.
Rate limiting is not in the standard Caddy build — it needs a plugin compiled in with xcaddy. An uncommented directive Caddy does not recognise stops it starting, so shipping it live would break your server rather than rate-limit it.
Because it names your hashed assets. Cache it and a browser keeps asking for filenames that the new deploy no longer has, which is a blank page for as long as the cache lives. The hashed assets themselves are immutable and cached for a year, which is the whole point of hashing them.
Read it first and then validate it. The certificate paths assume a Let's Encrypt layout, nothing here has been loaded by a web server, and neither this nor its inspector knows nginx's full directive table. `nginx -t` or `caddy validate` is the step before a reload.