Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm serving up a static site via nginx, and my goal is to replace URL's that look like:


The key being no trailing slash. I am currently doing something similar using location aliases but this is tedious because it requires a location block for every file, and it also appends a trailing slash since nginx looks at aliases as directories:

    location / {
        root    /srv/www/foo/public_html;
        index   index.html;

    location /bar1 {
        alias /srv/www/foo/public_html/;
        index bar1.html;

    location /bar2 {
        alias /srv/www/foo/public_html/;
        index bar2.html;

And so on. I've read through the documentation on rewrites, and I can't seem to synthesize what's being said in to what I need it to do. I'm not coming in from an Apache background; nginx is my first foray in to web servers so I'm sure I'm missing something obvious since my HTTP background is weak. Thanks in advance for any help you can provide.

share|improve this question
up vote 12 down vote accepted

try_files should be what you want.

Something like this:

try_files $uri.html $uri $uri/ =404;
share|improve this answer
This worked, with the minor exception that $uri.html was causing a 500. I had to use "${uri}.html". – Doug Stephen Jan 5 '12 at 17:54
Changing it to try_files $uri.html $uri/ =404; would be better SEO wise, as you wouldn't have two urls and pointing to the same resource. – Khaja Minhajuddin Mar 5 '12 at 9:35

Per the comment from @Khaja, the best answer is:

try_files $uri.html $uri/ =404;

So that only one copy of the resource is served (that with no .html extension). You don't want to divide your link strength over multiple URLs serving duplicate content. Find the documentation here.

share|improve this answer

Your Answer


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.