Operations 7 min read

Nginx Performance Optimization Techniques

To keep Nginx fast under heavy load, adjust worker processes and connections to match CPU cores, enable gzip compression, set browser and proxy caching headers, turn on sendfile with optimal chunk and TCP settings, tune log levels, and optionally use a CDN and HTTP/2.

Java Tech Enthusiast
Java Tech Enthusiast
Java Tech Enthusiast
Nginx Performance Optimization Techniques

Nginx is a high‑performance web server widely used on the Internet. Under high concurrency and traffic, tuning is essential to maintain efficiency.

This article introduces several optimization methods.

1. Adjust worker processes and connections

The number of worker processes is set by the worker_processes directive. Example:

worker_processes 4;

The maximum connections per worker are controlled by worker_connections :

worker_connections 1024;

Choose values that match the server’s CPU cores and expected load.

2. Enable Gzip compression

Install the gzip module when compiling Nginx, then add the following to the http block:

http {
...
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
...
}

3. Configure caching strategies

Set browser cache headers using Expires and Cache‑Control :

location ~* \.(jpg|jpeg|gif|png|css|js)$ {
add_header Cache-Control "public, max-age=31536000";
}

For reverse‑proxy caching, use proxy_cache_valid :

location / {
proxy_pass http://backend;
proxy_cache mycache;
proxy_cache_valid 200 302 60m;
proxy_cache_valid 404 1m;
}

4. Optimize file delivery

Enable sendfile to transfer files directly from disk to network:

http {
...
sendfile on;
...
}

Adjust sendfile_max_chunk and tcp_nopush for better throughput:

http {
...
sendfile_max_chunk 1m;
tcp_nopush on;
...
}

5. Tune logging

Set an appropriate log_level , e.g., info :

http {
...
log_level info;
...
}

Configure log rotation and compression to reduce disk usage.

6. Additional recommendations

Use a CDN to cache static assets and enable HTTP/2 for multiplexed, efficient transfers:

server {
listen 443 ssl http2;
...
}
optimizationConfigurationPerformance TuningNginxweb server
Java Tech Enthusiast
Written by

Java Tech Enthusiast

Sharing computer programming language knowledge, focusing on Java fundamentals, data structures, related tools, Spring Cloud, IntelliJ IDEA... Book giveaways, red‑packet rewards and other perks await!

0 followers
Reader feedback

How this landed with the community

login 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.