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 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.

share|improve this question
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

2 Answers 2

up vote 3 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;
share|improve this answer

This has not worked for me. This is what has worked:

  1. Open the NGINX configuration file for your site. Inside of the server block, add the path to your root directory and set the priority order for files:

    root /mnt/www/www.domainname.com;
    index  index.php index.html index.htm;
  2. Create an empty location block before all your other location blocks:

    location /latest {
    # Nothing in here; this is to avoid redirecting for this location
  3. Comment out the root directory directive in your location / {} block and add the redirection so it looks like this:

    location / {
    # root   /mnt/www/www.domainname.com;
    index  index.php index.html index.htm;
    rewrite ^/(.*)$ http://www.domainname.com/latest/$1 redirect;
  4. Make sure that your location ~ .php$ block points its root to

    root /mnt/www/www.domainname.com;

This fixed it for me.

share|improve this answer

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.