Sign up ×
Server Fault is a question and answer site for 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

he gets redirected to

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

any advice?


share|improve this question

1 Answer 1

up vote 1 down vote accepted

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

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

location / {
    rewrite ^

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

location @rewrite {
    rewrite ^

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


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.