我们有一个博客,我们在github上与Jekyll一起主持; 它在那里:http://blog.superfeedr.com
理想情况下,我希望它在http://superfeedr.com/blog/,因为我们需要添加一些AJAX,我们需要避免"同源策略"问题.
我们在"主"网络服务器上使用Nginx,我有以下设置:
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; }
不幸的是,正如你可以看到你去http://superfeedr.com/blog/这显然不起作用.奇怪的是,我们被重定向到Github的主页.
PS:显然,我们可以在我们的主服务器上托管博客,但目标是将其托管在不同的主机上,以便我们几乎可以保证它在网站停机时在线...
首先,nginx不会将host头发送到blog.superfeedr.com.这使它发送所有必需的标题:
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;
其次,需要一些网址重写.由于一些奇怪的原因,这取决于您使用的nginx的版本.无论如何,对于0.6.x(对我来说是0.6.32),这应该工作:
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; }
您还需要涵盖博客所引用的所有路径(例如,css,图像等)
location /css { error_page 402 = @blog; return 402; }
对于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; }