5

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?

3

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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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