1001Ferramentas
🌐 Dev

NGINX server block Generator

Build an NGINX server block: server_name, listen, root, index, location / try_files, gzip and logs. Starter config ready.

An nginx server block for static sites

Fresh server, files already in /var/www, and the only thing missing is the block nginx will read. It is always the same shape, yet a semicolon goes missing or try_files comes out wrong. Fill in server_name, port, root and index, decide whether you want gzip, and the block appears complete with access_log, error_log and a root location. The Copy button drops it on your clipboard.

One thing about the gzip directive: nginx always compresses text/html, which is why that type never appears in gzip_types and adding it changes nothing. The list covers everything else. Watch JavaScript in particular: the output lists application/javascript, but depending on the version nginx serves .js as text/javascript, which then falls outside the list. Check with curl -H "Accept-Encoding: gzip" -I and adjust.

Save it in /etc/nginx/sites-available with a symlink in sites-enabled (Debian and Ubuntu) or straight into /etc/nginx/conf.d/your-site.conf, then run nginx -t before reloading. The generated try_files ends in =404, which is right for a static site; for an SPA with client-side routing, replace =404 with /index.html. No TLS comes out of this: typing 443 ssl in the listen field produces a block with no ssl_certificate and nginx -t will reject it.

Frequently asked questions

Does it work for a React or Vue site?
It does, with one change: in the root location, swap =404 in try_files for /index.html, otherwise any route opened directly from the URL bar returns a 404.
How do I add HTTPS to this config?
Generate the block on port 80, get it working, then run certbot --nginx. It inserts the listen 443 ssl line, the certificate paths and the redirect from port 80 for you.
Can I keep www and non-www in the same server_name?
You can, and both will serve the same content. For SEO the usual advice is a 301 redirect from one to the other, and that second block is not generated here.

Related Tools