1

Basically, I'm trying to handle nice URLs mapped to actual HTML files on the server.

I have URLs like so:

/posts/nice-blog-post/

That need to return an HTML file on the filesystem like:

/path/to/site/posts/nice-blog-post.html

The location directive I have so far is:

location ^~ /(.*)/$ {
    alias /path/to/site/$1.html;
}

This sort of works, except only the following URL successfully resolves:

domain.com/posts/nice-blog-post

This URL (with trailing slash) returns a 404:

domain.com/posts/nice-blog-post/

So I have two issues:

  1. How do I get the URL with trailing slash resolving correctly?
  2. More importantly, when nginx handles the alias, it doesn't appear to return any response headers. The HTML file is then downloaded as a file rather than being rendered to the browser.
0

You need to change your regular expression, so it will handle both ways - with and without slash. Should be something like this I suppose:

location ^~ /(.*)/?$ {
    alias /path/to/site/$1.html;
}

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.