作为Rubygems的反向代理服务器清漆

我想为Rubygems设置caching服务器,因为我目前在越南,国际互联网连接速度很慢。 我一直试图通过清漆来完成这个工作,但经过几个小时的search和尝试各种事情,我仍然卡住,无法正常工作。

我的目标

这是一个示例请求组,当我安装一个gem时:

GET http://api.rubygems.org/latest_specs.4.8.gz 302 Moved Temporarily GET http://s3.amazonaws.com/production.s3.rubygems.org/latest_specs.4.8.gz 200 OK 

我想设置一个反向代理caching服务器(例如rubygems.mydomain.com),我可以执行以下请求,caching服务器将在内部遵循任何redirect。

 rubygems.mydomain.com/latest_specs.4.8.gz 

redirect位置将链接到各个域(一些rubygems子域,Amazon S3,rubygems镜像)。

当前状态

经过nginx的摆弄之后,我发现这个博客文章非常接近我想达到的目标。 但是,我对于Varnish如何正确工作知之甚less。

这是我目前的configuration文件

 import std; backend rubygems { .host = "rubygems.org"; .port = "80"; } sub vcl_recv { std.syslog(180, "RECV: " + req.http.host + req.url); if (!req.url ~ "^http") { std.syslog(180, "FETCH"); set req.backend = rubygems; return (lookup); } } sub vcl_fetch { if (beresp.status == 302) { set beresp.http.X-Magic-Redirect = "1"; return(deliver); } } sub vcl_hit { if (obj.http.X-Magic-Redirect == "1") { set req.url = obj.http.Location; return (restart); } } sub vcl_deliver { if (resp.http.X-Magic-Redirect == "1") { unset resp.http.X-Magic-Redirect; return(restart); } return(deliver); } 

我可以执行一个请求,但是它会响应一个错误:

 curl -is http://localhost:8080/latest_specs.4.8.gz HTTP/1.1 302 Found Server: Varnish Content-Type: text/html; charset=utf-8 Retry-After: 5 Content-Length: 376 Accept-Ranges: bytes Date: Sat, 01 Feb 2014 02:33:47 GMT X-Varnish: 933109322 Age: 1 Via: 1.1 varnish Connection: close <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <title>302 Found</title> </head> <body> <h1>Error 302 Found</h1> <p>Found</p> <h3>Guru Meditation:</h3> <p>XID: 933109322</p> <hr> <p>Varnish cache server</p> </body> </html> 

这就是请求的相应系统日志输出:

 Jan 31 18:33:46 precise64 varnishd[2387]: RECV: localhost:8080/latest_specs.4.8.gz Jan 31 18:33:46 precise64 varnishd[2387]: FETCH Jan 31 18:33:47 precise64 varnishd[2387]: RECV: localhost:8080/latest_specs.4.8.gz Jan 31 18:33:47 precise64 varnishd[2387]: FETCH Jan 31 18:33:47 precise64 varnishd[2387]: RECV: localhost:8080http://production.s3.rubygems.org/latest_specs.4.8.gz 

所以,对Rubygems的请求工作正常,但redirect后不能按预期工作。 如果有人能把我指向正确的方向,我会很高兴。

此时,当您从rubygems后端接收到302状态时,您必须在响应中由HTTP标头位置指定的新位置再次发出请求。

你应该得到这样的结果:

 vcl_fetch { if (beresp.status == 302) { /* The content is on another location */ /* First change the host of the request*/ set req.http.host = regsub(regsub(beresp.http.Location, "^http://", ""), "^([^/]+)/.*$", "\1"); /* Then change the url of the request */ set req.url = regsub(beresp.http.Location, "^http://[^/]+/(.*)$", "/\1"); return (restart); } } 

当我联系愤怒caching代理的作者时,她回答说:

不幸的是你的关注是正确的 两年前,我已经离开了公司,因为我维护这个公司的兴趣相当低。 而且似乎没有其他人把它捡起来了…

如果你得到这个工作,请分享。
特别是它是否与捆绑器依赖协议一起工作。

这里有一些很好的信息或者,你可能想看看geminabox