Debugging `try_files` with 404 as a last resort

Francis Daly francis at daoine.org
Tue Nov 13 23:30:42 UTC 2018


On Tue, Nov 13, 2018 at 05:23:41PM -0500, petecooper wrote:

Hi there,

> Ideally, I would like the following route for `try_files` (in order):
> 
> * $uri (requested URI)
> * $uri/ (requested URI, trailing slash)
> * /index.php?$args (use root `index.php` with args)
> * =404 (Nginx returns 404)

Do you know whether the file that corresponds to the url /index.php exists?

If it does exist, use

  try_files $uri $uri/ /index.php?$args;

If it does not exist, use

  try_files $uri $uri/ =404;

But really, in the latter case, you are probably better off just omitting
the try_files line altogether.

> I do not fully understand why https://example.com/articles results in a 404
> Not Found when `index.php?$args` generates a valid page (200 OK) and
> precedes `=404`. Does the `=` carry some weight?
> 
> I would greatly appreciate a pointer for further reading so I can better
> understand.

http://nginx.org/r/try_files

The final argument to try_files is a uri or =code. "code" is
returned. "uri" is searched for across all locations (due to an internal
redirect).

The other arguments to try_files are files.

Does a file with the name $document_root/index.php?$args (expanding the
two variables) exist? If not, processing will continue until the uri or
=code at the end of the argument list.

	f
-- 
Francis Daly        francis at daoine.org


More information about the nginx mailing list