Writing in the front

Recently, in the server built a set of pressure test environment, not for anything else, for the pressure test performance of Nginx, in the end there is no legendary so cow force! The operating environment is as follows: 11 VMS are installed with CentOS 6.8 64-bit OS, one vm is installed with Nginx deployed, and the other 10 VMS serve as clients to send requests to Nginx through threads that have overloaded the CPU to perform load testing on Nginx. Unexpectedly, there is a problem!!

Nginx error

The traffic of the Nginx server is very high, and the following error information is repeatedly displayed in the Nginx error log.

2020-07-23 02:53:49 [alert] 13576#0: accept() failed (24: Too many open files)
2020-07-23 02:53:49 [alert] 13576#0: accept() failed (24: Too many open files)
2020-07-23 02:53:49 [alert] 13576#0: accept() failed (24: Too many open files)
2020-07-23 02:53:49 [alert] 13576#0: accept() failed (24: Too many open files)
2020-07-23 02:53:49 [alert] 13576#0: accept() failed (24: Too many open files)
2020-07-23 02:53:49 [alert] 13576#0: accept() failed (24: Too many open files)
2020-07-23 02:53:49 [alert] 13576#0: accept() failed (24: Too many open files)
2020-07-23 02:53:49 [alert] 13576#0: accept() failed (24: Too many open files)
2020-07-23 02:53:49 [alert] 13576#0: accept() failed (24: Too many open files)
2020-07-23 02:53:49 [alert] 13576#0: accept() failed (24: Too many open files)
Copy the code

The number of open file handles is too high, causing Nginx to report an error. So how do we solve this problem?

Problem analysis

Since we can basically determine the cause of the problem from the Nginx error log, is this a problem with Nginx itself? The answer is: yes, not entirely!

Why? The reason is simple: Nginx can’t open that many file handles, partly because I haven’t configured the maximum number of files that Nginx can open. On the other hand, because CentOS 6.8 has a limit on the number of open file handles, I also did not configure the maximum number of file handles for the operating system. So it’s not all Nginx! In a sense, I was wrong about Nginx!

In CentOS 6.8, you can run the following command to view the default maximum number of file handles on the server:

[root@binghe150 ~]# ulimit -n
1024
Copy the code

As you can see, in CentOS 6.8, the default maximum number of file handles is 1024.

When the number of connections exceeds 1024, the following error message is displayed in the Nginx error log.

[alert] 13576#0: accept() failed (24: Too many open files)
Copy the code

To solve the problem

So how do we solve this problem? In fact, it is very simple, continue to read!

Run the following command to set the number of open file handles to a large enough size.

ulimit -n 655350
Copy the code

At the same time, add the following configuration items to nginx.conf.

worker_rlimit_nofile 655350; 
Copy the code

Note: The above configuration needs to be at the same level as error_log.

This solves the problem of excessive Nginx connections and allows Nginx to support high concurrency (you need to configure Nginx here).

In addition, ulimit -n affects the number of concurrent connections in MySQL. By increasing it, you can also increase the concurrency of MySQL.

Note: useulimit -n 655350The modification is valid only for the current shell and becomes invalid after exit.

Permanent solution

To make changes to the ULIMITS value permanent, you must modify the configuration file. You can put the ulimit modification command in /etc/profile, which is inconvenient.

Another method is to modify the/etc/security/limits the conf configuration file, as shown below.

vim /etc/security/limits.conf
Copy the code

Add the following configuration items at the end of the file.

* soft nofile 655360
* hard nofile 655360
Copy the code

Save and exit the Vim editor.

Soft indicates software, hard indicates hardware, and nofile indicates the number of file handles that can be opened.

Finally, note that for the limits.conf configuration to take effect, you must ensure that the pam_limits.so file is added to the startup file. Check whether the following configurations exist in the /etc/pam.d/login file:

session required /lib64/security/pam_limits.so
Copy the code

If no, add the preceding configuration items.

To get welfare

Follow the “Glacier Technology” wechat official account, reply to the “design Mode” keyword in the background to get the “Java 23 Design Modes in Simple Terms” PDF document. Reply to the “Java8” keyword to receive the Java8 New Features Tutorial PDF file.

Well, that’s all for today! Don’t forget to like, to see and forward, let more people see, study together, progress together!!

Write in the last

If you think the glacier is good, please search and pay attention to the “Glacier technology” wechat public number, with the glacier to learn high concurrency, distributed, micro services, big data, the Internet and cloud native technology, “Glacier technology” wechat public number updated a large number of technical topics, each technical article full of dry goods! Many readers have successfully changed their jobs to Dafang by reading articles on the wechat official account of glacier Technology. There are also many readers to achieve a leap in technology, become the technical backbone of the company! If you want to improve your ability like them, realize the leap of technical ability, enter dafa, get a promotion and pay raise, then pay attention to the “Glacier Technology” wechat public account, update the super hard core technology dry goods every day, let you no longer confused about how to improve technical ability!