introduction
During stress testing, Nginx with self-built CDN nodes may experience a "Too many open files" error. This usually means that the number of files Nginx tries to open exceeds the system's limits, which is particularly noticeable in high concurrency situations. This article will explain in detail how to identify and resolve this problem to ensure that Nginx can still operate properly when loads are high.
What is ulimit
ulimit
is a command used to restrict system users' access to shell resources. Its main function is to control the number of resources that a single user or process can use, and prevent system crashes or performance degradation due to excessive resource usage. During stress testing, Nginx needs to handle a large number of concurrent requests, which leads to a sharp increase in the number of open file descriptors.
Specifically,ulimit
The following resources can be limited:
- Number of open file descriptors (nofile)
- Maximum number of processes (nproc)
- Memory usage (memlock)
These limitations ensure that each user or process does not over-consuming system resources.
View current limits
To view the current user's file opening restrictions, you can enter the following command in the terminal:
ulimit -n
This command will return the maximum number of files that can be opened by the current user, and the default value is 1024. This value is likely to become a bottleneck when stress testing, because both Nginx's worker process and client connections may exceed this limit.
Modify ulimit limits
Temporary modification
Before you perform a stress test, you can temporarily modify the number of open files (e.g. set to 65535):
ulimit -n 65535
This modification only takes effect in the current terminal session and will become invalid after closing the terminal. Therefore, for temporary testing needs, this approach is a quick solution.
Permanent modification
In order for the changes to remain valid after the system restarts, editing is required/etc/security/
document. The following configurations can be added at the bottom of the file:
* soft nofile 65535 * hard nofile 65535
-
*
It means it is effective for all users. -
soft nofile
Soft restrictions are defined, and users can modify them without requiring elevated permissions. -
hard nofile
Defines a hard limit, indicating that the maximum limit set by the administrator and cannot be exceeded by the user.
After saving the file, execute againulimit -n
Command to confirm that the modification has taken effect.
Modify Nginx configuration
To ensure that Nginx can use the new open file limit, you also need to modify Nginx's configuration file. existAdd the following line to:
worker_rlimit_nofile 65535;
-
worker_rlimit_nofile
Directives allow Nginx's worker processes to increase the limit on the number of open files. This means that the worker process is able to handle more file descriptors without restarting the main process.
After modifying the configuration, please use the following command to overload the Nginx configuration:
nginx -s reload
This will make the changes effective immediately.
Summarize
Through the above steps, you have successfully modified itulimit
Related restrictions and Nginx, thus solving the "Too many open files" error that occurs in stress tests. This will ensure that Nginx can handle more concurrent connections, improving service stability and performance. It is recommended to check system settings before conducting high load testing to ensure they adapt to changing needs.
The above is the detailed content of the solution to the "Too many open files" problem in Nginx. For more information about Nginx Too many open files, please follow my other related articles!