# Route non-existant .php requests through try\_files?

**URL:** <https://www.ruby-forum.com/t/route-non-existant-php-requests-through-try-files/220645>\
**Category:** NGINX\
**Created:** [June 15, 2012, 5:24am UTC](https://www.ruby-forum.com/t/route-non-existant-php-requests-through-try-files/220645 "2012-06-15T05:24:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [June 15, 2012, 5:24am UTC](https://www.ruby-forum.com/t/route-non-existant-php-requests-through-try-files/220645/1 "2012-06-15T05:24:47Z")

</div>

Hi everyone,  
I’m investigating moving one of my sites from another web server to  
Nginx. The app is a PHP app that uses the “front controller” pattern  
(everything routes via index.php), and the app takes care of URL routing  
and handling 404 errors.

# My nginx site configuration looks something like this:

server {  
listen 8080;  
server\_name [example.com](http://example.com);  
root /var/www/example/live/public/;  
index index.htm index.php;

location / {  
try\_files $uri /index.php;  
}

location /assets/cache/ {  
expires max;  
}

location ~ .php$ {  
include fastcgi\_params;  
fastcgi\_pass php;  
fastcgi\_param FUEL\_ENV production;  
}  
}

# upstream php { server unix:/var/run/php5-fpm/www.sock; }

This works mostly as expected - Going to the root of the site hits the  
app’s homepage, and the sub pages work (so [example.com/about](http://example.com/about) loads  
file). However if I go to a non-existent PHP file (like  
[example.com/about.php](http://example.com/about.php)), I get a white page that says “File not found.”  
There’s some legacy links that have “.php” at the end and routing these  
to the correct location is handled in the app itself. Because of my  
try\_files setting, I expected this to go through index.php, like the  
URLs without extensions. However this doesn’t seem to be the case.  
Anyone know how to configure it so it works this way?

tl;dr: How do I get requests to non-existent .php files to route via my  
index.php file?

Posted at Nginx Forum:

> **[Route non-existant .php requests through try\_files?](https://forum.nginx.org/read.php?2,227550,227550#msg-227550)**
>
> Hi everyone, I'm investigating moving one of my sites from another web server to Nginx. The app is a PHP app that uses the "front controller" pattern (everything routes via index.php), and the app takes care of URL routing and handling 404...

---

<div class="post-metadata">

**Author:** ![Daniel15](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecccb3/32.png) [@Daniel15](https://www.ruby-forum.com/u/Daniel15)\
**Post date:** [June 15, 2012, 9:55am UTC](https://www.ruby-forum.com/t/route-non-existant-php-requests-through-try-files/220645/2 "2012-06-15T09:55:22Z")

</div>

On Thu, Jun 14, 2012 at 11:24:12PM -0400, Daniel15 wrote:

Hi there,

> location / {

> location /assets/cache/ {

> location ~ .php$ {

> However if I go to a non-existent PHP file (like  
> [example.com/about.php](http://example.com/about.php)), I get a white page that says “File not found.”  
> There’s some legacy links that have “.php” at the end and routing these  
> to the correct location is handled in the app itself. Because of my  
> try\_files setting, I expected this to go through index.php,

No.

One request is handled by one location. The request for /about.php  
will be handled by the “php” regex location, which doesn’t (currently)  
include try\_files.

> **[Module ngx\_http\_core\_module](http://nginx.org/en/docs/http/ngx_http_core_module.html#location)**
>
> This directive appeared in version 1.11.8.

Untested, but probably either:

copy the try\_files line into the “php” regex location; or

move the “php” regex location to be within the “location /” block

should do what you want in most cases.

(“most” because: what do you want to happen when the request is for  
“/assets/cache/absent.php”, or for “/assets/cache/present.php”? The two  
options above will do different things.)

Good luck with it,

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Daniel15](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecccb3/32.png) [@Daniel15](https://www.ruby-forum.com/u/Daniel15)\
**Post date:** [June 16, 2012, 5:59am UTC](https://www.ruby-forum.com/t/route-non-existant-php-requests-through-try-files/220645/3 "2012-06-16T05:59:06Z")

</div>

Thanks for your reply. Adding the try\_files setting into the PHP  
location block made it behave as expected

Thanks! 🙂

## Francis D. Wrote:

> No.
> 
> “/assets/cache/absent.php”, or for  
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://mailman.nginx.org/mailman/listinfo/nginx)

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,227550,227587#msg-227587](http://forum.nginx.org/read.php?2,227550,227587#msg-227587)
