Resolve 404 Not Found In NGINX


Share On        


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




AUTHOR

Channaveer Hakari

I am a full-stack developer working at WifiDabba India Pvt Ltd. I started this blog so that I can share my knowledge and enhance my skills with constant learning.

Never stop learning. If you stop learning, you stop growing