Generate, format, and validate high-performance Nginx reverse proxy, SSL/TLS, and caching configuration blocks.
Nginx is an asynchronous, event-driven, high-concurrency web server, reverse proxy, load balancer, and HTTP cache powering a significant portion of top internet websites. It handles thousands of simultaneous client connections with minimal memory overhead.
Nginx configuration files control critical production routing, SSL/TLS termination, HTTP/2 and HTTP/3 multiplexing, gzip/brotli compression, rate limiting, and CORS headers. Syntax errors or missing semicolons will cause `nginx -t` validation to fail and abort server reloads. Formatting and auditing Nginx server blocks ensures security and optimal network throughput.
An Nginx configuration is structured into hierarchical contexts: `http`, `server`, `location`, and `upstream`. Essential security directives include disabling server version tokens (`server_tokens off;`), enforcing modern TLS ciphers (`ssl_protocols TLSv1.2 TLSv1.3;`), setting HSTS headers, and proxying HTTP requests upstream with preserved client IP addresses (`X-Forwarded-For`, `X-Real-IP`).
server {
listen 443 ssl http2;
server_name api.example.com;
ssl_certificate /etc/letsencrypt/live/api.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/api.example.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
server_tokens off;
client_max_body_size 25M;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
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;
}
}
Run `sudo nginx -t`. If the syntax check passes, apply changes without dropping active connections by running `sudo systemctl reload nginx`.
Add `proxy_http_version 1.1;`, `proxy_set_header Upgrade $http_upgrade;`, and `proxy_set_header Connection "upgrade";` inside your location block.