nginx Config Generator
Build a server block for a reverse proxy, static site, single-page app or PHP, with HTTPS, HTTP/2 and security headers, and get the install steps in the order that works on a fresh server.
Free · No signupBuilt in your browser
# example.com, generated by the CtrlOps nginx config generator
# Pass WebSocket upgrades through; send every other request as an ordinary one
map $http_upgrade $connection_upgrade_example_com {
default upgrade;
'' close;
}
# Send every HTTP request to HTTPS
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
server_tokens off;
return 301 https://example.com$request_uri;
}
# Send www.example.com to example.com
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name www.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_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305;
ssl_prefer_server_ciphers off;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;
ssl_session_tickets off;
server_tokens off;
return 301 https://example.com$request_uri;
}
# example.com
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
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_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305;
ssl_prefer_server_ciphers off;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;
ssl_session_tickets off;
add_header Strict-Transport-Security "max-age=63072000" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
server_tokens off;
client_max_body_size 20m;
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 5;
gzip_min_length 256;
gzip_types text/css text/plain text/xml application/javascript application/json application/xml application/rss+xml image/svg+xml font/ttf font/otf;
location / {
proxy_pass http://127.0.0.1:3000;
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_example_com;
# An idle WebSocket closes after 60 seconds otherwise
proxy_read_timeout 1h;
}
}
Save as /etc/nginx/sites-available/example.com, then link it into sites-enabled. The Install steps tab has the commands in the order that works.
HTTP/2 is turned on in the listen line, which every nginx accepts. nginx 1.25.1 and later still work but warn that it is deprecated; pick 1.25.1 or newer if nginx -v says so.
HSTS makes browsers use HTTPS for this host for two years, even if the certificate later breaks. Add it once HTTPS works.
Built in this tab as you type. Nothing you enter leaves this page.
From config to live
Deploy your app without the hand-written config.
CtrlOps deploys Node.js, Next.js and React apps from a desktop app: it sets up the server, nginx and SSL, then ships your code, with credentials encrypted on your own machine.
Keep it handy
nginx directives,
in one line each.
The directives the generated config uses, and what each one does.
| Directive | What it does |
|---|---|
listen | The port and protocol the server block accepts, e.g. 80 or 443 ssl. |
server_name | The domain(s) this block matches, e.g. example.com www.example.com. |
http2 on | Turns on HTTP/2 in nginx 1.25.1 and later; older versions write listen 443 ssl http2. |
root | The filesystem directory files are served from. |
location | Rules that apply to matching request paths. |
proxy_pass | Forwards a request to a backend (reverse proxy). |
try_files | Tries each path in turn, falling back to the last - the basis of SPA routing. |
return 301 | Issues a permanent redirect, e.g. HTTP to HTTPS. |
ssl_certificate | Path to the TLS certificate (with ssl_certificate_key for the key). |
add_header | Adds a response header (security, caching, HSTS). |
client_max_body_size | Largest request body nginx accepts (raise to fix 413 errors). |
How the config is built
One server block.
No silent surprises.
What is an nginx server block?
nginx matches each request to a server block by its listen port and server_name, then to a location inside it by path. The generator writes the main block for your site, plus a port 80 block that redirects to HTTPS and one that redirects www, when you turn those on.
Get the certificate before you test
nginx -t fails while the certificate files a config names do not exist, which is the usual first-day snag with Let's Encrypt. So the install steps run certbot certonly --nginx first, then save the config, link it into sites-enabled on Debian and Ubuntu, test, and reload with no downtime. Renewal then runs on its own timer.
HTTP/2 and your nginx version
nginx 1.25.1 replaced listen 443 ssl http2 with a separate http2 on;. The new directive is unknown to older versions, and Ubuntu 24.04 ships 1.24 and Debian 12 ships 1.22, so a config copied from a newer guide stops nginx -t there. Pick your version and the generator writes the form it accepts.
Headers that survive every location
An add_header inside a location drops every add_header from the server block for those requests, so a cache rule with its own header quietly removes HSTS and the security headers from your images and scripts. The cache rule here uses expires instead, and keeps them all. Dotfiles such as .git and .env are refused before any other rule can serve them.
Reverse proxy and WebSockets
The proxy forwards Host, X-Real-IP, X-Forwarded-For and X-Forwarded-Proto, so your app sees the real visitor and knows the page was HTTPS. With WebSockets on, a map passes the upgrade through only when the browser asks for one, and the read timeout is raised so an idle socket is not cut after 60 seconds. Point it at 127.0.0.1 rather than localhost, which nginx tries as IPv6 first.
nginx FAQ
nginx config,
explained.
Use a server block with a location / that has proxy_pass pointing at your app (e.g. http://localhost:3000), plus the forwarded headers (Host, X-Real-IP, X-Forwarded-For, X-Forwarded-Proto). Pick the Reverse proxy type in this generator, set the upstream, and copy the result. Enable WebSocket support if your app uses them.
Serve the built files from a root directory and add try_files $uri $uri/ /index.html; so any unknown path returns index.html and the client-side router takes over. Choose the SPA type in this generator to produce exactly that.
Add a server block listening on port 80 that does return 301 https://$host$request_uri;, and serve the real site on 443 with your certificate. Enable HTTPS and the HTTP-to-HTTPS redirect in this generator and it writes both blocks for you.
nginx limits request bodies to 1 MB by default. Add client_max_body_size with a higher value (for example 20m or 100m) inside the server or location block, then test and reload. Set the Max upload size field in this generator to add it.
The main file is /etc/nginx/nginx.conf; site configs typically live in /etc/nginx/sites-available/ and are enabled by symlinking into /etc/nginx/sites-enabled/. After editing, run sudo nginx -t to validate, then sudo systemctl reload nginx to apply with no downtime.
Pick one canonical host for SEO and redirect the other to it with a 301. This generator can redirect www to root or root to www automatically, emitting the extra redirect server block. Either choice is fine as long as you are consistent.
Keep working
Related tools for your next task.