【问题标题】:caching the redirects in varnish with apache backend使用 apache 后端在清漆中缓存重定向
【发布时间】:2018-01-26 07:46:20
【问题描述】:

我有一个 apache 服务器在与清漆服务器不同的主机上运行。如果我们点击 apache 服务器的 ip,它会重定向到 apache_ip/index.php/Main_Page。

由于这个 apache 服务器是 varnish 的后端服务器,我总是从 varnish 收到 301 响应。

sh-4.3# curl 172.16.217.59 -v
* Rebuilt URL to: 172.16.217.59/
*   Trying 172.16.217.59...
* TCP_NODELAY set
* Connected to 172.16.217.59 (172.16.217.59) port 80 (#0)
> GET / HTTP/1.1
> Host: 172.16.217.59
> User-Agent: curl/7.52.1
> Accept: */*
> 
< HTTP/1.1 301 Moved Permanently
< Date: Thu, 17 Aug 2017 18:00:03 GMT
< Server: Apache/2.4.27 (Debian)
< X-Content-Type-Options: nosniff
< Vary: Accept-Encoding,Cookie
< Cache-Control: s-maxage=1200, must-revalidate, max-age=0
< Last-Modified: Thu, 17 Aug 2017 18:00:03 GMT
< Location: http://172.16.217.173/index.php/Main_Page
< Content-Length: 0
< Content-Type: text/html; charset=utf-8
< X-Varnish: 32789 32787
< Age: 1003
< Via: 1.1 varnish (Varnish/5.0)
< Connection: keep-alive
< 
* Curl_http_done: called premature == 0
* Connection #0 to host 172.16.217.59 left intact

如何配置清漆服务器来处理这个重定向?我希望转到重定向位置并缓存其内容。

【问题讨论】:

    标签: apache redirect varnish


    【解决方案1】:

    我认为让清漆跟随重定向不是一个好主意。 301 响应旨在由客户端解释,而不是真正由代理解释,即使有可能也是如此。

    更好的方法是使用 Dimas 所说的主机名。

    创建 yoursite.com 让它指向你的清漆 在您的 apache 配置中注册它 使其成为 apache 上的默认主机名(因此重定向包含它而不是服务器 ip)

    其他解决方案是 hack。您可以:在 varnish 中,当您有后端响应时,解析它并更改位置标头以用您的 varnish 服务器替换您的 apache 服务器 ip,或者实施重试逻辑并遵循重定向(仍在 backend_response 子例程中)。

    【讨论】:

      【解决方案2】:

      如果认为最好的解决方案是使用主机名,例如 something.com,并将其指向您的 Varnish 服务器并配置 Apache 服务器以识别它。

      但是如果你不能这样做,你可以改变你的 Varnish VCL 配置来重写请求来改变主机头,并从响应中改变 Location 头,这样的事情应该可以解决问题:

      sub vcl_rev {
        set req.http.host = "172.16.217.173";
      } 
      
      sub vcl_fetch {
        if (beresp.status == 301 || beresp.status == 302) {
          set beresp.http.Location = regsub(beresp.http.Location, "172\.16\.217\.59", "172.16.217.213");
        }
      }
      

      我没有测试上面的代码,但它应该可以工作。

      这样,Apache 将始终接收正确的 Host 标头,并且 Varnish 将使用 Varnish IP 地址而不是来自 Apache 的 IP 地址发送重定向

      【讨论】:

      • @ShivamMitra 我仍然认为最好的方法是使用主机名,这样可以省去你在 VCL 上弄乱规则的麻烦,但你应该能够通过我的 lats 编辑来解决问题跨度>
      猜你喜欢
      • 2017-07-21
      • 1970-01-01
      • 2013-12-28
      • 1970-01-01
      • 2018-07-18
      • 2013-03-13
      • 2014-01-09
      • 2023-04-11
      • 1970-01-01
      相关资源
      最近更新 更多