“NGINX 102 Baby” guide focused on advanced settings—request size limits, timeouts, and related tweaks—without rehashing the basics covered in NGINX 101.
NGINX 102 Baby – Advanced Tuning
1. Request Size Limits
Increase the allowed request body size (e.g., for large file uploads):
# Global setting for all servers and locations.
# This applies unless overridden in a more specific block.
http {
client_max_body_size 50m; # Default for all servers and locations is 50 MB.
server {
listen 80;
server_name example.com;
# Server-level setting: Overrides the http-level setting for this server.
# All locations under this server will use 100 MB unless further overridden.
client_max_body_size 100m; # Now, for this server, maximum body size is 100 MB.
location / {
proxy_pass http://localhost:3000;
# Inherits the server-level setting (100 MB) by default.
# Uncomment the next line to override it for this location only.
# client_max_body_size 10m; # For this location, maximum body size would be 10 MB.
}
}
}
2. Timeout Settings
Client-Side Timeouts
Set timeouts to avoid hanging connections:
server {
client_body_timeout 60s;
client_header_timeout 60s;
keepalive_timeout 65s;
send_timeout 60s;
# other settings...
}
Upstream/Proxy Timeouts
Ensure long-running backend responses are handled properly:
location /api/ {
proxy_pass http://backend_server;
proxy_connect_timeout 600s;
proxy_send_timeout 600s;
proxy_read_timeout 600s;
}
Note: Suffix s
makes units explicit (seconds).
3. Additional Performance Tweaks
Proxy Buffering
For handling large responses efficiently:
location /data/ {
proxy_pass http://backend_server;
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
}
Gzip Compression
Reduce bandwidth with simple gzip settings:
http {
gzip on;
gzip_types text/plain text/css application/json application/javascript;
}
Connection & Rate Limiting
Prevent abuse with simple limits:
http {
# Define a memory zone named "addr" (10MB) for connection limiting, keyed by client IP.
limit_conn_zone $binary_remote_addr zone=addr:10m;
# Define a memory zone named "one" (10MB) for rate limiting, with a limit of 5 requests per second per client.
limit_req_zone $binary_remote_addr zone=one:10m rate=5r/s;
server {
location / {
# Limit each client to a maximum of 10 simultaneous connections.
limit_conn addr 10;
}
location /api/ {
# Limit clients to 5 requests per second, allow bursts up to 10, processing bursts without delay.
limit_req zone=one burst=10 nodelay;
}
}
}
4. Testing & Reloading
Always verify your configuration before reloading:
sudo nginx -t && sudo systemctl reload nginx
Top comments (0)