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

I'm using S3 to host some static content, and using a bit of url rewriting to make everything work.

This is my conf right now:

server {
    listen   80; ## listen for ipv4
    listen   [::]:80 default ipv6only=on; ## listen for ipv6

    server_name "~^(.+)\.bar\.com$";

    access_log  /var/log/nginx/foobar-star.access.log;

    set $subdomain $1;

    rewrite ^(.*)$ /$subdomain$request_uri?;

    location / {
            proxy_pass http://foo.bar.com.s3.amazonaws.com;
    }
}

My problem is that in case the request is baz.bar.com I want my url to be foo.bar.com.s3.amazonaws.com/baz/index.html, and if it's baz.bar.com/css/whatever.css I want the general case foo.bar.com.s3.amazonaws.com/baz/css/whatever.css that I already cover in the configuration. I can't use $request_filename because S3 always serves something, even to tell you that the key you're requesting doesn't exist, so I need a general rule to append to my url index.html in case it's not present. I believe this is the reason why the normal index directive doesn't work too ...

To make things short: if I try to reach baz.bar.com/index.html everything works like a charm, if I try baz.bar.com/ it (correctly) returns a document that tells me that the key 'baz' doesn't exist. I would like baz.bar.com/ to render my index.html directly, and leave everything else untouched.

Hope I've been clear - I know it's a bit confusing :)

TIA

share|improve this question
    
Sorry for the wall of text, serverfault doesn't let me post more than 2 urls :) –  ngw May 18 '11 at 0:54
    
Are you asking how to automatically add index.html to any requests that end with /? –  kolbyjack May 19 '11 at 0:41
    
That would be one way to do it. So yes, if it's possible to do something like that, that would be very helpful. –  ngw May 19 '11 at 0:56

2 Answers 2

up vote 2 down vote accepted

To automatically add index.html to each request that ends with / before proxying, I think you want something like:

server {
    listen   80; ## listen for ipv4
    listen   [::]:80 default ipv6only=on; ## listen for ipv6

    server_name "~^(.+)\.bar\.com$";

    access_log  /var/log/nginx/foobar-star.access.log;

    set $subdomain $1;

    location / {
        # if the request ends with /, add index.html
        # the break will stop nginx from looping
        rewrite /$ "/$subdomain${uri}index.html" break;

        # else, just prefix $subdomain
        rewrite ^ /$subdomain$request_uri? break;

        proxy_pass http://foo.bar.com.s3.amazonaws.com;
    }
}
share|improve this answer
    
Works wonderfully :) –  ngw May 19 '11 at 21:29

What HTTP error code S3 return when it is unable to find the key? You can use the error code to serve custom error page which would be the index page.

List of S3 error codes http://docs.amazonwebservices.com/AmazonS3/latest/API/

Link to configure custom error pages http://articles.slicehost.com/2008/5/16/ubuntu-hardy-nginx-virtual-host-settings

share|improve this answer
    
Well, I would prefer another solution... Silencing errors doesn't seem a very good design. –  ngw May 18 '11 at 4:45

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.