I'm currently hosting a couple small static websites using Nginx 1.10.2 on Fedora 25 Server Edition x86_64.
I've configured Nginx to assume the .html
for requests with no file extension (try_files
), and to redirect (permanent rewrite
) to the .html
-less version of a URL if .*\.html
is requested.
I also have custom error pages. As far as I can tell so far, the error_page
directive doesn't cooperate well with rewrites, because I'm getting stuck with redirect loops for pages that should return a normal error message... I believe this is all the pertinent configuration:
server {
[...]
try_files $uri $uri.html $uri/index.html =404;
error_page 400 /errors/400.html;
error_page 401 /errors/401.html;
error_page 403 /errors/403.html;
error_page 404 /errors/404.html;
error_page 408 /errors/408.html;
error_page 500 /errors/500.html;
error_page 503 /errors/503.html;
# Remove "index" from URL if requesting the front page.
rewrite "^/index(\.html)?$" "/" permanent;
# Strip .html file extension, trailing slash, or "index.html".
rewrite "/(.+)(\.html|/|/index.html)$" "/$1" permanent;
[...]
}
Here's what I think Nginx is doing:
- client requests
/fake-page
- look up
/fake-page
,/fake-page.html
, or/fake-page/index.html
. - when none of those match, internally redirect to show Error 404 page.
- but then
/errors/404.html
gets stripped of .html withpermanent
flag, causing a 301 user redirection.
I've tried several variations on that last rewrite
line, even putting it in a location ^~ /errors/ {}
block (which I think should mean that the rewrite only applies to URLs that are not under the /errors/ directory). But everything I've done has resulted in an Error 404 permanently redirecting to the 404 page, which then does not return the actual 404 status --- or it ends up stuck in a redirect loop.