How to fix the "Resource temporarily unavailable" error in nginx?

In the future, using the website cloudhosting.lv, you agree to the rules of using Cookies. Read more. I agree

How to fix the "Resource temporarily unavailable" error in nginx?

This tutorial discusses using nginx + php-fpm (at the time of writing, the PHP version was 7.3).

If you experience the problem that when there is a small load on the server, all sites suddenly start to give a 502 Bad Gateway error. In the logs, you may see something like:

[error] 7460#7460: *5365312 connect() to unix:/run/php/php7.3-fpm.sock failed (11: Resource temporarily unavailable) while connecting to upstream

This situation occurs because the operating system rejects attempts by nginx to connect to the Unix socket. The reason may be that the maximum number of connections to the socket has been exceeded, or the maximum number of unprocessed connections to the socket has been reached.

To check the limits, run:

sysctl net.core

Look for the following lines:

net.core.somaxconn = 128
net.core.netdev_max_backlog = 200

The error occurs because the maximum number of connections is 128, while the maximum number of unprocessed connections is 200.

To change the limits, change the following lines to the /etc/sysctl.conf file:

net.core.somaxconn = 20000
net.core.netdev_max_backlog = 65535

Apply the parameters by running:

sysctl -p

Restart php-fpm:

/etc/init.d/php7.3-fpm restart

Use the loadem utility to test your site with the new configuration:

./loadem -l 1 https://mysite.ru 200
URL: https://mysite.ru
Clients: 2000
Starting
MaTps 7.81, Tps 7.81, Err 0.00%, Resp Time 0.966
Completed 8 requests in 1.03 seconds
Total TPS: 7.74
Avg. Response time: 0.966
Max Response time: 1.064

Note the Err 0.00%. It should now be equal to 0 (be sure to test your site before changing the sysctl parameters).