Ошибка 404 not found nginx

Веб-серверы Nginx и Apache мало похожи друг на друга, и их отличия касаются не только особенностей подключения пользователей, но и обработки URL на сервере. Очень часто новые пользователи Nginx получают ошибку 404 для URL, которые, по сути, должны были бы работать.

В этой статье рассмотрим, почему возникает ошибка «404 not found Nginx», а также способы её устранения и отладки.Мы не будем разбираться с ситуацией, когда файла действительно нет на сервере — это решение, не требующее пояснений. Мы рассмотрим проблему обработки location в Nginx.

Давайте сначала разберёмся, как обрабатываются URL в Nginx. Когда веб-сервер определил, к какому блоку server (сайту) нужно передать запрос пользователя, просматриваются все префиксные блоки location и выбирается тот, который подходит лучше всего. Например, рассмотрим стандартную конфигурацию для WordPress. Здесь префиксные location отмечены зелёным, а с регулярным выражением — оранжевым:

location / {
index index.html index.php;
}
location /favicon.ico {
access_log off;
}
location ~* \.(gif|jpg|png)$ {
expires 30d;
}
location ~ \.php$ {
fastcgi_pass localhost:9000;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
include fastcgi_params;
}

Префиксные локейшены всегда начинаются с символа /. Регулярные же содержат символы регулярных выражений: ~ $ ^ * и так далее. Если пользователь запрашивает favicon.ico, то будет выбран второй location, так как он лучше всего соответствует запросу, при любом другом запросе будет выбран location /, так как он соответствует всем запросам, а других префиксных location у нас нет. Это просто, а дальше начинается магия. После того, как был найден нужный location, Nginx начинает проверять все регулярные выражения в порядке их следования в конфигурационном файле.

При первом же совпадении Nginx останавливает поиск и передаёт управление этому location. Или, если совпадений не было найдено, используется ранее обнаруженный префиксный location. Например, если запрос заканчивается на .php, то первый location будет проигнорирован, а управление передастся четвёртому (~ \.php$)

Таким образом, любое неверно составленное регулярное выражение в любой части конфигурационного файла может полностью всё сломать. Поэтому разработчики рекомендуют по минимум использовать регулярные выражения. Что касается вложенных location, то обрабатываются они так же как и основные, только уже после передачи управления в нужный location. Путём чтения конфигурационного файла понять, какой location вызывает 404 сложно, поэтому, чтобы исправить ошибку, нам понадобиться режим отладки Nginx.

Как включить режим отладки Nginx?

Сначала нам необходимо установить версию Nginx с поддержкой отладки. Чтобы проверить, поддерживает ли ваша текущая версия этот режим, наберите:

nginx -V

В выводе должна быть строчка «—with-debug». Если её нет, значит отладка не поддерживается, и надо установить версию с поддержкой. В CentOS такой пакет называется nginx-debug. Для его установки наберите:

sudo yum install nginx-debug

Теперь появился ещё один исполняемый файл, и он собран уже с поддержкой отладки:

nginx-debug -V

Откройте конфигурационный файл вашего сайта или глобальный конфигурационный файл, если вы не задавали настройки логов отдельно для каждого сайта, и в конце стоки error_log замените error на debug:

error_log /var/log/nginx/domains/test.losst.pro.error.log debug

Останавливаем обычную версию и запускаем версию с отладкой:

systemctl stop nginx
systemctl start nginx-debug

Как исправить «404 Not Found Nginx»?

1. Регулярные выражения

Как я уже сказал выше, самой частой проблемой, которая вызывает 404, являются регулярные выражения. Смотрим, что происходит в лог файле:

tail -f /var/log/nginx/domains/test.losst.pro.error.log

Видим, что серверу пришёл запрос /vstats. Дальше он проверяет location: /, /robots.txt, /vatsts/, /site-control/. Здесь уже можем понять, в чём проблема — промазали на один слеш. Дальше проверяются все регулярные выражения, и, так как в них ничего найдено не было, выбирается location /.

Далее директива try_files пытается найти файл /vstats, не находит и ищет index.php, который, в свою очередь, возвращает 404.

Если мы наберём, то что ожидает видеть Nginx — /vstats/, то откроется наша страница статистики.

Если мы добавим к конфигурационному файлу ещё один location с регулярным выражением, например:

location ~* {
try_files $uri $uri/ =404;
}

То абсолютно все запросы будут обрабатываться именно этим регулярным выражением и, естественно, что ничего работать не будет. Видим, что приходит запрос /vstats/:

Он совпадает с префиксным location, но потом Nginx видит наше регулярное выражение и передаёт управление ему.

Поэтому будьте очень осторожны с регулярными выражениями, если они вам нужны, то размещайте их только внутри префиксных location, чтобы ограничить их область действия этим location, иначе может возникнуть ошибка 404 nginx.

2. Недостаточно памяти

Если php-скрипту не хватило оперативной памяти для выполнения, и его процесс был убит операционной системой, то Nginx тоже может вернуть ошибку 404. Такое поведение вы будете наблюдать, когда скрипт очень долго выполняется, а потом появляется «404 Not Found» или страница ошибки вашего движка. Обычно эта неисправность тоже видна в отладочном логе.

Решить такую проблему можно, освободив память на сервере, часто такое может возникать из-за утечек памяти в php, когда процессы php-fpm занимают почти всю память на сервере. Поэтому перезапуск php-fpm решает проблему:

systemctl restart php-fpm

Чтобы избежать этой проблемы в будущем, можно настроить автоматический перезапуск процессов после обработки определённого количества запросов. Например каждые 200 запросов:

vi /etc/php-fpm.d/www.conf

pm.max_requests = 200

3. Не найден index

Если вы запрашиваете URL вида /vstats/, но в настройках Nginx не указан файл index, который нужно использовать для этой ссылки, то у вас ничего не выйдет, и вы получите 404. Вы можете добавить директиву index в ваш location:

location / {
index index.php index.html index.htm;
}

Или сразу в server, в Nginx все location наследуют директивы, установленные в server.

4. Другие проблемы

Подобных проблем, вызывающих 404 в Nginx, может быть очень много, но все они решаемы, и всё, что нужно для их решения, есть в отладочном логе Nginx. Просто анализируйте лог и на основе этого вносите исправления.

Выводы

В этой статье мы рассмотрели основные причины, из-за которых может возникнуть ошибка 404 not found Nginx. Как видите, может быть много проблем, но всё достаточно просто решается. А с какими проблемами, вызывающими эту ошибку, вы сталкивались? Как их решали? Напишите в комментариях!

Обнаружили ошибку в тексте? Сообщите мне об этом. Выделите текст с ошибкой и нажмите Ctrl+Enter.

Creative Commons License

Статья распространяется под лицензией Creative Commons ShareAlike 4.0 при копировании материала ссылка на источник обязательна .

Ubuntu 14

When you’re running a website on an Nginx web server, encountering a 404 Not Found error can be frustrating. This error typically means that while the server itself is reachable, the specific page you’re trying to access isn’t. In this article, we’ll guide you through some troubleshooting steps to resolve this issue.

To troubleshoot a 404 Not Found error on an Nginx web server, you should first check the Nginx configuration to ensure that the server block is properly set up with the correct root directory and index file. Next, verify the file permissions and make sure they are readable by the Nginx user. If your website uses HTTPS, check the SSL configuration as well. Restart Nginx after making any changes. If the issue persists, check the Nginx error log for more specific information.

  1. Understanding the 404 Error
  2. Check the Nginx Configuration
    • Verify Server Block Configuration
    • Check File Permissions
    • Check the SSL Configuration
  3. Restart Nginx
  4. Check the Nginx Error Log
  5. Conclusion

Understanding the 404 Error

The 404 Not Found error is an HTTP status code that means the server couldn’t find the requested resource. In the context of a web server like Nginx, this usually means that there’s no match for the URL you’re trying to access in the server’s configuration or the file doesn’t exist in the site’s root directory.

Check the Nginx Configuration

The first step in troubleshooting is to check your Nginx configuration. The configuration files for Nginx are typically located in the /etc/nginx/ directory.

Verify Server Block Configuration

The server block (also known as a virtual host) configurations are located in the /etc/nginx/sites-available/ directory. Each website hosted on your server should have its own configuration file here.

Here’s an example of what a server block might look like:

server {
 listen 80;
 server_name test.com www.test.com;
 root /var/www/test.com/html;
 index index.html index.htm;
}

In this example, listen 80; is telling Nginx to listen on port 80, which is the standard HTTP port. The server_name directive should be set to your website’s domain name. The root directive specifies the root directory of your website, where your index file is located. The index directive lists the names of files that Nginx should try to serve as the default page if a specific one isn’t requested.

Make sure that the root directive points to the correct directory and that an index file exists there.

Check File Permissions

Next, check the permissions of the files and directories of your website. They should be readable by the user that Nginx runs as, which is usually www-data.

You can check the current permissions with the ls -l command:

ls -l /var/www/test.com/html

The output will show the permissions, owner, group, and name of the files and directories in the specified directory. The permissions are displayed in the form rwxrwxrwx, which represents the permissions for the owner, group, and others, respectively. The r stands for read, w for write, and x for execute.

If the permissions are incorrect, you can change them with the chmod command, and you can change the owner with the chown command:

sudo chown -R www-data:www-data /var/www/test.com/html
sudo chmod -R 755 /var/www/test.com/html

In the chown command, -R means to apply the change recursively to the directory and its contents. www-data:www-data is the user and group that you’re changing the owner to.

In the chmod command, 755 sets the permissions to read, write, and execute for the owner, and read and execute for the group and others.

Check the SSL Configuration

If your website uses HTTPS, you also need to check the SSL configuration in the Nginx server block. It should look something like this:

server {
 listen 443 ssl;
 server_name test.com www.test.com;
 root /var/www/test.com/html;
 index index.html index.htm;

 ssl_certificate /etc/letsencrypt/live/test.com/fullchain.pem;
 ssl_certificate_key /etc/letsencrypt/live/test.com/privkey.pem;
}

The listen 443 ssl; directive tells Nginx to listen on port 443, which is the standard HTTPS port, and to enable SSL. The ssl_certificate and ssl_certificate_key directives specify the locations of the SSL certificate and private key files, respectively.

Make sure that the paths to the SSL certificate and private key files are correct and that the files are readable by the www-data user.

Restart Nginx

After making any changes to the Nginx configuration, you need to restart the service for the changes to take effect. You can do this with the following command:

sudo service nginx restart

Check the Nginx Error Log

If you’re still encountering the 404 error after following the above steps, the next place to look is the Nginx error log. By default, it’s located at /var/log/nginx/error.log.

You can view the most recent entries in the log with the tail command:

sudo tail /var/log/nginx/error.log

The error log can provide more specific information about what’s causing the 404 error.

Conclusion

Troubleshooting a 404 Not Found error on an Nginx web server involves checking the server block configuration, file permissions, and SSL configuration, and examining the Nginx error log. By following these steps, you should be able to identify and resolve the issue.

If you encounter a 404 Not Found error on your Nginx web server, you should first check your Nginx configuration. Verify the server block configuration to ensure that the root directory and index file are correctly specified. Also, check the file permissions to ensure that they are readable by the Nginx user (www-data). If your website uses HTTPS, make sure to check the SSL configuration as well. After making any changes to the configuration, restart Nginx for the changes to take effect. If the issue persists, check the Nginx error log for more specific information about the error.

The Nginx configuration files are typically located in the /etc/nginx/ directory. The server block configurations for each website hosted on your server can be found in the /etc/nginx/sites-available/ directory.

When you visit an Nginx-configured website, your browser sends a request to the web server. After that, your web server responds with the data having an HTTP header. HTTP status codes are included in that HTTP header to explain how the request is responded.

When your requests are handled successfully, the HTTP status code is not displayed on your browser. However, if anything goes wrong, your web browser will typically display a message with the HTTP status code to tell you the problem with the request. The error messages such as 504, 500, 503, 502, including the “Error 404 not found” message, are part of that process.

Essentially, the “404 error” indicates that your or your visitor’s web browser was connected successfully to the website server or the host. However, it was unable to locate the requested resource, such as filename or any specific URL.

For instance, If someone attempts to reach “yourwebsite.com/anypostname” and does not have any content linked with “anypostname“, in such a case, you will receive a 404 error on your browser as the resource requested does not exist. In other words, we can say that when a requested asset such as JavaScript, image, or CSS file is missing, your operational browser will generate a “404” error.

How to fix 404 error in Nginx

If you are getting a “404 Not Found” Nginx error and you have checked that the requested asset exists on your server, then your configuration file may be triggering the error. To fix the “404 Not Found”, open up your terminal by pressing “CTRL+ALT+T” and execute the below-given command for opening the Nginx configuration file:

$ sudo nano /etc/nginx/nginx.conf

Your Nginx configuration file will look like this:

If the path added in the Nginx configuration file is incorrect, it will result in a “404 Not Found” Ngnix error. So, verify your path leading towards the asset directory:

root /usr/share/nginx/html;

It will also be helpful to review your errors and access logs in Nginx. To do so, utilize the below-given “cat” command for extracting the content of the error_log present in the “/var/log/nginx/error.log” file:

$ sudo cat /var/log/nginx/error.log

To check the content of the access_log, write out this command in your terminal:

$ sudo cat /var/log/nginx/access.log

How to fix 404 Nginx error using online tools

The “404 Nginx error” is also linked with the external resources, and it occurs when those resources are removed or modified. That’s why it is crucial to run the 404 error checks frequently to ensure that your website links are not broken. A regular check-up and fixing the broken links will assist you in making sure that the user experience of your website visitor is at a stable level. The following are some of the tools that you can utilize for checking the “404 Not Found” errors:

W3C Check Link

In the W3C Link Checker online tool, you have to enter your website URL, and it will scan all of your web pages for 404 Not Found and other issues. When the scan is over, it will return all of the broken URLs along with other results:

Check My Links

Check My Links is a basic Chrome plugin that permits you to check the links on the current web page. When this plugin is activated, the extension will determine if the links on the current page are valid or broken:

Broken Link Checker

Broken Link Checker is another useful plugin that offers various methods for checking the broken links of your website. A time period can be set that instructs this plugin to check for broken links every “X” hour. You can choose whether the plugin should send an email report comprising all of the broken links or the site portion which is successfully scanned:

If you face a “404 Not Found” Nginx error or want to ensure that your website links are not broken or monitor your site, then utilize the above methods to fix it.

Conclusion

The “404 Not Found Error” on the web page is an HTTP response status code that declares your requested resource was not found. It can be difficult for you to figure out the reason behind the “404 not Found Error“. In this post, we have explained what the “404 Not Found Error” is. We also provided you the methods to fix the “404 Not Found Error” by utilizing the Nginx configuration file and the other online tools such as Check My Links, W3C Check Link, and the Broken Link Checker.

About the author

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.

There are many times when you have installed NGINX but sometimes you will come across the weird thing ie 404 not found.

In thing article I will walk you through how to get it resolved.


Prerequisites

You have NGINX installed. If not then in Ubuntu or any Debain version you can install with the following

sudo apt-get update

sudo apt-get install nginx

1) Forgot To Restart NGINX Server

Most of the times developer change the configuration file and forget to restart the NGINX server

First TEST if the NGINX configuration is correct or not with the following command

sudo nginx -t

If everything is correct then run the reload command

sudo service nginx reload

I have written a detailed article on how to install and manage your NGINX server in Linux don’t forget to check it out How To Install NGINX In Linux / Ubuntu Package Manager & Manage It.


2) Created New Configuration File But Not Activated

All your configuration files resides in /etc/nginx/sites-available & once you activate them they will be in /etc/nginx/sites-enabled which are eventually the symbolic link to sites-available.

By default the default configuration file will be enabled first go to sites-enabled folder and remove the soft link of default. Don’t worry you can activate it later.

You might have your website configuration file in sites-available For Eg. Mine is stackcoder.in which wont be activated inside sites-enabled. To activate the configuration file use the following code

sudo ln -s /etc/nginx/sites-available/stackcoder.in /etc/nginx/sites-enabled/

Make sure to change it to your configuration file name.


3) If Using PHP, Then Check If PHP Socket Exists

Go to your website configuration file inside /etc/nginx/sites-available/stackcoder.in , here mine is stackcoder.in your’s might be different. And here you look for the following code block

location ~ \.php$ {
  include snippets/fastcgi-php.conf;
  fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}

Check if this path exists unix:/var/run/php/php7.2-fpm.sock


4) If Using LARAVEL Project

If your using LARAVEL project with NGINX configuration file then make sure to include the following file inside /etc/nginx/sites-available/stackcoder.in make sure to check your configuration file name and make the following changes

location / {
    try_files $uri $uri/ /index.php?q=$uri&$args;
}

NOTE: Don’t forget to restart the NGINX server after doing this


5) FASTCGI_PARAM Issues

Sometimes you might have set the wrong path for fastcgi_param

WRONG Configuration

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
    include    fastcgi_params;
}

CORRECT Configuration

I prefer the following one

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
    include fastcgi_params;
}

OR

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
    include fastcgi_params;
}

6) Project Files / NGINX Permission Issues

Yup you read it right. Some times you will face problems when you might have installed the project in wrong directory, or directory without proper permission privileges issues.

Cross check if the project path and any other path given in NGINX configuration is correct or not.

Check if there are any permission issues. You can find the error logs in /var/log/nginx/error.log


7) Uninstall & Install NGINX Again (Last Brahmastra)

Some times NGINX can be mischievous, He he he! kidding. Just re-installing works

To remove NGINX use the following command

sudo apt-get remove nginx

To install it again use the following command

sudo apt-get install nginx

Conclusion

Hope this article helped you. I have written other articles on NGINX kindly check them too

How To Cache Static Files With NGINX Server

Redirect www to a non-www website or vice versa

How To Create Free SSL Certificate With Lets Encrypt/Certbot In Linux (Single / Multiple Domains)

How To Install Linux, NGINX, MYSQL, PHP (LEMP Stack) on Ubuntu

Nginx is a very popular web server these days. This article will show you some common errors when running an Nginx web server and possible solutions. This is not a complete list. If you still can’t fix the error after trying the advised solutions, please check your Nginx server logs under /var/log/nginx/ directory and search on Google to debug the problem.

Unable to connect/Refused to Connect

If you see the following error when trying to access your website:

Firefox can’t establish a connection to the server at www.example.com

or

www.example.com refused to connect

or

The site can't be reached, www.example.com unexpectedly closed the connection.

Firefox Unable to connect

It could be that

  1. Nginx isn’t running. You can check Nginx status with sudo systemctl status nginx. Start Nginx with sudo systemctl start nginx. If Nginx fails to start, run sudo nginx -t to find if there is anything wrong with your configuration file. And check the journal (sudo journalctl -eu nginx) to find out why it fails to start.
  2. Firewall blocking ports 80 and 443. If you use the UFW firewall on Debian/Ubuntu, run sudo ufw allow 80,443/tcp to open TCP ports 80 and 443. If you use Firewalld on RHEL/CentOS/Rocky Linux/AlmaLinux, run sudo firewall-cmd --permanent --add-service={http,https}, then sudo systemctl reload firewalld to open TCP ports 80 and 443.
  3. Fail2ban. If your server uses fail2ban to block malicious requests, it could be that fail2ban banned your IP address. Run sudo journalctl -eu fail2ban to check if your IP address is banned. You can add your IP address to the fail2ban ignoreip list, so it won’t be banned again.
  4. Nginx isn’t listening on the right network interface. For example, Nginx isn’t listening on the server’s public IP address.

If systemctl status nginx shows Nginx is running, but sudo ss -lnpt | grep nginx shows Nginx is not listening on TCP port 80/443, it could be that you deleted the following lines in the /etc/nginx/nginx.conf file.

include /etc/nginx/conf.d/*;

So Nginx doesn’t use the virtual host files in /etc/nginx/conf.d/ directory. Add this line back.

nginx include virtual hosts file

The Connection Has Timed Out

the connection has timed out

This could mean that your server is offline, or Nginx isn’t working properly. I once had an out-of-memory problem, which caused Nginx to fail to spawn the worker processes. If you can see the following error message in /var/log/nginx/error.log file, your server is short of memory.

fork() failed while spawning "worker process" (12: Cannot allocate memory)

404 Not Found

nginx 404 not found

404 not found means Nginx can’t find the resources your web browser asks for. The reason could be:

  1. The web root directory doesn’t exist on your server. In Nginx, the web roor directory is configured using the root directive, like this: root /usr/share/nginx/linuxbabe.com/;.  Make sure your website files (HTML, CSS, JavaScript, PHP) are stored in the correct directory.
  2. PHP-FPM isn’t running. You can check PHP-FPM status with sudo systemctl status php7.4-fpm (Debian/Ubuntu) or sudo systemctl status php-fpm.
  3. You forgot to include the try_files $uri /index.php$is_args$args; directive in your Nginx server config file. This directive is needed to process PHP code.
  4. Your server has no free disk space. Try to free up some disk space. You can use the ncdu utility (sudo apt install ncdu or sudo dnf install ncdu) to find out which directories are taking up huge amount of disk space.

403 Forbidden

This error means that you are not allowed to access the request resources. Possible scenario includes:

  • The website administrator blocks public access to the requested resources with an IP whitelist or other methods.
  • The website could be using a web application firewall like ModSecurity, which detected an intrusion attack, so it blocked the request.

Some web applications may show a different error message when 403 forbidden happens. It might tell you that “secure connection failed”, while the cause is the same.

secure connection failed nginx

500 Internal Server Error

Nginx 500 internal server error

This means there is some error in the web application. It could be that

  1. The database server is down. Check MySQL/MariaDB status with sudo systemctl status mysql. Start it with sudo systemctl start mysql. Run sudo journalctl -eu mysql to find out why it fails to start. MySQL/MariaDB process could be killed due to out-of-memory issue.
  2. You didn’t configure Nginx to use PHP-FPM, so Nginx doesn’t know how to execute PHP code.
  3. If your web application has a built-in cache, you can try flushing the app cache to fix this error.
  4. Your web application may produce its own error log. Check this log file to debug this error.
  5. Your web application may have a debugging mode. Turn it on and you will see more detailed error messages on the web page. For example, you can turn on debugging mode in the Modoboa mail server hosting platform by setting DEBUG = True in the /srv/modoboa/instance/instance/settings.py file.
  6. PHP-FPM could be overloaded. Check your PHP-FPM log (such as /var/log/php7.4-fpm.log). If you can find the [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers) warning message, you need to allocate more resources to PHP-FPM.
  7. Sometimes reloading PHP-FPM (sudo systemctl reload php7.4-fpm) can fix the error.
  8. It also might be that you didn’t install the database module for PHP, so PHP can’t connect to the database. For MySQL/MariaDB, install it with sudo apt install php7.4-mysql. For PostgreSQL, install it with sudo apt install php7.4-pgsql.

Nginx Shows the default page

welcome to Nginx

If you try to set up an Nginx virtual host and when you type the domain name in your web browser, the default Nginx page shows up, it might be

  • Your Nginx virtual host file doesn’t have the server_name directive.
  • You didn’t use a real domain name for the server_name directive in your Nginx virtual host.
  • You forgot to reload Nginx.
  • You can try deleting the default virtual host file in Nginx (sudo rm /etc/nginx/sites-enabled/default).

The page isn’t redirecting properly

Firefox displays this error as The page isn’t redirecting properly. Google Chrome shows this error as www.example.com redirected you too many times.

Firefox The page isn’t redirecting properly

This means you have configured Nginx redirection too many times. For example, you may have added an unnecessary return 301 directive in the https server block to redirect HTTP to HTTPS connection.

If you have set up a page cache such as Nginx FastCGI cache, you need to clear your server page cache.

504 Gateway time-out

This means the upstream like PHP-FPM/MySQL/MariaDB isn’t able to process the request fast enough. You can try restarting PHP-FPM to fix the error temporarily, but it’s better to start tuning PHP-FPM/MySQL/MariaDB for faster performance.

Optimize MySQL/MariaDB Performance

Here is the InnoDB configuration in my /etc/mysql/mariadb.conf.d/50-server.cnf file. This is a very simple performance tunning.

innodb_buffer_pool_size = 1024M
innodb_buffer_pool_dump_at_shutdown = ON
innodb_buffer_pool_load_at_startup  = ON
innodb_log_file_size = 512M
innodb_log_buffer_size = 8M

#Improving disk I/O performance
innodb_file_per_table = 1
innodb_open_files = 400
innodb_io_capacity = 400
innodb_flush_method = O_DIRECT
innodb_read_io_threads = 64
innodb_write_io_threads = 64
innodb_buffer_pool_instances = 3

Where:

  • The InnoDB buffer pool size needs to be at least half of your RAM. ( For VPS with small amount of RAM, I recommend setting the buffer pool size to a smaller value, like 400M, or your VPS would run out of RAM.)
  • InnoDB log file size needs to be 25% of the buffer pool size.
  • Set the read IO threads and write IO thread to the maximum (64) and
  • Make MariaDB use 3 instances of InnoDB buffer pool. The number of instances needs to be the same number of CPU cores on your system.

After saving the changes, restart MariaDB.

sudo systemctl restart mariadb

Recommended reading: MySQL/MariaDB Database Performance Monitoring with Percona on Ubuntu Server

Find Out Which PHP Script Caused 504 Error

You can check the web server access log to see if there are any bad requests. For example, some folks might find the following lines in the Nextcloud access log file (/var/log/nginx/nextcloud.access).

"GET /apps/richdocumentscode/proxy.php?req=/hosting/capabilities HTTP/1.1" 499 0 "-" "Nextcloud Server Crawler"
"GET /apps/richdocumentscode/proxy.php?req=/hosting/capabilities HTTP/1.1" 499 0 "-" "Nextcloud Server Crawler"
"GET /apps/richdocumentscode/proxy.php?req=/hosting/capabilities HTTP/1.1" 499 0 "-" "Nextcloud Server Crawler"

Notice the HTTP status code 499, which means the HTTP client quit the connection before the server gives back an answer. Successful HTTP code should be 2xx or 3xx. If you can find HTTP code 4xx, it means there’s a problem with this HTTP request. In this example, the Nextcloud richdocumentscode app isn’t working.

Increase Timeout Settings

You can also set a longer timeout value in Nginx to reduce the chance of gateway timeout. Edit your Nginx virtual host file and add the following lines in the server {...} block.

  proxy_connect_timeout       600;
  proxy_send_timeout          600;
  proxy_read_timeout          600;
  send_timeout                600;

If you use Nginx with PHP-FPM, then set the fastcgi_read_timeout to a bigger value like 300 seconds.  Default is 60 seconds.

 location ~ \.php$ {
     try_files $uri /index.php$is_args$args;
     include snippets/fastcgi-php.conf;
     fastcgi_split_path_info ^(.+\.php)(/.+)$;

     fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
     include fastcgi_params;
     fastcgi_read_timeout 300;
 }

Then reload Nginx.

sudo systemctl reload nginx

PHP-FPM also has a max execution time for each script. Edit the php.ini file.

sudo nano /etc/php/7.4/fpm/php.ini

You can increase the value to 300 seconds.

max_execution_time = 300

Then restart PHP-FPM

sudo systemctl restart php7.4-fpm

Memory Size Exhausted

If you see the following line in your Nginx error log, it means PHP reached the 128MB memory limit.

PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 57134520 bytes)

You can edit the php.ini file (/etc/php/7.4/fpm/php.ini) and increase the PHP memory limit.

memory_limit = 512M

Then restart PHP7.4-FPM.

sudo systemctl restart php7.4-fpm

If the error still exists, it’s likely there’s bad PHP code in your web application that eats lots of RAM.

PR_END_OF_FILE_ERROR

  1. You configured Nginx to rediect HTTP request to HTTPS, but there’s no server block in Nginx serving HTTPS request.
  2. Maybe Nginx isn’t running?
  3. Sometimes, the main Nginx binary is running, but a worker process can fail and exit due to various reasons. Check the Nginx error log (/var/log/nginx/error.log) to debug.

PHP-FPM Upstream Time Out

Some folks can find the following error in Nginx error log file ( under /var/log/nginx/).

[error] 7553#7553: *2234677 upstream timed out (110: Connection timed out) while reading response header from upstream

Possible solutions:

  1. Restart PHP-FPM.
  2. Upgrade RAM.
  3. Optimize database performance. Because PHP-FPM needs to fetch data from the database, if the database is slow to process requests, PHP-FPM will time out.

502 Bad Gateway

nginx 502 bad gateway

This error could be caused by:

  • PHP-FPM isn’t running. Check its status: sudo systemctl status php7.4-fpm.
  • PHP-FPM is running, but Nginx isn’t able to connect to PHP-FPM (Resource temporarily unavailable), see how to fix this error below.
  • PHP-FPM is running, but Nginx doesn’t have permission to connect to PHP-FPM socket (Permission denied). It might be that Nginx is running as nginx user, but the socket file /run/php/php7.4-fpm.sock is owned by the www-data user. You should configure Nginx to run as the www-data user.

Resource temporarily unavailable

Some folks can find the following error in Nginx error log file ( under /var/log/nginx/).

connect() to unix:/run/php/php7.4-fpm.sock failed (11: Resource temporarily unavailable)

This usually means your website has lots of visitors and PHP-FPM is unable to process the huge amounts of requests. You can adjust the number of PHP-FPM child process, so it can process more requests.

Edit your PHP-FPM www.conf file. (The file path varies depending on your Linux distribution.)

sudo nano /etc/php/7.4/fpm/pool.d/www.conf

The default child process config is as follows:

pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3

The above configuration means

  • PHP-FPM dynamically create child processes. No fixed number of child processes.
  • It creates at most 5 child processes.
  • Start 2 child processes when PHP-FPM starts.
  • There’s at least 1 idle process.
  • There’s at most 3 idle processes.

The defaults are based on a server without much resources, like a server with only 1GB RAM. If you have a high traffic website, you probably want to increase the number of child processes, so it can serve more requests. (Make sure you have enough RAM to run more child processes.)

pm = dynamic
pm.max_children = 20
pm.start_servers = 8
pm.min_spare_servers = 4
pm.max_spare_servers = 12

Save and close the file. Then we also increase the PHP memory limit.

sudo nano /etc/php/7.4/fpm/php.ini

Find the following line.

memory_limit = 128M

By default, a script can use at most 128M memory. It’s recommended to set this number to at lest 512M.

memory_limit = 512M

Save and close the file. Restart PHP-FPM. (You might need to change the version number.)

sudo systemctl restart php7.4-fpm

To monitor the health of PHP-FPM, you can enable the status page. Find the following line in the PHP-FPM www.conf file.

;pm.status_path = /status

Remove the semicolon to enable PHP-FPM status page. Then restart PHP-FPM.

sudo systemctl restart php7.4-fpm

Then edit your Nginx virtual host file. Add the following lines.  The allow and deny directives are used to restrict access. Only the whitelisted IP addresses can access the status page.

location ~ ^/(status|ping)$ {
        allow 127.0.0.1;
        allow your_other_IP_Address;
        deny all;

        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_index index.php;
        include fastcgi_params;
        #fastcgi_pass 127.0.0.1:9000;
        fastcgi_pass   unix:/run/php/php7.4-fpm.sock;
}

Save and close the file. Then test Nginx configurations.

sudo nginx -t

If the test is successful, reload Nginx for the changes to take effect.

sudo systemctl reload nginx

Sample PHP-FPM status page.

Nginx PHP-FPM status page

The PHP-FPM www.conf file gives you a good explanation of what each parameter means.

PHP-FPM status page slow requests

If PHP-FPM is very busy and unable to serve a request immediately, it will queue this request. By default, there can be at most 511 pending requests, determined by the listen.backlog parameter.

listen.backlog = 511

If you see the following value on the PHP-FPM status page, it means there has never been a request put in the queue, i.e. your PHP-FPM can process requests quickly.

listen queue:         0
max listen queue:     0

If there are 511 pending requests in the queue, it means your PHP-FPM is very busy, so you should increase the number of child processes.

You may also need to change the Linux kernel net.core.somaxconn setting, which defines max number of connections allowed to a socket file on Linux, such as the PHP-FPM Unix socket file. By default, its value is 128 before kernel 5.4 and 4096 starting with kernel 5.4.

linuxbabe@ubuntu:~$ sysctl net.core.somaxconn
net.core.somaxconn = 128

If you run a high traffic website, you can use a big value. Edit /etc/sysctl.conf file.

sudo nano /etc/sysctl.conf

Add the following two lines.

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

Save and close the file. Then apply the settings.

sudo sysctl -p

Note: If your server has enough RAM, you can allocate a fixed number of child processes for PHP-FPM like below. In my experience, this fixed the 500 internal error for a Joomla + Virtuemart website.

pm = static
pm.max_children = 50

Two Virtual Host files For the Same Website

If you run sudo nginx -t and see the following warning.

nginx: [warn] conflicting server name "example.com" on [::]:443, ignored
nginx: [warn] conflicting server name "example.com" on 0.0.0.0:443, ignored

It means there are two virtual host files that contain the same server_name configuration. Don’t create two virtual host files for one website.

PHP-FPM Connection reset by peer

Nginx error log file shows the following message.

recv() failed (104: Connection reset by peer) while reading response header from upstream

This may be caused by a restart of PHP-FPM. If it’s retarted manually by yourself, then you can ignore this error.

Nginx Socket Leaks

If you find the following error message in the /var/log/nginx/error.log file, your Nginx has a socket leaks problem.

2021/09/28 13:27:41 [alert] 321#321: *120606 open socket #16 left in connection 163
2021/09/28 13:27:41 [alert] 321#321: *120629 open socket #34 left in connection 188
2021/09/28 13:27:41 [alert] 321#321: *120622 open socket #9 left in connection 213
2021/09/28 13:27:41 [alert] 321#321: *120628 open socket #25 left in connection 217
2021/09/28 13:27:41 [alert] 321#321: *120605 open socket #15 left in connection 244
2021/09/28 13:27:41 [alert] 321#321: *120614 open socket #41 left in connection 245
2021/09/28 13:27:41 [alert] 321#321: *120631 open socket #24 left in connection 255
2021/09/28 13:27:41 [alert] 321#321: *120616 open socket #23 left in connection 258
2021/09/28 13:27:41 [alert] 321#321: *120615 open socket #42 left in connection 269
2021/09/28 13:27:41 [alert] 321#321: aborting

You can restart the OS to solve this problem. If it doesn’t work, you need to compile a debug version of Nginx, which will show you debug info in the log.

invalid PID number “” in “/run/nginx.pid”

You need to restart your web server.

sudo pkill nginx
sudo systemctl restart nginx

Cloudflare Errors

Here are some common errors and solutions if your website runs behind Cloudflare CDN (Content Delivery Network).

521 Web Server is Down

  • Nginx isn’t running.
  • You didn’t open TCP ports 80 and 443 in the firewall.
  • You changed the server IP address, but forgot to update DNS record in Cloudflare.

The page isn’t redirecting properly

If your SSL setting on the SSL/TLS app is set to Flexible, but your origin server is configured to redirect HTTP requests to HTTPS, Your Nginx server sends reponse back to Cloudflare in encrypted connection. Since Cloudflare is expecting HTTP traffic, it keeps resending the same request, resulting in a redirect loop. In this case, you need to use the Full (strict) SSL/TLS option in your Cloudflare settings.

Cloudflare SSL TLS full strict

Wrapping Up

I hope this article helped you to fix common Nginx web server errors. As always, if you found this post useful, then subscribe to our free newsletter to get more tips and tricks 🙂

You may want to check out:

  • How to Fix Common Let’s Encrypt/Certbot Errors

Понравилась статья? Поделить с друзьями:
  • Ошибка 404 linux
  • Ошибка 404 на айфоне
  • Ошибка 404 windows
  • Ошибка 403 тендер
  • Ошибка 404 json