Understanding location blocks and try files

Jonathan Matthews contact at jpluscplusm.com
Wed Jan 22 21:07:13 UTC 2014

On 22 January 2014 20:36, Francis Daly <francis at daoine.org> wrote:
> On Wed, Jan 22, 2014 at 02:54:35PM -0500, AD7six wrote:
> Hi there,
>>     location ~ \.php$ {
>>     location ~* \.(?:manifest|appcache|html?|xml|json)$ {
>> A valid response where the url is a file:
>> $ curl -i http://nginx.dev/foo.json
>> HTTP/1.1 200 OK
>> An invalid response when passed to php:
>> $ curl -i http://nginx.dev/apples.json
>> HTTP/1.1 404 Not Found
> Why do you think that this request is passed to php?

I /believe/ AD is thinking along these lines:

* I have a server{} level "try_files", which goes $uri, $uri/, /index.php?$args;
* When file.json is present in the server{}-level root, it should be
served (and is) due to try_files trying the "$uri" setting first;
* When file.json is /missing/, the try_files setting should then
result in nginx falling back to the php location, which AD then
expects to do something meaningful with this request ...

... and its this last step which isn't working as expected. I don't
quite have the explanation or docs to hand to say why this won't work,
but this SO page seems to have an interestingly un-up-voted answer at
the bottom of the page:

"You are probably under the delusion that try_files on server level
must work for every request. Not at all. Quite the contrary, it works
only for requests that match no location blocks."

I'd be really interested to get confirmation that that statement is
unequivocally true!


More information about the nginx mailing list