Nginx configuration uses a block-based syntax where directives are organized into contexts: http, server, and location. This reference covers the most common nginx config patterns with ready-to-paste snippets.
No snippets match your search.
How to Use This Nginx Configuration Reference
Nginx configuration files follow a hierarchical block structure. The main config (/etc/nginx/nginx.conf) includes site configs from /etc/nginx/sites-enabled/. Each site gets its own server block file. After editing, test with nginx -t and reload with systemctl reload nginx.
Step 1: Start with a Basic Server Block
Every nginx site needs a server block with listen, server_name, and a root location. Copy the "Basic HTTP server block" snippet, replace example.com with your domain and /var/www/html with your document root.
Step 2: Add SSL with Let's Encrypt
Run certbot --nginx -d example.com to automatically obtain and configure SSL. Certbot modifies your server block to add SSL directives and creates an HTTP-to-HTTPS redirect. Review the "SSL/HTTPS server block" snippet for the pattern certbot produces.
Step 3: Configure Reverse Proxy for Node.js or Python
For apps running on localhost ports, use the reverse proxy snippet: proxy_pass http://127.0.0.1:3000; inside a location block. Add the proxy headers (X-Real-IP, X-Forwarded-For) so your app sees the real client IP.
Step 4: Enable Gzip and Security Headers
Add gzip compression in the http block to reduce transfer sizes by 60-80% for text assets. Add security headers (X-Frame-Options, X-Content-Type-Options, Content-Security-Policy) to improve your security posture without any application changes.
Frequently Asked Questions
Is this nginx configuration cheatsheet free?
Yes, completely free with no signup required. All snippets are copyable.
What is a server block in nginx?
A server block (equivalent to Apache's VirtualHost) defines how nginx handles requests for a specific domain or IP address. It contains listen directives, server_name, root path, and location blocks that match specific URL patterns.
How do I redirect HTTP to HTTPS in nginx?
Add a separate server block that listens on port 80 and redirects everything: server { listen 80; server_name example.com; return 301 https://$host$request_uri; }. The 301 is a permanent redirect that browsers and search engines will cache.
What is proxy_pass used for in nginx?
proxy_pass forwards requests to a backend application server (Node.js, Python, PHP-FPM, etc.). Example: proxy_pass http://127.0.0.1:3000; inside a location block sends matching requests to your app on port 3000.
How does nginx differ from Apache?
Nginx uses an event-driven, non-blocking architecture that handles thousands of connections with minimal memory. Apache uses a process/thread-per-request model. Nginx excels at serving static files and as a reverse proxy; Apache is more feature-rich with .htaccess support.