📋 Understanding NGINX Backlog
The backlog in the context of NGINX refers to the maximum length of the queue of pending connections waiting to be accepted by the server. This is a critical parameter for handling high-traffic scenarios and ensuring optimal server performance.
🎯 How Backlog Works
Connection Flow Diagram
⚙️ Configuration Location
The backlog setting is specified in the listen directive within the server block. An
important consideration with backlog (like reuseport) is that in SSL configurations, socket
options can only be defined once per listening socket.
📝 Step-by-Step Configuration
Step 1: Navigate to NGINX Configuration Directory
cd /etc/nginx/sites-available/Step 2: Open Your First Site's NGINX Server Block File
Open your site configuration file using your preferred text editor:
sudo nano your-site.confStep 3: Add Backlog to Port 80 Listen Directive
Add the backlog option to the HTTP listen directive:
listen 80 backlog=2048;Step 4: Add Backlog to Port 443 Listen Directive
Add the backlog option to the HTTPS listen directive:
listen 443 ssl backlog=2048;Step 5: Save and Test Configuration
Test the NGINX configuration for syntax errors:
sudo nginx -tStep 6: Reload NGINX
Reload NGINX to enable the new configuration:
sudo systemctl reload nginx📊 Recommended Backlog Values
| Server Load | Recommended Value | Use Case |
|---|---|---|
| Low to Medium | 2048 | Standard websites, small to medium traffic |
| High Traffic | 4096 | Busy sites with significant concurrent connections |
| Enterprise Level | 8192+ | Very high traffic applications with thousands of concurrent users |
🔍 Complete Configuration Example
Here's a complete example of an NGINX server block with backlog configured:
server {
listen 80 backlog=2048;
listen [::]:80 backlog=2048;
listen 443 ssl backlog=2048;
listen [::]:443 ssl backlog=2048;
server_name example.com www.example.com;
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/key.pem;
root /var/www/example.com/public_html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
}
}
✅ Key Configuration Rules
Important Points to Remember:
- Single Configuration: The backlog option is only added to ONE of your site's listen sockets (typically your first site).
- Automatic Application: Once set on the first site, it will be automatically applied by NGINX to all other sites on the same port.
- No Redundancy Needed: When creating a second, third, or fourth site, you do NOT need to add backlog again — it's inherited from the first configuration.
- Per-Socket Configuration: Backlog is configured per listening socket, not per server block.
🎭 Multiple Sites Configuration
First Site (with backlog):
server {
listen 80 backlog=2048;
listen 443 ssl backlog=2048;
server_name site1.com;
# ... rest of configuration
}
Second Site (no backlog needed):
server {
listen 80;
listen 443 ssl;
server_name site2.com;
# ... rest of configuration
}
Third Site (no backlog needed):
server {
listen 80;
listen 443 ssl;
server_name site3.com;
# ... rest of configuration
}
🔧 Troubleshooting
Check Current Configuration
sudo nginx -T | grep backlogView Active Connections
netstat -an | grep :80 | wc -lMonitor NGINX Logs
sudo tail -f /var/log/nginx/error.log📈 Performance Monitoring
After implementing the backlog configuration, monitor your server's performance to ensure optimal settings:
- Connection Queue Length: Monitor for connection queue saturation
- Response Times: Check if response times improve under high load
- Connection Rejections: Watch for rejected connections in logs
- CPU and Memory Usage: Ensure resource utilization remains acceptable
🎓 Best Practices
- Start Conservative: Begin with a backlog of 2048 and increase only if needed
- Monitor Performance: Regularly check server metrics and logs
- System Limits: Ensure your system's
net.core.somaxconnis set appropriately - Test Changes: Always test configuration changes in a staging environment first
- Document Settings: Keep records of why specific values were chosen
- Regular Reviews: Periodically review and adjust based on traffic patterns
⚡ System-Level Configuration
For the backlog setting to be effective, ensure your system's socket backlog limit is properly configured:
Check Current System Limit
sysctl net.core.somaxconnSet System Limit (Temporary)
sudo sysctl -w net.core.somaxconn=4096Set System Limit (Permanent)
sudo nano /etc/sysctl.confAdd the following line:
net.core.somaxconn=4096Apply the changes:
sudo sysctl -p📚 Summary
Configuring NGINX backlog is a straightforward process that can significantly improve your server's ability to handle high-traffic scenarios. The key points to remember are:
- Backlog defines the maximum queue length for pending connections
- Configure backlog only once per listening socket (in your first site)
- Recommended starting value is 2048 for most applications
- Increase to 4096 or higher for high-traffic sites
- Backlog is NOT compatible with QUIC protocol
- Always test configuration before reloading NGINX
- Monitor server performance after changes