I would like to serve example.com/mypage.html from example.com/mypage.

Within nginx.conf I tried putting mypage.html in designated root directory and then

location /mypage}
 alias mypage.html;

But no dice. Must be something similar?

Edit: If I put entire path from '/' in alias it works but I would rather have it relative from the set root directory. How do I reference the root dir I have defined earlier in the config with:

root /usr/share/nginx/www;

Further, when I do this and the client makes a request for /mypage, nginx defaults to octet stream filetype. How do I force text/html for URLs I define like this?


You can add the following to your location / block to make it work for all pages. This will first try to serve the requested uri, if that doesn't work it will try to serve it with an html extension (without adding it to the url)

location / {
    try_files $uri $uri.html $uri/ /index.html;

or if you want it only to work with one page you can add the following to your server block

location ~ /mypage {
    try_files $uri.html /index.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.