Loading introduction
Loading stats
Loading about
Loading posts
Loading article
Skip to content
Md Nayeem Hossain
Explorer
home.tsx
nginx-configuration-production-guide.md
nginx-configuration-production-guide.md
blog.json

// DevOps

Nginx Configuration for Production: Complete Guide

Configure Nginx for production with SSL, load balancing, caching, and security best practices.

Md Nayeem HossainDec 5, 202411 min read

Nginx Configuration for Production#

Nginx is the de facto reverse proxy for the web. It handles SSL termination, load balancing, and static files much better than Node.js can.

A Perfect Reverse Proxy Config#

Here is a production-ready block for proxying a Node.js app.

nginx
1server {2    listen 80;3    server_name example.com;4 5    # 1. Redirect HTTP to HTTPS6    return 301 https://$host$request_uri;7}8 9server {10    listen 443 ssl http2;11    server_name example.com;12 13    # SSL Certificates (Let's Encrypt usually handles these paths)14    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;15    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;16 17    # 2. Security Headers18    # Protect against clickjacking and XSS19    add_header X-Frame-Options "SAMEORIGIN";20    add_header X-XSS-Protection "1; mode=block";21    add_header X-Content-Type-Options "nosniff";22 23    location / {24        # 3. Proxy to Node.js25        proxy_pass http://localhost:3000;26 27        # 4. Forward Headers28        # Critical for your app to know the real IP of the user29        proxy_set_header Host $host;30        proxy_set_header X-Real-IP $remote_addr;31        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;32        proxy_set_header X-Forwarded-Proto $scheme;33    }34}

Performance Tuning: Gzip#

Enable compression to make your site load faster. It trades a tiny bit of CPU for massive bandwidth savings.

nginx
1gzip on;2gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;3gzip_min_length 1000; # Don't compress tiny files

Handling Rate Limiting#

Protect your API from abuse directly at the web server level.

nginx
1# Define the zone (10 requests per second per IP)2limit_req_zone $binary_remote_addr zone=mylimit:10m rate=10r/s;3 4server {5    location /api/ {6        # Apply the limit with a small "burst" allowance7        limit_req zone=mylimit burst=20 nodelay;8 9        proxy_pass http://localhost:3000;10    }11}

Nginx is the shield that protects your application server. Configuring it correctly is essential for security and performance.

#Nginx#DevOps#Server#Configuration
Newer REST API Design: Best Practices and Standards

Outline

  • Nginx Configuration for Production
  • A Perfect Reverse Proxy Config
  • Performance Tuning: Gzip
  • Handling Rate Limiting