Optimizing Performance
1. Setting Worker Processes
| Directive | Values | Description |
|---|---|---|
| worker_processes | auto, 1-32, or off |
Sets the number of worker processes. auto uses the number of CPU cores. |
Note: The number of worker processes should generally match the number of CPU cores for optimal performance.
2. Configuring Worker Connections
| Directive | Values | Description |
|---|---|---|
| worker_connections | 512 |
Maximum number of simultaneous connections that can be handled by a single worker process |
| multi_accept | on | off |
Allows a worker process to accept multiple connections at once (default: off) |
Example: Basic worker_connections configuration
events {
worker_connections 1024;
multi_accept on;
}
Note: The optimal value for
worker_connections depends on available system resources and expected traffic load. A higher value allows more concurrent connections but consumes more memory.
3. Enabling Sendfile
| Directive | Values | Description |
|---|---|---|
| sendfile | on | off |
Enables or disables the use of sendfile() system call to transfer data from file to socket. Default is off. |
Note: sendfile is particularly effective for serving static files and can significantly reduce CPU usage by minimizing data copying between kernel and user space.
4. Using TCP Optimizations
| Directive | Description | Values | Notes |
|---|---|---|---|
| tcp_nodelay | Enables or disables the TCP_NODELAY option | on | off |
Improves latency by disabling Nagle's algorithm |
| tcp_nopush | Enables or disables the TCP_NOPUSH option | on | off |
Improves throughput by delaying the first packet |
Example: Enabling TCP optimizations
server {
listen 80;
tcp_nodelay on;
tcp_nopush on;
location / {
proxy_pass http://backend;
}
}
Note: These optimizations are particularly useful for high-performance web servers and proxy setups.
5. Configuring Keepalive
| Directive | Values | Description |
|---|---|---|
| keepalive_timeout | 65s |
Timeout for keepalive connections. Default is 75s. |
| keepalive_requests | 100 |
Maximum number of requests per keepalive connection. Default is 100. |
| keepalive_disable | none |
Disables keepalive for specific user agents. Example: msie6. |
Example: Basic Keepalive Configuration
server {
listen 80;
server_name example.com;
keepalive_timeout 65;
keepalive_requests 100;
}
Example: Disabling Keepalive for Specific Browsers
server {
listen 80;
server_name example.com;
keepalive_disable msie6;
}
6. Setting Buffer Sizes
| Directive | Values | Description |
|---|---|---|
| client_body_buffer_size | 128k |
Buffer size for client request body. Default is 128k. |
| client_max_body_size | 1m |
Maximum allowed size of the client request body. Default is 1m. |
| client_body_temp_path | /var/cache/nginx/client_temp |
Path for temporary files when body is larger than client_body_buffer_size. |
| client_header_buffer_size | 32k |
Buffer size for reading client request headers. Default is 32k. |
| large_client_header_buffers | 4 8k |
Number and size of buffers for large client request headers. Default is 4 8k. |
Example: Configuring Buffer Sizes
http {
client_body_buffer_size 256k;
client_max_body_size 10m;
client_header_buffer_size 64k;
large_client_header_buffers 8 16k;
}
7. Enabling Open File Cache
| Directive | Values | Description |
|---|---|---|
| open_file_cache | off, on |
Enables or disables the open file cache. Default is off. |
| open_file_cache_valid | time |
Time during which a file entry is considered valid in the cache. Default is 60s. |
| open_file_cache_min_uses | number |
Minimum number of requests after which a file entry is added to the cache. Default is 1. |
| open_file_cache_errors | on, off |
Enables caching of errors for files. Default is off. |
| open_file_cache_use_stale | error, timeout, invalid, updating, expired |
Controls when a stale cached entry is used. Default is off. |
Example: Basic Open File Cache Configuration
http {
open_file_cache on;
open_file_cache_valid 1m;
open_file_cache_min_uses 2;
open_file_cache_errors on;
}
Note: The
open_file_cache directive is particularly useful for reducing disk I/O on high-traffic sites by caching file metadata.
8. Using Gzip Compression
| Directive | Values | Description |
|---|---|---|
| gzip | on | off |
Enables or disables gzip compression for responses. Default is off. |
| gzip_types | Content-Type values | Defines MIME types to compress. Default includes common text-based types like text/html, text/css, and application/javascript. |
| gzip_min_length | Integer (bytes) | Minimum response size to compress. Default is 20 bytes. |
| gzip_comp_level | 1 to 9 |
Compression level. Higher levels provide better compression but use more CPU. Default is 1. |
Example: Enabling Gzip Compression
server {
listen 80;
root /var/www/html;
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml;
gzip_min_length 1024;
gzip_comp_level 6;
location / {
try_files $uri $uri/ =404;
}
}
Note: Gzip compression is most effective for text-based content like HTML, CSS, and JavaScript files. Binary files such as images or videos are not compressed.
9. Implementing Caching
| Directive | Description | Example |
|---|---|---|
| proxy_cache | Defines a cache zone for proxy responses | proxy_cache my_cache; |
| proxy_cache_valid | Sets cache validity for different response codes | proxy_cache_valid 200 1h; |
| proxy_cache_key | Defines the cache key for requests | proxy_cache_key "$scheme$proxy_host$request_uri"; |
| proxy_cache_min_uses | Minimum number of requests before caching | proxy_cache_min_uses 2; |
| proxy_cache_use_stale | Controls when stale content is used | proxy_cache_use_stale error timeout updating http_500; |
| proxy_cache_lock | Prevents multiple requests for same cache item | proxy_cache_lock on; |
Example: Basic Caching Configuration
http {
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m use_temp_path=off;
server {
location /api/ {
proxy_cache my_cache;
proxy_cache_valid 200 1h;
proxy_cache_min_uses 2;
proxy_cache_use_stale error timeout updating http_500;
proxy_cache_lock on;
proxy_pass http://backend;
}
}
}
Example: Cache Busting with Versioning
server {
location ~* \.(css|js|png|jpg|jpeg|gif|ico)$ {
expires 1y;
add_header Cache-Control "public, immutable";
proxy_cache my_static_cache;
proxy_cache_valid 200 1y;
proxy_cache_key "$scheme$proxy_host$request_uri";
proxy_pass http://static_backend;
}
}
Note: Ensure
proxy_cache_path is defined in the http context, not inside a server block.
Warning: Caching static assets without versioning can lead to stale content being served. Always use cache-busting techniques for assets.
10. Optimizing SSL Performance
| Directive | Description | Values | Notes |
|---|---|---|---|
| ssl_protocols | Sets the SSL/TLS protocols that Nginx will support. | ssl_protocols SSLv2 SSLv3 TLSv1 TLSv1.1 TLSv1.2 TLSv1.3 |
It is recommended to enable only TLSv1.2 and TLSv1.3 for security. |
| ssl_ciphers | Specifies the list of ciphers to use for SSL connections. | ssl_ciphers ECDHE-RSA-AES256-GCM-SHA512 DHE-RSA-AES256-GCM-SHA512 ECDHE-RSA-AES256-GCM-SHA384 DHE-RIGA-AES256-GCM-SHA384 |
Use strong ciphers and avoid weak ones like RC4 or DES. |
| ssl_prefer_server_ciphers | Enables the server to choose the cipher suite. | on, off |
Recommended to set to on for better security. |
| ssl_session_cache | Configures the SSL session cache. | shared:SSL:10m, off |
Enables reuse of SSL sessions to reduce handshake overhead. |
| ssl_session_timeout | Sets the time during which a client can reuse an SSL session. | 5m, 10m |
Typically set to 5-10 minutes. |
Example: SSL Configuration
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/key.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers "ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384";
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 5m;
}
Note: Ensure that the SSL certificate and key are properly configured to avoid handshake errors.
11. Using HTTP/2
| Directive | Description | Default | Notes |
|---|---|---|---|
| http2 | Enables HTTP/2 support | off | Requires TLS (HTTPS) to be enabled |
| http2_max_field_size | Maximum size of HTTP/2 header fields | 4k |
Can be increased for large headers |
| http2_max_header_size | Maximum size of HTTP/2 header set | 16k |
Used to prevent header flood attacks |
| http2_idle_timeout | Idle timeout for HTTP/2 connections | 30s |
Controls how long idle connections are kept open |
| http2_max_requests | Maximum number of requests per HTTP/2 stream | 1000 |
Used to prevent resource exhaustion |
Example: Enabling HTTP/2 in Nginx
server {
listen 443 ssl http2;
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/key.pem;
location / {
proxy_pass http://backend;
}
}
Note: HTTP/2 must be used over TLS (HTTPS) to function properly.
Warning: Ensure that all backend services support HTTP/2 to avoid connection issues.
12. Configuring Worker Priority
| Directive | Values | Description |
|---|---|---|
| worker_priority | -19 to 20 |
Sets the scheduling priority of worker processes. Lower values mean higher priority. |
Note: Requires root privileges to set values outside the default range.