0

My nginx config looks like this

server {
        listen       80;
        server_name  localhost;

        location / {
           root /var/www;
           index index.php index.html;
           autoindex on; 
        }

        location /folder1 {
           root   /var/www/folder1;
           index  index.php index.html index.htm;
           try_files $uri $uri/ index.php?$query_string;
        }

        location /folder2 {
           root   /var/www/folder2;
           index  index.php index.html index.htm;
        }

        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            include        fastcgi_params;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

The problem with the above setup is that i am not able to execute php files. Now as per my understanding of nginx config rules, when i am in my webroot(/) which is /var/www the value of $document_root becomes /var/www so when i request for localhost/hi.php the fastcgi_param SCRIPT_FILENAME becomes /var/www/hi.php and that is the actual path of the php script. But when i actully go to the url localhost/hi.php i get a message File not found. But when I go to the url localhost/hi.html i can see the html output of the page. So this means that there is a problem with the location block of php.

Similarly when i request for localhost/folder1/hi.php the $document_root becomes /var/www/folder1 because this is specified as the root in folder's location block so again the fastcgi_param SCRIPT_FILENAME becomes /var/www/folder1/hi.php. But when i actully go to the url localhost/folder1/hi.php i get a message File not found. But when I go to the url localhost/folder1/hi.html i can see the html output of the page. So again php are not being executed.

Please help?

lovesh
  • 183
  • 1
  • 10

1 Answers1

2

You need to remove the root lines from the two location /folder* blocks, and move the root /var/www to within the server block, not the location / block. This is one of the most common nginx misconfigurations.

Michael Hampton
  • 244,070
  • 43
  • 506
  • 972
  • I read the link you pointed to but could not get this line `If you add a root to every location block then a location block that isn't matched will have no root`. If a location block is not matched then whether it has a root or not shouldn't matter. And in my above config all location blocks i specify have a root – lovesh Sep 25 '12 at 21:51
  • Heck I'll give you +1 here for the same reason as I did here: http://serverfault.com/questions/406158/nginx-php5-fpm-file-not-found/406169#406169 - that article is great! – Ben Jan 20 '14 at 01:24