Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

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;
}
share|improve this answer
    
what is the ~ for? –  user1561108 Nov 29 '12 at 1:20
1  
Check this page for the various regex options: wiki.nginx.org/HttpCoreModule#location –  jagsler Nov 29 '12 at 1:30
    
@user1561108 "~" for case sensitive matching #1 –  jberger Nov 7 '13 at 21:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.