10

Below is my nginx.conf.

In case of non existing files /index.php is served fine.

But when my URL is /foo/bar => /foo/bar/index.php is served as PHP source code via download.

Any ideas?

try_files $uri $uri/ $uri/index.php /index.php;

location ~ \.php$ { 
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}
5
  • Have you started your fastcgi service? Commented Aug 23, 2012 at 12:48
  • :-) Sure it even serves the last option /index.php if I put non existing URL. Commented Aug 23, 2012 at 12:49
  • 2
    I have the same problem. If I hit a url that maps to a non-existent file, it correctly executes index.php. However, if I place a new php file and attempt to hit that file directly, I get its source code Commented Aug 23, 2012 at 12:50
  • Hmm the only difference I can see between mine and yours is mine isn't inside a location. try_files $uri $uri/ /index.php?$args; Commented Aug 23, 2012 at 12:54
  • in your example above, what heppens when you try /foo/bar/index.php directly? Commented Aug 23, 2012 at 14:07

3 Answers 3

3

Solution was to add index index.php

    index index.php

    try_files $uri $uri/ $uri/index.php /index.php;

    location ~ \.php$ { 
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Actually, the solution is to use a named location as the final parameter to try_files. The problem is that an internal redirect (i.e. an attempt to check if any other regexes such as your PHP handling regex) is only issued for the final fallback parameter. In the configuration in your question, the request for /index.php did not even try to match the regex. The reason your answer works is because index anything will issue an internal redirect if anything is accessed, causing the actual PHP regex to match.
@MahmoudAl-Qudsi could you post an answer showing how to use a named location
1

My config

index index.html index.htm index.php;
location ~ \.php$ {
        fastcgi_pass unix:/tmp/php-fpm.sock;
        include fastcgi_params;
      }

reload nginx and fastcgi both

1 Comment

I'm talking about try_files. Actually I need more patterns to use with try_files like drupal and passenger. But adding index index.php solved this. Thanks.
0

I also had this same issue. When I had the following in the PHP context (location ~ ^(.+\.php)(.*)$ { ...):

try_files $fastcgi_script_name =404;

.. it always returned 404. Finally I read the try_files docs, where it says:

The path to a file is constructed from the file parameter according to the root and alias directives.

I had only defined alias for each location block (and none in the PHP-FPM block, of course), but didn't have an overall root set anywhere, so NGiNX didn't know where to look for a file. Once I set root for the server block, it started working.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.