Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Is it possible to use the current status code as a parameter in try_files?

For example, we try to provide a host specific 503 static response, or a server-wide fallback if it wasn't found:

error_page 503 @error503;

location @error503 {
    root /path_to_static_root/;
    try_files /$host/503.html /503.html =503;
}

There are a number of these directives, so it would be convenient to do something like:

error_page 404 @error
error_page 500 @error
error_page 503 @error

location @error {
    root /path_to_static_root/;
    try_files /$host/$status.html /$status.html =$status;
}

But the Variables documentation doesn't list anything that we could use to do this.

Is it possible, or is there an alternative way to do this?

share|improve this question

1 Answer 1

Your examples have infinite loop.

Probably, you want something like this:

error_page 404 @error404;
error_page 500 @error500;
error_page 503 @error503;

location @error404 {
    root /path_to_static_root/;
    try_files /$host/404.html /404.html =404;
}

location @error500 {
    root /path_to_static_root/;
    try_files /$host/500.html /500.html =500;
}

location @error503 {
    root /path_to_static_root/;
    try_files /$host/503.html /503.html =503;
}

..and it will work faster then with variables.

NGINX FAQ: Is there a proper way to use nginx variables to make sections of the configuration shorter, using them as macros for making parts of configuration work as templates?

Update:

The community wiki is not documentation. From the official documentation:

$status - response status (1.3.2, 1.2.2)

@ http://nginx.org/en/docs/http/ngx_http_core_module.html#variables

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.