try_files ignored in combination with error_page and named locations

Džen yvldwt at
Wed Apr 4 17:27:35 UTC 2012

On 04/04/2012 11:03, Edho Arief wrote:
> location /maintenance-static/ {
> }
> location / {
>    try_files /$hostid-maintenance/ @backend;
> }

Right, this would work just fine. However, requests to /maintenance-static/
would still be possible even if /$hostid-maintenance/ doesn't exist (minor
problem, but maybe the backend uses /maintenance-static/ for some reason).

Unfortunately, I still haven't found a solution which pleases me. Just to
clarify, this is how I want the request to be processed (el-cheapo pseudo

	if the directory /maintenance/ exists, then
		set root to /maintenance/
		if the file $uri exists, then
			serve $uri with errcode 503
			serve index.html with errcode 503
		proxy_pass http://backend

My new attempt:

	recursive_error_pages on;
	error_page 599 @maintenance;

	if ( -d $document_root/$hostid-maintenance ) {
		return 599;

	location @maintenance {
		root		/var/nginx/$hostid-maintenance;
		index		index.html;
		try_files	$uri $uri/ /index.html /;

I used error code 599 because I noticed that for some reason the backend 
raise an 503 error although $document_root/$hostid-maintenance doesn't 
The result in such a situation would be unexpected (an infinite redirect 
By using an unused error code like 599, nginx's default 503 error page 
will be
displayed instead, if the backend raises 503.

This configuration seems to work, but the 599 error code in the HTTP header
isn't that nice. Unfortunately, something like

	error_page 599 =503 @maintenance;

doesn't result in what I expected (it will again just return nginx's default
503 error page, although error 503 is correctly set in the HTTP header).

I still think that I misunderstood something, am I right?

Kind regards


More information about the nginx mailing list