Article directory
Have you ever suddenly discovered that the CPU usage of Nginx has skyrocketed during your work? What's worse is that every time you check the process, multiple worker processes of Nginx are consuming resources like crazy.
Seeing this scene, you may get excited and shout: "Oh my God, is the server going to explode?" Don't worry, this does not mean that your server is "finished", but Nginx needs you to do a comprehensive optimization!
Analysis of the causes of excessive Nginx load

First, we need to figure out why Nginx suddenly "lacks power". There are many reasons for this problem, so don't worry, let's explore them together.
1. Improper configuration
In the Nginx configuration file, the most important point is worker_processes. This parameter determines the number of processes started by Nginx.
- If you configure too few worker processes, the CPU load will soar; if you configure too many, you will run out of memory.
- You have to find a balance, like,Set worker_processes to 1 to 2 times the number of CPU cores.
- If you have 4 cores, try
worker_processes 4Or directly set it toauto.
2. The number of visits has increased dramatically
Sometimes, a sudden spike in Nginx load isn't due to operational errors, but rather an excessive volume of requests. High concurrency can overwhelm Nginx's worker processes , causing each process to work beyond its capacity, consuming CPU and memory. In this case, you might need to upgrade server resources, such as increasing the number of CPU cores or adding more memory. Of course, this also serves as a reminder: don't forget to consider CDN traffic distribution or load balancing.
3. Encountered malicious attacks
Being incredibly popular online isn't all good; malicious attacks can target you at any time. If you notice abnormally high CPU usage and suspicious IP addresses being requested, your website is very likely under a DDoS attack. In this case, you need to immediately deploy a firewall or limit access frequency , such as using Nginx's built-in rate limiting module or setting up an IP blacklist.
How to solve the problem of high memory usage of Nginx process?
So the question is, why does the Nginx worker process take up so many resources? We have to start with configuration and gradually optimize it.
Configuration method
Open the Nginx configuration file: Usually, the main configuration file of Nginx is located at
/etc/nginx/nginx.conf.Setting
worker_processes: Found in the configuration fileeventsBlock, setworker_processesIf there is noeventsblock, you need to create one.nginx events { worker_connections 1024; use epoll; # 或者适用于操作系统的其他事件模型 }
1. Reasonable setting of worker_connections
Nginx worker_connections The parameter determines the maximum number of connections that each worker process can handle. If it is too small, it will affect the concurrency performance, and if it is too large, it may consume too many resources.
How to calculate the appropriate value?
If you have a 4 core CPU, 16GB of RAM, a safe starting point is worker_connections 4096.
But if your website has a lot of traffic, consider increasing this value to 8192 to ensure that each process can handle enough requests.
events {
worker_connections 8192;
}
In this way, Nginx's processing power will be greatly improved.
2. Adjust keepalive_timeout
Another key parameter for Nginx to process requests is keepalive_timeout.
This setting determines how long the client's connection to the server can be maintained.
Setting the length too long can lead to excessive consumption of connection resources.
You can try keepalive_timeout Setting it to 15 seconds can both maintain the connection and release resources.
keepalive_timeout 15;
3. Optimizing file descriptor limits
By default, Linux systems limit the number of file descriptors that each process can open.
If Nginx needs to process a large number of files (such as static resources), you may see an Nginx error message saying "too many open files. "
You can pass worker_rlimit_nofile Increase the file descriptor limit, for example, set it to 65535.
worker_rlimit_nofile 65535;
4. Enable caching and gzip
In website performance optimization, caching and compression are two key points.
By enabling Nginx's caching feature, static resources (such as images and JS files) can be cached in memory , thereby greatly reducing the server load.
In addition, turning on the gzip compression function can reduce the amount of data transmitted and increase website speed.
gzip on;
gzip_types text/plain application/javascript;
5. Analyze Nginx resource usage
Finally, if you have done all the above optimizations and Nginx is still taking up a lot of CPU, you may need to use some tools to do in-depth analysis.
in Use top Or htop View real-time resource consumption of a process,by strace Tracing system calls, or using nmon Generate performance reports. Only by comprehensively analyzing the actual operation of Nginx can more accurate tuning be performed.
Final Thoughts
When Nginx's CPU usage spikes, don't panic. It may just be due to improper configuration or too much traffic.
Through reasonable adjustments worker_processes 和 worker_connectionsBy enabling caching, optimizing timeouts and file descriptors, etc., you can significantly reduce the load on Nginx.
Nginx is a powerful web server that, when properly optimized, can provide excellent performance for your website.
Remember, any problem can be solved scientifically, and optimizing server performance is no exception.
Timely monitoring and adjustments are key to maintaining the high efficiency of Nginx. If you master these techniques, your website will remain safe even under high traffic or malicious attacks.
I hope this article has inspired you. Go optimize your Nginx now!
Hopefully, the article "How to deal with high CPU load and high process memory usage on Nginx server?" shared on Chen Weiliang's blog ( https://www.chenweiliang.com/ ) will be helpful to you.
Feel free to share this article's link: https://www.chenweiliang.com/cwl-32093.html
