0
0
NginxHow-ToBeginner · 4 min read

How to Optimize Nginx Performance: Tips and Configuration

To optimize nginx performance, configure worker_processes to match CPU cores, enable gzip compression, and use caching with proxy_cache. Also, tune connection handling with keepalive_timeout and optimize buffer sizes for faster response.
📐

Syntax

The main directives to optimize Nginx performance include:

  • worker_processes: Number of worker processes, usually set to CPU cores.
  • worker_connections: Max simultaneous connections per worker.
  • gzip: Enables compression to reduce response size.
  • proxy_cache: Caches responses to speed up repeated requests.
  • keepalive_timeout: Controls how long connections stay open.
  • client_body_buffer_size and client_header_buffer_size: Buffer sizes for client requests.
nginx
worker_processes auto;
worker_connections 1024;
gzip on;
gzip_types text/plain application/json text/css application/javascript;
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m use_temp_path=off;
keepalive_timeout 65;
client_body_buffer_size 16k;
client_header_buffer_size 1k;
💻

Example

This example shows a basic Nginx configuration optimized for performance by setting worker processes, enabling gzip compression, and configuring proxy caching.

nginx
worker_processes auto;
worker_connections 2048;

http {
    gzip on;
    gzip_types text/plain application/json text/css application/javascript;

    proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=500m inactive=30m use_temp_path=off;

    server {
        listen 80;

        location / {
            proxy_pass http://backend_server;
            proxy_cache my_cache;
            proxy_cache_valid 200 10m;
            proxy_cache_use_stale error timeout updating;
            proxy_cache_background_update on;
            proxy_cache_lock on;
        }
    }
}

keepalive_timeout 65;
client_body_buffer_size 16k;
client_header_buffer_size 1k;
⚠️

Common Pitfalls

Common mistakes when optimizing Nginx include:

  • Setting worker_processes too low or too high, causing underutilization or overhead.
  • Not enabling gzip or compressing unsupported content types.
  • Ignoring caching configuration, leading to repeated backend requests.
  • Setting keepalive_timeout too low, causing frequent connection reopenings, or too high, wasting resources.
  • Using default buffer sizes that are too small for your traffic, causing slowdowns.

Example of a wrong and right way to set worker_processes:

nginx
# Wrong: fixed low number
worker_processes 1;

# Right: automatic based on CPU cores
worker_processes auto;
📊

Quick Reference

Summary tips to optimize Nginx performance:

  • Set worker_processes to auto for CPU core matching.
  • Increase worker_connections for high traffic.
  • Enable gzip compression for text-based content.
  • Use proxy_cache to reduce backend load.
  • Tune keepalive_timeout to balance resource use and latency.
  • Adjust buffer sizes to fit your request sizes.

Key Takeaways

Set worker_processes to auto to match CPU cores for efficient parallelism.
Enable gzip compression to reduce response size and speed up delivery.
Use proxy_cache to cache backend responses and reduce load.
Tune keepalive_timeout to keep connections open without wasting resources.
Adjust buffer sizes to handle client requests smoothly.