I have a blog hosted at http://site.com/blog.

How do I instruct nginx to rewrite requests from site.com to site.com/blog?

This should not be permanent.

link|improve this question

56% accept rate
Presuming all URLs and keep the relative path, you should be able to just add rewrite ^/(.*)$ /blog/$1 redirect to the top of your server block. – cyberx86 Nov 27 '11 at 6:15
@cyberx86 Infinite redirect loop, that. – Shane Madden Nov 27 '11 at 6:24
Now that was just careless of me - I should know better. Replacing, 'redirect' with 'last' doesn't generate an infinite loop, but still isn't quite ideal (it keeps the URL, but serves the right file). – cyberx86 Nov 27 '11 at 6:50
feedback

1 Answer

up vote 2 down vote accepted
location = / {
    rewrite ^ http://site.com/blog/ redirect;
}

This'll just do requests specifically for the root. If you need to catch everything (redirect http://site.com/somearticle/something.html to http://site.com/blog/somearticle/something.html), then you'll need something more involved:

location /blog/ {
    # Empty; this is just here to avoid redirecting for this location,
    # though you might already have some config in a block like this.
}
location / {
    rewrite ^/(.*)$ http://site.com/blog/$1 redirect;
}
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.