Nginx doesn't find subdirectories and also doesn't run index.php at default directory

Reynier asked:

I changed from Apache to Nginx and now I’m having some problems.

  1. When I try to access to any folder as for example http://devserver/monitor/web where I have a Symfony project I get 404 error
  2. When I try to access to http://devserver I get this message all the time File not found.

This is how my /etc/nginx/conf.d/default.conf file looks like:

server {
    listen       80;
    server_name  devserver;

    location / {
        root   /var/www/html;
        index  index.php;

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;

    # pass the PHP scripts to FastCGI server listening on
    location ~ \.php$ {
        root           html;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        include        fastcgi_params;

What is wrong there?

My answer:

You have placed your root directives in the wrong places.

root should be defined in the server block, not in each location block. This is one of the most common nginx misconfigurations.

To resolve the issue, remove all of the root directives from each location block, and place the correct root directive within the server block, not within any location.

The only reason to have root in a location block is when you actually want a different document root for that location, such as for serving the nginx default error messages (as you are doing here).

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.