Nginx Proxy for a GitHub Page?
Question
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...
Solution
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; }
OTHER TIPS
Another way to do this (but without involving nginx) could be with a DNS directive. I think most DNS services offer URL forward service.
For example, in hover.com, first add blog
with A
directive to 64.99.80.30
under DNS tab, and then in the Forward tab, add blog
forward to http://superfeedr.com/blog/
In dnsimple.com, it's simpler, just add blog
URL
record to forward to http://superfeedr.com/blog/
These forwards, I believe, also work for https://
type URLs.