SoFunction
Updated on 2025-04-11

Project practice for implementing high concurrency in Nginx

Nginx is a lightweight web and reverse proxy server known for its high performance, stability, rich feature set, simple configuration, and low resource consumption. Nginx can effectively handle high concurrent connections, thanks to its event-driven architecture. Here are some strategies and best practices to optimize Nginx for high concurrent connection processing capabilities.

Using the latest stable version of Nginx

Always use the latest stable version of Nginx as each new version may introduce performance improvements and security fixes.

Reasonably configure worker processes (workers)

Nginx will automatically set the number of worker processes by default, which usually matches the number of CPU cores of the server. You canIn the fileworker_processesConfigure in the command.

worker_processes auto; # Automatically set according to the number of CPU cores

Configure the number of worker processes connections (worker_connections)

The maximum number of connections that each worker can establish isworker_connectionsCommand control. This value is usually set to 1024 or higher, but should be less than or equal to the maximum open file limit allowed by the operating system.

events {
    worker_connections 4096;
}

Use epoll or kqueue

For Linux systems, useepollAs a connection processing method. For BSD systems, usekqueue

events {
    use epoll;
}

Enable multi-threading support

Starting with Nginx 1.9.13, thread pools can be enabled to improve performance.

events {
    worker_connections 4096;
    multi_accept on;
    use epoll;
}

thread_pool default threads=16 max_queue=65536;

Adjust TCP/IP and kernel parameters

By adjusting kernel parameters, the network stack performance can be improved, such as increasing the buffer size of TCP connections:

sysctl -w net.ipv4.tcp_fin_timeout=30
sysctl -w =4096
sysctl -w net.ipv4.tcp_max_syn_backlog=20480
sysctl -w .netdev_max_backlog=50000
sysctl -w .rmem_max=16777216
sysctl -w .wmem_max=16777216
sysctl -w net.ipv4.tcp_rmem='4096 12582912 16777216'
sysctl -w net.ipv4.tcp_wmem='4096 12582912 16777216'

Turn on keepalive

passkeepaliveInstructions can reduce overhead because they allow multiple requests to be transmitted in one connection.

keepalive_timeout 65;

Use the quick turn-on option (tcp_nodelay and tcp_nopush)

These options reduce latency and increase network throughput:

http {
    tcp_nodelay on;
    tcp_nopush on;
}

Static file processing optimization

When using Nginx as a static file server, make sure to properly set cache-related header information to reduce unnecessary file I/O.

location ~* .(jpg|jpeg|gif|png|css|js|ico|html)$ {
    expires max;
    log_not_found off;
}

Reduce logging

Logging has an impact on performance, so adjust the log level as needed or turn off unnecessary logging completely.

access_log off;
error_log /var/log/nginx/ crit;

SSL/TLS optimization

If using SSL/TLS, using session caching and tickets can improve the efficiency of handshake:

ssl_session_cache shared:SSL:10m;
ssl_session_tickets on;

Using load balancing

If running on multiple servers, configuring Nginx's load balancing feature can better handle high traffic and ensure service stability.

upstream backend {
    server ;
    server ;
}

Directly optimize at the operating system level

In addition to optimizing on Nginx configuration, it should also be ensured that the operating system itself is correctly configured for high concurrency, including file descriptor limitations and network stack configuration.

By applying the above strategies, Nginx's concurrent processing capabilities can be greatly improved, so as to maintain stable and fast service response when facing high traffic. Continuous monitoring and tuning of the system will help keep server performance optimized.

This is the end of this article about Nginx's project practice to achieve high concurrency. For more related content about Nginx's high concurrency, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!