Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NGINX try_files does not pass to PHP

Tags:

php

nginx

I have a very simple PHP site:

.  
├── about.php  
├── index.php  
├── project  
│   ├── project_one.php  
│   └── project_two.php  
└── projects.php  

And the following nginx config (only relevant parts shown):

location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/path/to/php.sock;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_intercept_errors on;
}

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

Hitting the / works as expected. Hitting any of the http://my.site.com/{projects | about | project/*} URLs should use try_files to hit the $uri.php file, and pass it to PHP. But instead, the browser just downloads the PHP file itself.

I can get it to work by adding individual location directives for the above locations, like so:

location /projects {
    try_files $uri $uri/ /$uri.php;
}
location /about {
    try_files $uri $uri/ /$uri.php;
}
location /project {
    try_files $uri $uri/ $uri.php;
}

But this is clearly not the way to do this.

What am I doing wrong???

like image 748
TL-Eugene Avatar asked May 28 '14 16:05

TL-Eugene


People also ask

Can nginx serve PHP files?

Create a PHP page in NginxAfter the restart, PHP is fully enabled on Nginx. To prove this, create a PHP file in Nginx's /var/www/html folder and test to ensure the page renders properly on the server. This creates the most basic PHP file outside of a “Hello World” example you could create.

What does Try_files do in nginx?

The try_files directive exists for an amazing reason: It tries files in a specific order. NGINX can first try to serve the static content, and if it can't, it moves on. This means PHP doesn't get involved at all.

What is Try_files $URI?

The try_file directive is in the server and location blocks and specifies the files and directories in which Nginx should check for files if the request to the specified location is received. A typical try_files directive syntax is as: location / { try_files $uri $uri/ /default/index.html; }

What is $URI in nginx?

It is exactly the $uri/ part that makes nginx assuming an URI can be a directory name and looking for an index file presence inside it.


1 Answers

Per nginx documentation for try_files

Checks the existence of files in the specified order and uses the first found file for request processing; the processing is performed in the current context

so nginx find PHP file and process it in context of location / therefor just serve it as static file. Only last parameter is different, it's not checked but nginx make internal redirect (if it's uri) or return error code (if it's =code). So you need to remove =404 from try_files to have internal redirect. And add try_files to location ~ \.php to make sure that file exists.

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/path/to/php.sock;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_intercept_errors on;
}

location / {
    index index.php;
    try_files $uri $uri/ $uri.php;
}
like image 185
Alexey Ten Avatar answered Oct 27 '22 01:10

Alexey Ten