I know there is lots of similar posts out there but still couldn't find the one fits my case.
I have very simple location block which serves static html file but it serve 404 not the file i specified.
location / {
if ($request_uri != /abcd) {
return 500;
}
root /usr/share/nginx/html;
#try_files $uri.html =404;
#try_files index2.html =404;
try_files $uri $uri/ =404;
}
And this is output of ls in root directory
50x.html abcd.html index.html index2.html
As you can see there is abcd.html and index2.html but when i set the file name it can't be reached and shows 404 page.
Only $uri.html
can be resolved and shows the right page. What should i do if i want to serve the static file for the location?
For example i want to server abcd.html
for the request /abcd
and return 500 for the other requests.
All Nginx URIs contain a leading /
.
The file parameter of the try_files
directive requires a leading /
to match the specified path. See try_files
manual page.
The $uri
variable already contains a leading /
.
So:
try_files $uri.html =404;
matches a file because $uri.html
== /abcd.html
try_files index2.html =404;
does not match a file as it's missing a leading /
try_files $uri $uri/ =404;
does not match a file as there is no file called abcd
For example:
root /usr/share/nginx/html;
location / {
return 500;
}
location = /abcd {
try_files /index2.html =404;
}
Assuming that index2.html
always exists, the =404
is never reached.