Methods of payment Abuse

How to fix 404 Not Found Nginx with insufficient memory

15.11.2023, 13:31

404 Not Found is an error that occurs when trying to access a web page or resource that is not found on the server. Nginx is a web server and proxy server that can be used to serve websites. 404 Not Found Nginx means that the requested page or resource was not found on the server that uses Nginx.

How can I fix the error if there is not enough RAM?

When a php script ran out of RAM to execute and its process was stopped by the system, Nginx can also return a 404 error.

You will observe such a situation when the script is executed for a long time, after which it issues a "404 Not Found" or an error page of your engine. Usually this malfunction is also visible in the debugging log.

The problem is solved by freeing up memory on the server, this can often occur due to memory leaks in php, when php-fpm processes occupy almost all the memory on the server. By restarting php-fpm, this problem cannot be solved:

$ systemctl restart php-fpm

Is it possible to avoid this situation in the future? The answer is yes, you can. To do this, you need to set up automatic restart of processes.

Setting up automatic restart of processes

To configure the automatic restart of processes after processing a certain number of requests in Linux, you can use process management tools such as systemd or supervisord.

systemd

Create a file with the .service extension in the /etc/systemd/system/ directory with the configuration of your process. For example, the php-fpm.service file

[Unit]
Description=PHP FastCGI Process Manager
After=network.target
[Service]
Type=simple
ExecStart=/usr/sbin/php-fpm
Restart=always
RestartSec=3
StartLimitInterval=0
StartLimitBurst=10
[Install]
WantedBy=multi-user.target

In this example, the StartLimitBurst parameter sets the number of restarts before temporarily shutting down the process.

After creating the service file, run the command to restart systemd and activate the new service:

sudo systemctl daemon-reload

sudo systemctl enable php-fpm.service

sudo systemctl start php-fpm.service

supervisord

Install it using the package manager of your Linux distribution. Then create a configuration file for your process in the /etc/supervisor/conf.d/ directory. For example, the php-fpm.conf file:

[program:php-fpm]
command=/usr/sbin/php-fpm
autorestart=true
startretries=10

In this example, the startretries parameter sets the number of attempts to restart the process.

After creating the configuration file, restart supervisord:

sudo supervisorctl reread

sudo supervisorctl update

These steps will help you set up automatic restarts of processes after processing a certain number of requests in Linux.