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.

Unable to execute PHP files on nginx server.

Below is my default.conf file.

#
# The default server
#
server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index index.html index.php index.htm; 
    }

    error_page  404              /404.html;
    location = /404.html {
        root   /usr/share/nginx/html;
    }

    # redirect server error pages to the static page /50x.html
    #
    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 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
        include        fastcgi_params;
    }
}
share|improve this question
    
What does "unable to execute" mean? What URL are you trying to access? What is the HTTP response? What appears in nginx's error log? –  mgorven Apr 25 '12 at 6:46

2 Answers 2

Please make sure you read the Pitfalls page, see

http://wiki.nginx.org/Pitfalls#Multiple_Index_Directives

you should not put those index directives under a location as suggested here, it's not the correct way to do it. (it works but it's wrong)

share|improve this answer
    
Thanks for comment. Your link says it's wrong to input index anywhere, but I don't know, if index included to http section already. It's good advice if you're building your site configuration, but in this case it's better to include it to make sure it will work. –  Dmitry Verkhoturov May 21 '12 at 10:06

You also could to use a try_files directive:

try_files $uri $uri/ /index.php;

And that root line under location ~ \.php$ looks fishy ...

http {
   upstream phpfpm {
      #server unix:/var/run/php5-fpm.sock;
      server 127.0.0.1:9000;
   }
}

server {
         ...
      index index.php;

      location / {
         ...
         error_page 404 = @phpfpm;
         try_files $uri $uri/ /index.php;
      }

      location ~ \.php$ {
         try_files $uri $uri/ /index.php;
         include        fastcgi_params;
         fastcgi_intercept_errors on;

         #fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
         #fastcgi_index index.php;
         fastcgi_pass   phpfpm;
      }

Think that would work, it's not a recipe, you need to work your config into it.

share|improve this answer

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.