Sub topic 14
Tuning Nginx Workers, Compression, and Caching
This chapter assumes that you can edit Nginx configuration files, use sudo, and reload a running Nginx service. You should also understand HTTP requests and responses. Make a backup and test every configuration change before reloading Nginx.
Nginx performance depends on three related choices:
- Worker processes determine how much CPU work Nginx can perform in parallel.
- Compression reduces response size but uses CPU time.
- Caching avoids repeated work by reusing responses.
Choosing worker settings
Nginx uses a master process and one or more worker processes. The master reads configuration and manages workers. Workers accept connections and process requests.
The events context controls connection handling. The http context controls HTTP behavior. A common starting point in /etc/nginx/nginx.conf is:
worker_processes auto;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
}worker_processes auto usually creates one worker per available CPU core. This is a sensible default for a general-purpose server. A small server may need only one worker, while a heavily loaded server may need testing before choosing a different value.
worker_connections is the maximum number of connections handled by each worker. The approximate connection capacity is:
worker_processes × worker_connections
This is only an estimate. File descriptor limits, upstream connections, TLS, and keep-alive connections can reduce the usable capacity. Raising this value without raising the operating system's open-file limit may have no effect.
Compressing responses
Compression reduces bandwidth usage for text-based responses such as HTML, CSS, JavaScript, JSON, and XML. It does not usually help already-compressed formats such as JPEG, PNG, MP4, or ZIP files.
Add compression in the http context:
http {
gzip on;
gzip_vary on;
gzip_min_length 1000;
gzip_comp_level 5;
gzip_types
text/plain
text/css
application/javascript
application/json
application/xml
image/svg+xml;
}gzip_vary on adds a Vary: Accept-Encoding response header. Caches can then keep compressed and uncompressed versions separate. A compression level from 1 to 9 trades CPU time for a smaller response. Level 5 is a reasonable starting point.
Caching static files
Browsers can cache static files when Nginx sends an Expires or Cache-Control header. Add long caching only to files whose names change when their contents change, such as app.abc123.js.
server {
listen 80;
server_name example.com;
root /var/www/example.com/html;
location /assets/ {
try_files $uri =404;
expires 7d;
add_header Cache-Control "public, max-age=604800";
}
location / {
try_files $uri $uri/ =404;
}
}Do not apply a long browser cache lifetime to HTML that changes frequently. A stale HTML document can refer to old assets or hide a newly deployed version.
| Setting | Main benefit | Main cost or risk |
|---|---|---|
| More workers | Uses multiple CPU cores | More processes and memory |
| Gzip compression | Smaller text responses | More CPU usage |
| Browser caching | Fewer repeat requests | Stale content |
| Proxy caching | Less application work | Cache invalidation complexity |
A safe tuning workflow
Measure before changing settings. Check CPU, memory, response time, bandwidth, and error rates. Change one setting at a time, then compare results under a representative load.
The request path can be viewed like this:
Exercise
A server has four CPU cores and uses worker_processes auto with worker_connections 1024. Is 4096 guaranteed simultaneous client requests?
Show answer
No. It is only an approximate connection limit. File descriptor limits, keep-alive connections, upstream connections, TLS, and application capacity can reduce the practical number.