How to fix 404 Not Found Nginx when memory is low

15.11.2023
13:31

404 Not Found is an error that occurs when you try 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 to resolve the error if there is not enough RAM?

When a php script did not have enough RAM to execute and its process was stopped by the system, Nginx can also return a 404 error.

This is the situation you will see when the script runs for a long time, after which it returns"404 Not Found" or your engine's error page. Usually, this fault is also visible in the debug log.

How to fix 404 Not Found Nginx when memory is low

The problem is solved by freeing up memory on the server, often this can occur due to memory leaks in php, when php-fpm processes take up almost all memory on the server. By restarting php-fpm this problem is partially 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 configure automatic restart of processes.

Configuring the automatic restart of processes

To configure automatic restart of processes after 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 your process configuration. For example, the file php-fpm.service

[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 the process is temporarily shut down.

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 your Linux distribution's package manager. Then create a configuration file for your process in the /etc/supervisor/conf.d/ directory. For example, the file php-fpm.conf:

[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 configure processes to restart automatically after processing a certain number of requests in Linux.

Other articles

15.11.2023
5 629
Knowledge base / Information
What is the difference between VPS and VDS
15.11.2023
8 435
Knowledge base / Instructions - THE.Hosting
How to move the site to a new hosting
15.11.2023
19 561
Knowledge base / Instructions - THE.Hosting
What is a VMOS virtual machine