Linux

How to Fix Common Nginx Web Server Errors

Nginx is a very popular web server these days. In this article we will show you how to fix common Nginx errors...

· >
How to fix common Nginx Errors

Nginx is a very popular web server these days. In this article we will show you how to fix common Nginx errors when setting up an Nginx web server and possible solutions. 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 errors like “unable to connect / refused to connect” or “www.example.com refused to connect” when trying to access your website, it might be due to following reasons.

  • Nginx Service not Running:

You can check the Nginx service status by typing “systemctl status nginx” this will tell you whether Nginx is running state. Moreover you can also check Nginx configuration by executing “nginx -t”, the output shall let you know whether Nginx configuration file are ok or is there any issue.

  • Firewall Blocking Web Ports:

If you are using UFW firewall you can allow ports 80 & 443 by “sudo ufw allow 80, 443/tcp” in your linux terminal. if you wan to know more about configuring UFW firewall, visit our article on how to install & configure UFW firewall to secure web servers. Or if you are using firewalld to protect your linux box, you can use command “sudo firewall-cmd –add-service={http, https} –permanent”, then reload the firewall using command “sudo systemctl reload firewalld”

  • Nginx not listening on Public IP:

In Nginx configuration file

404 Not Found – Nginx

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 root directory is configured using the root directive, like this: root /usr/share/nginx/example.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 – Nginx

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

  • 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 (WAF) 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.

YOU MIGHT ALSO LIKE TO LEARN ABOUT

500 Internal Server Error – Nginx

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. If your web application has a built-in cache, you can try flushing the app cache to fix this error.
  3. Your web application may produce its own error log. Check this log file to debug this error.
  4. You didn’t configure Nginx to use PHP-FPM, so Nginx doesn’t know how to execute PHP code.

Nginx Shows the default page – 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

  • You didn’t use a real domain name for the server_name directive in your Nginx virtual host.
  • You forgot to reload Nginx.

Two Virtual Host file 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.

Conclusion

In this article we learnt different errors faced by new admins while setting up Nginx server for the very first time. Offcourse, there are other errors as this is not the complete list. Share your experience with us what errors you have faced while setting up your Nginx Web Server. We hope this has been informative for you and would like to thank you for viewing.

Leave a Reply

Your email address will not be published. Required fields are marked *