Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

http://foo.com/bar.html

with

http://foo.com/bar

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

2 Answers 2

up vote 10 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
1  
Changing it to try_files $uri.html $uri/ =404; would be better SEO wise, as you wouldn't have two urls foobar.com/bar and foobar.com/bar.html 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

 
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.