<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
<title>Route non-existant .php requests through try_files?</title>
<description>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 &amp;quot;front controller&amp;quot; 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;
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 loads file). However if I go to a non-existent PHP file (like example.com/about.php), I get a white page that says &amp;quot;File not found.&amp;quot; There's some legacy links that have &amp;quot;.php&amp;quot; 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?</description><link>http://forum.nginx.org/read.php?2,227550,227550#msg-227550</link><lastBuildDate>Mon, 20 May 2013 17:50:23 -0400</lastBuildDate>
<generator>Phorum 5.2.16</generator>
<item>
<guid>http://forum.nginx.org/read.php?2,227550,227587#msg-227587</guid>
<title>Re: Route non-existant .php requests through try_files?</title><link>http://forum.nginx.org/read.php?2,227550,227587#msg-227587</link><description><![CDATA[Thanks for your reply. Adding the try_files setting into the PHP location block made it behave as expected<br /><br />Thanks! :)<br /><br />Francis Daly Wrote:<br />-------------------------------------------------------<br />&gt; On Thu, Jun 14, 2012 at 11:24:12PM -0400, Daniel15<br />&gt; wrote:<br />&gt;<br />&gt; Hi there,<br />&gt;<br />&gt; &gt; location / {<br />&gt;<br />&gt; &gt; location /assets/cache/ {<br />&gt;<br />&gt; &gt; location ~ \.php$ {<br />&gt;<br />&gt; &gt; However if I go to a non-existent PHP file (like<br />&gt; &gt; example.com/about.php), I get a white page that<br />&gt; says &quot;File not found.&quot;<br />&gt; &gt; There's some legacy links that have &quot;.php&quot; at<br />&gt; the end and routing these<br />&gt; &gt; to the correct location is handled in the app<br />&gt; itself. Because of my<br />&gt; &gt; try_files setting, I expected this to go through<br />&gt; index.php,<br />&gt;<br />&gt; No.<br />&gt;<br />&gt; One request is handled by one location. The<br />&gt; request for /about.php<br />&gt; will be handled by the &quot;php&quot; regex location, which<br />&gt; doesn't (currently)<br />&gt; include try_files.<br />&gt;<br />&gt; http://nginx.org/r/location<br />&gt;<br />&gt; Untested, but probably either:<br />&gt;<br />&gt; copy the try_files line into the &quot;php&quot; regex<br />&gt; location; or<br />&gt;<br />&gt; move the &quot;php&quot; regex location to be within the<br />&gt; &quot;location /&quot; block<br />&gt;<br />&gt; should do what you want in most cases.<br />&gt;<br />&gt; (&quot;most&quot; because: what do you want to happen when<br />&gt; the request is for<br />&gt; &quot;/assets/cache/absent.php&quot;, or for<br />&gt; &quot;/assets/cache/present.php&quot;? The two<br />&gt; options above will do different things.)<br />&gt;<br />&gt; Good luck with it,<br />&gt;<br />&gt; f<br />&gt; --<br />&gt; Francis Daly francis@daoine.org<br />&gt;<br />&gt; _______________________________________________<br />&gt; nginx mailing list<br />&gt; nginx@nginx.org<br />&gt; http://mailman.nginx.org/mailman/listinfo/nginx]]></description>
<dc:creator>Daniel15</dc:creator>
<category>Nginx Mailing List - English</category><pubDate>Fri, 15 Jun 2012 23:58:28 -0400</pubDate></item>
<item>
<guid>http://forum.nginx.org/read.php?2,227550,227553#msg-227553</guid>
<title>Re: Route non-existant .php requests through try_files?</title><link>http://forum.nginx.org/read.php?2,227550,227553#msg-227553</link><description><![CDATA[On Thu, Jun 14, 2012 at 11:24:12PM -0400, Daniel15 wrote:<br /><br />Hi there,<br /><br />&gt; location / {<br /><br />&gt; location /assets/cache/ {<br /><br />&gt; location ~ \.php$ {<br /><br />&gt; However if I go to a non-existent PHP file (like<br />&gt; example.com/about.php), I get a white page that says &quot;File not found.&quot;<br />&gt; There's some legacy links that have &quot;.php&quot; at the end and routing these<br />&gt; to the correct location is handled in the app itself. Because of my<br />&gt; try_files setting, I expected this to go through index.php,<br /><br />No.<br /><br />One request is handled by one location. The request for /about.php<br />will be handled by the &quot;php&quot; regex location, which doesn't (currently)<br />include try_files.<br /><br />http://nginx.org/r/location<br /><br />Untested, but probably either:<br /><br />copy the try_files line into the &quot;php&quot; regex location; or<br /><br />move the &quot;php&quot; regex location to be within the &quot;location /&quot; block<br /><br />should do what you want in most cases.<br /><br />(&quot;most&quot; because: what do you want to happen when the request is for<br />&quot;/assets/cache/absent.php&quot;, or for &quot;/assets/cache/present.php&quot;? The two<br />options above will do different things.)<br /><br />Good luck with it,<br /><br />f<br />--<br />Francis Daly francis@daoine.org<br /><br />_______________________________________________<br />nginx mailing list<br />nginx@nginx.org<br />http://mailman.nginx.org/mailman/listinfo/nginx]]></description>
<dc:creator>Francis Daly</dc:creator>
<category>Nginx Mailing List - English</category><pubDate>Fri, 15 Jun 2012 03:56:01 -0400</pubDate></item>
<item>
<guid>http://forum.nginx.org/read.php?2,227550,227550#msg-227550</guid>
<title>Route non-existant .php requests through try_files?</title><link>http://forum.nginx.org/read.php?2,227550,227550#msg-227550</link><description><![CDATA[Hi everyone,<br />I'm investigating moving one of my sites from another web server to Nginx. The app is a PHP app that uses the &quot;front controller&quot; pattern (everything routes via index.php), and the app takes care of URL routing and handling 404 errors.<br /><br />My nginx site configuration looks something like this:<br />=====<br />server {<br />listen 8080;<br />server_name example.com;<br />root /var/www/example/live/public/;<br />index index.htm index.php;<br /><br />location / {<br />try_files $uri /index.php;<br />}<br /><br />location /assets/cache/ {<br />expires max;<br />}<br /><br />location ~ \.php$ {<br />include fastcgi_params;<br />fastcgi_pass php;<br />fastcgi_param FUEL_ENV production;<br />}<br />}<br /><br />upstream php {<br />server unix:/var/run/php5-fpm/www.sock;<br />}<br />=====<br /><br />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 loads file). However if I go to a non-existent PHP file (like example.com/about.php), I get a white page that says &quot;File not found.&quot; There's some legacy links that have &quot;.php&quot; 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?<br /><br />tl;dr: How do I get requests to non-existent .php files to route via my index.php file?]]></description>
<dc:creator>Daniel15</dc:creator>
<category>Nginx Mailing List - English</category><pubDate>Thu, 14 Jun 2012 23:24:12 -0400</pubDate></item>
</channel>
</rss>