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 server static file with nginx. what i want to do it, that when someone browses to

http://foo.bar.com/

he gets redirected to

http://bar.com

but when i access the static file itself, it should serve that file, like:

http://foo.bar.com/blog/main.css

any advice?

thanks!

share|improve this question

1 Answer 1

up vote 1 down vote accepted

First of all you need 2 server blocks, one for foo.bar and one for bar.

In the foo.bar server block you then specify a couple of locations.

location / {
    rewrite ^ http://bar.com
}

location ~ ^/blog/.+\.(jpg|css|png)$ {
    try_files $uri @rewrite;
}

location @rewrite {
    rewrite ^ http://bar.com
}

Nginx locations work by specificity so the regex location will be used before the location / if the URI matches, there we check if the file exist, and if it does serve it, otherwise we'll redirect any way.

I highly recommend you go read the wiki as all of this is specified there and it should help you understand this thoroughly.

share|improve this answer
    
thanks martin, works perfect! –  Tronic Apr 17 '11 at 10:21

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.