reverse-proxy – Varnish作为Rubygems的反向代理服务器

我想为 Rubygems设置缓存服务器,因为我目前在越南,而国际互联网连接速度很慢.我一直试图通过Varnish完成这项工作,但经过数小时的谷歌搜索和尝试各种各样的事情,我仍然卡住了,无法让它正常工作.

我的目标

这是我安装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

我想设置一个反向代理缓存服务器(例如rubygems.mydomain.com),我可以在其中执行以下请求,缓存服务器将在内部跟踪任何重定向.

rubygems.mydomain.com/latest_specs.4.8.gz

重定向位置将链接到各个域(一些rubygems子域,Amazon S3,rubygems镜像).

当前状态

在摆弄了Nginx之后,我发现this blog post与我想要达到的非常接近.但是,我对Varnish如何使其正常工作知之甚少.

那是我当前的配置文件

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>

这是请求的相应syslog输出

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的请求工作正常,但是重定向后不能按预期工作.如果有人能指出我正确的方向,我会很高兴的.

解决方法

此时,当您从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);
   }
}

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...