nginx.conf Best Practices

Distribution defaults for nginx.conf are conservative. A few main and http settings improve throughput and remove information leaks before you write a single server block.

Main and events

worker_processes auto matches CPU cores. worker_connections sets the per-worker limit.

worker_processes auto;
events {
    worker_connections 4096;
}

http block defaults

Turn off server_tokens, enable sendfile and tcp_nopush, set keepalive_timeout and a log format you can parse.

http {
    server_tokens off;
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 30s;
}

Open the nginx.conf generator

Frequently asked questions

How many worker_connections do I need?

Total clients is roughly worker_processes times worker_connections, halved when proxying because each client uses two connections. Also check the open-files limit.

Guides