I have the following location configuration as part of a reverse proxy server:
error_page 503 502 @down;
location @down {
try_files /error/test/$server_name.html /error/test/generic.html;
internal;
}
location /error/ {
root /usr/share/nginx/html/;
internal;
}
For a server block with server_name of events.example.com, I would expect /usr/share/nginx/html/error/test/events.example.com.html
to be served if it exists. Otherwise Nginx should serve /usr/share/nginx/html/error/test/generic.html
.
Nginx is always displaying the generic.html
page, even when the more specific page exists. I thought that it might be that the variable wasn't being evaluated, so I created a file called $server_name.html
in the error directory. The problem still persisted. If I change the try_files line to
index /error/test/$server_name.html
the configuration works as expected.
How can I have Nginx resolve a file based on the $server_name variable and fallback to a generic file if the first file does not exist?
$host
variable instead.$server_name
is usually not what people thing it is.