Benjamin Harel
Benjamin Harel

Reputation: 2946

Try_files does not hit PHP ( NginX configuration)

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;
}

Upvotes: 10

Views: 22131

Answers (3)

Ville
Ville

Reputation: 4346

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.

Upvotes: 0

Benjamin Harel
Benjamin Harel

Reputation: 2946

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;
    }

Upvotes: 3

amitchhajer
amitchhajer

Reputation: 12830

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

Upvotes: 1

Related Questions