🛠️ Developer & Web

Nginx Config Generator

A production-ready server block in a few clicks.

  • 4site types
  • 3www options
  • HTTPSCertbot certificate paths
  • .confone-click download

    

How to use the Nginx Config Generator

  1. 1

    Enter your domain

    Type example.com; a pasted https://, www. or path is stripped.

  2. 2

    Pick the site type

    Static, single-page app, reverse proxy or PHP-FPM. Fields that do not apply to the chosen type are dimmed.

  3. 3

    Choose www handling and options

    Redirect to non-www or www, or serve both; then HTTPS, gzip, asset caching, security headers, WebSockets and upload size.

  4. 4

    Download, test and reload

    Save the .conf, run sudo nginx -t, then sudo systemctl reload nginx.

Features

Correct redirects

HTTP → HTTPS and www ↔ non-www as separate 301 server blocks.

Let’s Encrypt ready

Certificate, key, options file and DH parameters at Certbot’s standard paths.

HTTP/2

Uses the http2 on; directive, with a comment for older nginx versions.

Gzip and caching

Compression for text types and a one-year cache rule for static assets.

Security headers

nosniff, SAMEORIGIN framing, a referrer policy and HSTS on HTTPS.

Hidden files blocked

Requests for dotfiles like .git or .env are denied, except .well-known.

Who uses it — and for what

VPS owners

Set up a site on a fresh Ubuntu or Debian server.

React and Vue apps

Serve a built SPA so page refreshes do not 404.

Node and Python apps

Put nginx in front of Express, Next.js, Django or FastAPI.

PHP sites

Run WordPress or Laravel through PHP-FPM.

Where the config goes

On Ubuntu and Debian, save the file as /etc/nginx/sites-available/example.com, link it with sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/, test with sudo nginx -t and apply with sudo systemctl reload nginx. On other systems use /etc/nginx/conf.d/example.com.conf. With HTTPS ticked, the config expects certificates at the standard Certbot paths — run sudo certbot --nginx -d example.com -d www.example.com to obtain them.

Choosing a site type

  • Static — plain HTML/CSS/JS files served directly; returns 404 for missing files.
  • SPA — routes every unknown path to index.html so client-side routing works on refresh.
  • Reverse proxy — forwards requests to an app on a local port (Node, Next.js, Django, FastAPI) with the right headers.
  • PHP-FPM — passes .php files to PHP; check your PHP version’s socket path.

What every generated directive does

DirectivePurpose
listen 443 ssl; / listen [::]:443 ssl;Accept HTTPS on IPv4 and IPv6 (port 80 versions without HTTPS).
http2 on;Enable HTTP/2; this directive exists from nginx 1.25.1. On older versions delete it and use listen 443 ssl http2; as the comment says.
server_nameWhich host names this block answers.
return 301 …$request_uriPermanent redirect that keeps the path and query string.
client_max_body_size 20M;Largest request body (uploads); larger ones get 413.
gzip_*Compress text, CSS, JS, JSON, XML, RSS and SVG responses over 256 bytes at level 5; gzip_vary adds Vary: Accept-Encoding.
root / indexFolder to serve and default file (index.php first for PHP).
expires 1y + Cache-Control "public, immutable"Long browser caching for CSS, JS, images and fonts, without access logging.
try_filesOrder in which nginx looks for a file before falling back.
fastcgi_pass + snippets/fastcgi-php.confHand .php requests to PHP-FPM using Debian/Ubuntu’s bundled snippet.
proxy_*Forward to your app and pass the real host, client IP and scheme.
location ~ /\.(?!well-known)Deny dotfiles while allowing ACME challenges.

Gotchas worth knowing before you deploy

  • Certificates must exist first. With HTTPS ticked, nginx -t fails if /etc/letsencrypt/live/<domain>/ is missing. Either generate the config with HTTPS off, run Certbot, then switch to the HTTPS version, or obtain the certificate with Certbot’s standalone or webroot mode first. Both domain names (with and without www) need to be on the certificate because the redirect block for the other name also listens on 443.
  • Long cache plus unchanging file names is a trap. immutable with one year is right for fingerprinted files like app.3f9a1c.js that Vite, webpack and most frameworks produce. If you edit style.css in place, visitors may keep the old copy; untick caching or rename files when they change.
  • add_header inheritance. The asset location adds its own Cache-Control header, and in nginx a location with any add_header does not inherit the server-level ones. Static assets therefore will not carry the security headers — usually harmless for images and CSS, but worth knowing if a scanner complains.
  • HSTS is sticky. The header (one year, without includeSubDomains or preload) tells browsers to use HTTPS only. Make sure HTTPS works reliably before deploying it.
  • Proxy apps should listen on 127.0.0.1 so they cannot be reached directly from the internet, and should trust X-Forwarded-For only from nginx (for example Express’s trust proxy).

Adapting the output

The generator writes one complete file per site, so extra features go in by hand. Common additions: a location /api/ block with its own proxy_pass in an SPA config, gzip_types entries for formats you serve such as application/wasm, or rate limiting with limit_req. For PHP-FPM, check the real socket with ls /run/php/ — the default assumes PHP 8.3. On distributions without snippets/fastcgi-php.conf (some RHEL-based systems), replace that include with explicit fastcgi_param lines. If your host uses Apache instead, the .htaccess generator covers similar redirects and headers.

Worked examples

Reverse proxy for a Node app on port 3000

With HTTPS on, non-www canonical and WebSocket support ticked, the main block’s location is:

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 "upgrade";
    proxy_read_timeout 60s;
}

Above it come two redirect blocks: port 80 for both names returning 301 https://example.com$request_uri, and a 443 block for www.example.com doing the same.

Single-page app fallback

Choosing “Single-page app” writes try_files $uri $uri/ /index.html; in location /. A refresh on /dashboard/settings finds no such file, so nginx serves index.html and your router takes over. The static type instead ends with =404.

Pro tips

  • Always run sudo nginx -t before reloading — a typo in one site file can stop every site.
  • Use reload, not restart, so open connections finish cleanly.
  • Check redirects with curl -I http://www.example.com and follow the Location header.

Glossary

Server block
nginx’s equivalent of a virtual host: settings for one or more domain names.
Reverse proxy
A server that receives requests and forwards them to an application behind it.
PHP-FPM
The FastCGI process manager that runs PHP for web servers like nginx.
HSTS
Strict-Transport-Security — a header telling browsers to use HTTPS only for a period.

Frequently asked questions

How do I test the config?

Run sudo nginx -t — it reports syntax errors before you reload.

Why do I get “413 Request Entity Too Large”?

Increase client_max_body_size (the upload size option).

Does it include HTTP/2?

Yes, the HTTPS server block enables HTTP/2.

Why do I get 502 Bad Gateway with the proxy config?

nginx cannot reach your app. Check that it is running and listening on the same port as proxy_pass, and look at /var/log/nginx/error.log.

Can I use this on shared hosting?

Usually not. Shared hosts rarely let you edit nginx files; most use Apache with .htaccess instead.

Does it set up the SSL certificate?

No. It points to where Certbot puts certificates; you still run Certbot to obtain and renew them.

Found it useful? Share the Nginx Config Generator:

Last updated 2026-11-28 · Built by Vinayak Agarwal · Part of Developer & Web (77 tools)