Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

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.

share|improve this answer
    
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: serverfault.com/questions/406158/nginx-php5-fpm-file-not-found/… - that article is great! –  Steve Jan 20 at 1:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.