DEV Community

楊東霖
楊東霖

Posted on • Originally published at devplaybook.cc

Nginx Config Generator: Stop Writing Server Blocks by Hand

Nginx Config Generator: Stop Writing Server Blocks by Hand

Nginx configuration is powerful but verbose. A simple reverse proxy with SSL, compression, and security headers is 60+ lines of config that looks identical across every project. An Nginx config generator handles the boilerplate so you can focus on the parameters that actually differ.

What Is an Nginx Config Generator?

An Nginx config generator takes your inputs — domain name, upstream server, SSL settings, caching rules — and outputs a valid nginx.conf or site config file. Instead of remembering the exact syntax for proxy_pass, ssl_protocols, or gzip_types, you fill in a form and get production-ready config.

The DevPlaybook Nginx Config Generator supports:

  • Reverse proxy with upstream health checks
  • SSL/TLS with modern cipher suites
  • Static file serving with cache headers
  • Load balancing (round-robin, least-conn, IP hash)
  • Security headers (HSTS, X-Frame-Options, CSP)
  • Gzip/Brotli compression

Common Nginx Configurations You'll Need

Basic Reverse Proxy

The most common Nginx use case: forward traffic from port 80/443 to an app running locally.

server {
    listen 80;
    server_name api.example.com;

    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}
Enter fullscreen mode Exit fullscreen mode

Getting this right requires remembering proxy_http_version, Upgrade header for WebSockets, and proxy_cache_bypass. A generator produces this correctly every time.

SSL Termination with Let's Encrypt

server {
    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-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384;
    ssl_prefer_server_ciphers off;

    add_header Strict-Transport-Security "max-age=63072000" always;

    location / {
        proxy_pass http://localhost:3000;
    }
}

server {
    listen 80;
    server_name example.com;
    return 301 https://$server_name$request_uri;
}
Enter fullscreen mode Exit fullscreen mode

SSL config in 2026 means TLS 1.2+, modern ciphers, and an HTTP → HTTPS redirect. Generators update these defaults as best practices evolve.

Load Balancer Configuration

upstream backend {
    least_conn;
    server 10.0.0.1:3000 weight=3;
    server 10.0.0.2:3000 weight=1;
    server 10.0.0.3:3000 backup;
}

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://backend;
        proxy_next_upstream error timeout invalid_header http_500;
    }
}
Enter fullscreen mode Exit fullscreen mode

Load balancing configuration involves upstream groups, health check directives, and failover rules. Getting the proxy_next_upstream directive right is easy to forget.

Security Headers Worth Adding

Every production Nginx config should include security headers:

add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Content-Security-Policy "default-src 'self'" always;
add_header Permissions-Policy "geolocation=(), camera=(), microphone=()" always;
Enter fullscreen mode Exit fullscreen mode

A generator can include these as a group with a checkbox — "Add security headers" — without you having to remember each directive.

Performance Optimization

Gzip Compression

gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_proxied any;
gzip_comp_level 6;
gzip_types
    text/plain
    text/css
    text/xml
    text/javascript
    application/javascript
    application/json
    application/xml
    image/svg+xml;
Enter fullscreen mode Exit fullscreen mode

The gzip_types list is the part people forget or get wrong — JSON and SVG aren't compressed by default.

Static File Caching

location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2)$ {
    expires 1y;
    add_header Cache-Control "public, immutable";
    access_log off;
}
Enter fullscreen mode Exit fullscreen mode

Setting far-future expires for hashed static assets is standard for any modern frontend. The immutable directive tells browsers not to revalidate during the expiry period.

Rate Limiting

limit_req_zone $binary_remote_addr zone=api:10m rate=100r/m;

server {
    location /api/ {
        limit_req zone=api burst=20 nodelay;
        limit_req_status 429;
        proxy_pass http://localhost:3000;
    }
}
Enter fullscreen mode Exit fullscreen mode

Rate limiting requires the limit_req_zone directive in the http block and limit_req in location blocks. A generator handles this context correctly.

Nginx vs Apache: When to Use Which

Use Nginx when:

  • High concurrency (thousands of simultaneous connections)
  • Reverse proxy / load balancer role
  • Static file serving
  • Memory is constrained

Use Apache when:

  • You need .htaccess per-directory config
  • Complex per-user virtual hosting
  • Legacy PHP apps using mod_php

Most modern stacks default to Nginx. When in doubt, use Nginx.

Testing Your Config

Before reloading Nginx, always test the config:

# Test config syntax
sudo nginx -t

# Reload without dropping connections
sudo nginx -s reload

# Show currently loaded config
sudo nginx -T
Enter fullscreen mode Exit fullscreen mode

The -T flag dumps the entire processed config, useful for debugging include files.

Common Mistakes

Trailing slash in proxy_pass: proxy_pass http://localhost:3000/; vs proxy_pass http://localhost:3000; — with a trailing slash, Nginx strips the location prefix. Without it, it preserves it. This causes unexpected URL behavior.

Missing HTTP/1.1 for WebSockets: WebSocket upgrades require proxy_http_version 1.1 and the Connection: upgrade header. Without these, WebSocket connections fail silently.

Wildcard in server_name: server_name *.example.com only matches one level of subdomain. api.v1.example.com won't match — you need a regex: server_name ~^(.+)\.example\.com$.

How to Use the Generator

  1. Open the Nginx Config Generator
  2. Select your use case: reverse proxy, static site, load balancer
  3. Enter domain name and upstream server details
  4. Toggle security headers, SSL, compression
  5. Copy the generated config

The generator produces commented output explaining each directive, which helps you understand what to customize later.

Validating Your Configuration

Generated config is a starting point, not a final product. After generating:

  1. Review the server_name and proxy_pass values
  2. Verify SSL certificate paths match your actual certs
  3. Test with nginx -t before deploying
  4. Check the actual HTTP headers with HTTP Headers Inspector

Summary

Nginx config generators eliminate the repetitive parts of server configuration. The syntax for reverse proxies, SSL, and load balancers is well-established — you shouldn't memorize it, you should generate it. Focus on the parameters that vary per project and let the generator handle the structure.

For more infrastructure tools, see Docker Compose Generator and GitHub Actions Generator.


Level Up Your Dev Workflow

Found this useful? Explore DevPlaybook — cheat sheets, tool comparisons, and hands-on guides for modern developers.

🛒 Get the DevToolkit Starter Kit on Gumroad — 40+ browser-based dev tools, source code + deployment guide included.

Top comments (0)