views:

404

answers:

1

We have a blog that we host on github with Jekyll; it is there : http://blog.superfeedr.com

Ideally, I want it to be at http://superfeedr.com/blog/ because we need to add some AJAX and we need to avoid the "Same Origin Policy" problems.

We use Nginx on our "main" webserver, and I have the following setup :

location /blog/ {

proxy_pass http://blog.superfeedr.com/;
    proxy_redirect     off;
    proxy_max_temp_file_size 0;

    client_max_body_size       10m;
    client_body_buffer_size    128k;

    proxy_connect_timeout      90;
    proxy_send_timeout         90;
    proxy_read_timeout         90;

    proxy_buffer_size          4k;
    proxy_buffers              4 32k;
    proxy_busy_buffers_size    64k;
    proxy_temp_file_write_size 64k;
}

Unfortunately, as you can see if you go to http://superfeedr.com/blog/ this obviously doesn't work. Oddly enough, we're redirected to Github's homepage.

PS: obviously, we could host the blog on our main server, but the goal is to host it on a different host so that we can almost guarantee it to be online if the site is down...

+3  A: 

First, nginx does not send Host header to the blog.superfeedr.com. This makes it send all the required headers:

proxy_set_header   Host                    blog.superfeedr.com;
proxy_set_header   X-Host                 blog.superfeedr.com;
proxy_set_header   X-Real-IP             $remote_addr;
proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;

Second, some url rewriting required. By some weird reason this depends on the version of nginx you are using. Anyway, for 0.6.x (0.6.32 for me) this should work:

    location /blog {
                rewrite  ^/blog(.*)$ /$1 last;
                error_page 402 = @blog;
                return 402;
    }
    location @blog {
        proxy_pass http://blog.superfeedr.com;

        # the rest of proxying parameters should be here

         proxy_set_header   Host                    blog.superfeedr.com;
         proxy_set_header   X-Host                 blog.superfeedr.com;
         proxy_set_header   X-Real-IP $remote_addr;
         proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
    }

You also need to cover all the paths the blog refers to (css, images etc), e.g.

location /css {
    error_page 402 = @blog;
    return 402;
}

For 0.7.59:

        location /blog {
                set $blog 1;
                rewrite  ^/blog(.*)$ /$1 last;
        }
        location /css {
                set $blog 1;
                error_page 402 = @blog;
                return 402;
        }
        location / {
                if ($blog) {
                        error_page 402 = @blog;
                        return 402;
                }
                # here is where default settings for / should be
                root /usr/local/www/nginx/;
        }
        location @blog {
                proxy_pass http://blog.superfeedr.com;

                # the rest of proxying parameters should be here

                proxy_set_header   Host                   blog.superfeedr.com;
                proxy_set_header   X-Host                 blog.superfeedr.com;
                proxy_set_header   X-Real-IP $remote_addr;
                proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        }
rzab
Sorry that doesn't work...
Julien Genestoux
I've added some corrections to my answer. The first version with just 4 extra lines was plain wrong.
rzab
I am sorry, but it still doesn't work. It might be something I am doing wrong. Here is the full content of our nginx config : http://gist.github.com/138292Now, if you go to http://superfeedr.com/blog/ you get a 404...
Julien Genestoux
The config seems to be fine, except i don't think you really want leave /blog/ instead if /blog (no trailing slash). What version of nginx are you using?
rzab
nginx version: nginx/0.7.59Hum, you said : (i've tried it out) and works (as far as i can tell) with 0.6.32.Did it really work for you? Have you been able to proxy that page? If so, could you give a url?PS/ with or without the traling / didn't change anything.
Julien Genestoux
The last config works fine! great!
Julien Genestoux
Ok. I've cleaned up the answer and deleted my last comment with link (don't want it to be crawled)
rzab