Nginx Config Generator: Build Production-Ready Server Blocks
Nginx (pronounced “engine-x”) is a high-performance web server, reverse proxy, and load balancer used by some of the highest-traffic sites on the internet. Unlike Apache’s process-per-request model, Nginx uses an event-driven, asynchronous architecture that serves thousands of simultaneous connections with minimal memory.
A server block is the Nginx equivalent of Apache’s virtual host — it defines how Nginx responds to requests for a given domain or IP. You can run multiple server blocks in one Nginx instance, each serving a different site or application.
The core Nginx server block anatomy. Add listen 443 ssl http2; and ssl_certificate directives for HTTPS.
Reverse Proxy vs Static Site Config
A static site config sets a root directory and lets Nginx serve files directly — ideal for React builds or documentation sites. A reverse proxy config uses proxy_pass to forward requests to a backend process (Node.js, Gunicorn, Spring).
The reverse proxy handles SSL termination and request buffering so your app doesn’t have to. You can mix both: serve /static/ assets directly and proxy /api/ to your backend in one server block.
Basic Reverse-Proxy Server Block
A minimal working config that forwards all traffic to a local process:
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Host $host;
}
}Key directives: listen (port), server_name (domain), location (URL routing), proxy_pass (upstream URL), gzip on (compression).
Enabling HTTPS with Certbot
The simplest path to HTTPS is Certbot (Let’s Encrypt). Run certbot --nginx -d example.com and it edits your server block automatically — adding listen 443 ssl, ssl_certificate, ssl_certificate_key, and an HTTP→HTTPS redirect.
Certificates are free and auto-renewed every 90 days. For manual config, point ssl_certificate at your .pem file and ssl_certificate_key at your private key. Use ssl_protocols TLSv1.2 TLSv1.3; to drop legacy TLS versions.
Security Headers: HSTS, X-Frame-Options, CSP
Add these inside your server block to harden your site:
• Strict-Transport-Security (HSTS) tells browsers to only connect over HTTPS for the specified duration.
• X-Frame-Options: DENY prevents clickjacking by blocking your page from loading in iframes.
• Content-Security-Policy restricts which scripts and resources the browser can load.
• X-Content-Type-Options: nosniff stops MIME-type sniffing attacks.
Toggle “HTTPS On” in the generator above — all four headers are included automatically.
Frequently Asked Questions
What is Nginx?
Nginx (pronounced 'engine-x') is a high-performance open-source web server, reverse proxy, load balancer, and HTTP cache. It handles many concurrent connections with low memory use via an event-driven, asynchronous architecture.
What is the difference between Nginx and Apache?
Apache uses a process/thread-per-request model that consumes more memory under high concurrency. Nginx uses a non-blocking event loop handling thousands of connections with minimal memory. Nginx excels at serving static files and acting as a reverse proxy; Apache's .htaccess and module ecosystem remain popular for shared hosting.
What is a reverse proxy?
A reverse proxy sits in front of one or more backend servers and forwards client requests to them. This lets Nginx handle SSL termination, caching, and load balancing without changing your app server (Node.js, Python, Java, etc.).
How do I enable HTTPS in Nginx?
Add a server block listening on port 443 with ssl, then point ssl_certificate and ssl_certificate_key at your cert files. The easiest route is Certbot (Let's Encrypt): run certbot --nginx -d example.com and it configures everything automatically with free, auto-renewed certificates.
What is the location directive?
The location block matches URL paths inside a server block and defines how Nginx handles those requests. You can use exact matches (= /path), prefix matches (/api/), and regex matches (~* \.php$). Nginx picks the most specific matching location.
How do I serve a React or Next.js app with Nginx?
For a static React build, set root to your build output directory and add try_files $uri /index.html; so client-side routing works. For a Next.js Node.js server, use proxy_pass http://localhost:3000; in a location / block so Nginx forwards all requests to Next.
What is an upstream block in Nginx?
The upstream block defines a pool of backend servers for load balancing. Nginx distributes requests across listed servers using round-robin by default. You can add weights, set max_fails, and pick algorithms like least_conn or ip_hash for sticky sessions.