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 did Nginx suddenly become "exhausted"?There are many reasons that may cause the problem, don't be afraid, let's find out 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, the sudden surge in Nginx load is not due to your operational errors, but because of too much traffic.Highly concurrent access requests can overload Nginx's worker process, each process is overloaded, and the CPU and memory are also full. At this time, you may need to improve server resources, such as increasing the number of CPU cores or increasing memory. Of course, this is also a reminder: don't forget to consider CDN diversion or load balancing.
3. Encountered malicious attacks
Being popular on the Internet is not always a good thing. Malicious attacks may target you at any time. If you find that the CPU usage is abnormally high and the request IP source is suspicious, it is very likely that your website is under DDoS attack.You need to deploy a firewall or limit access frequency immediately, for example, using Nginx's built-in current 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.
If it is set too long, it will cause too much connection resources to be occupied.
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 The system has a limit on the number of file descriptors that can be opened per process.
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 cache function, static resources (such as images and JS files) can be cached in memory, thus greatly reducing the burden on the server.
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.
使用 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 through科学methods to solve the problem, and optimizing server performance is no exception.
Timely monitoring and adjustment, is the key to keeping Nginx running efficiently. If you can master these tips, your website will be safe even in the face of high traffic or malicious attacks.
I hope this article has inspired you. Go optimize your Nginx now!
Hope Chen Weiliang Blog ( https://www.chenweiliang.com/ )'s sharing of "What to do if Nginx server CPU load and process memory usage are high?" may be helpful to you.
Welcome to share the link of this article:https://www.chenweiliang.com/cwl-32093.html
To unlock more hidden tricks🔑, welcome to join our Telegram channel!
If you like it, please share and like it! Your sharing and likes are our continuous motivation!