Boost Nginx Performance: 10 Proven Configuration Tweaks

This guide explains ten practical Nginx optimizations—including event‑driven handling, worker process tuning, gzip compression, static and dynamic caching, DNS resolver settings, TCP_NODELAY, logging adjustments, request rate limiting, and HTTP/2 activation—to dramatically improve server throughput and latency.

Architect Chen
Architect Chen
Architect Chen
Boost Nginx Performance: 10 Proven Configuration Tweaks

1. Event‑driven model

Nginx processes requests using an event‑driven architecture instead of spawning a process or thread per connection, allowing it to handle massive concurrency with minimal resource consumption.

2. Optimize worker processes

The worker_processes directive defines how many worker processes Nginx starts. Setting it to auto (or the number of CPU cores) fully utilizes the server’s CPU resources. The worker_connections directive controls the maximum concurrent connections per worker; a typical value is 1024 or higher.

worker_processes auto;
worker_connections 1024;

3. Enable gzip compression

Gzip reduces the amount of data sent to clients, speeding up page loads. Common settings include:

gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml+rss;
gzip_comp_level 5;
gzip_buffers 16 8k;
gzip_vary on;
gzip_proxied any;

4. Static file caching

Configure appropriate HTTP cache headers so browsers cache static assets (images, CSS, JS, fonts) for a long period, reducing repeat requests.

location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff|woff2|eot|ttf)$ {
    expires 30d;
    add_header Cache-Control "public, no-transform";
}

5. Cache dynamic content

Use fastcgi_cache or proxy_cache to store responses from back‑end applications (e.g., PHP‑FPM, Node.js). Define a cache zone, key, and validity period.

http {
    fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m inactive=60m;
    fastcgi_cache_key "$scheme$request_method$host$request_uri";
}
server {
    location ~ \.php$ {
        fastcgi_cache my_cache;
        fastcgi_cache_valid 200 60m;
        fastcgi_pass 127.0.0.1:9000;
    }
}

6. Reduce DNS lookup time

If upstream servers are referenced by domain name, Nginx resolves them at start‑up. Use the resolver directive with a reliable DNS server (e.g., 8.8.8.8) to avoid repeated lookups.

location / {
    resolver 8.8.8.8;
    proxy_pass http://backend_server;
}

7. Enable TCP_NODELAY

Setting tcp_nodelay on forces packets to be sent immediately instead of waiting to fill buffers, which is beneficial for low‑latency applications.

http {
    tcp_nodelay on;
}

8. Optimize logging

Logging incurs I/O overhead. Disable unnecessary access logs or buffer them in memory.

http {
    access_log off;               # disable access log
    # or
    # access_log /var/log/nginx/access.log buffer=32k;
}

9. Rate limiting

Use the limit_req_zone and limit_req modules to cap request rates per IP, protecting against abuse.

http {
    limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
}
server {
    location /login/ {
        limit_req zone=one burst=5;
    }
}

10. Use HTTP/2

HTTP/2 provides multiplexing, server push, and header compression. Enable it by adding the http2 flag to the listen directive.

server {
    listen 443 ssl http2;
    # ... other settings ...
}
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendOptimizationconfiguration
Architect Chen
Written by

Architect Chen

Sharing over a decade of architecture experience from Baidu, Alibaba, and Tencent.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.