【问题标题】:Redirect all png requests as jpg requests in Varnish在 Varnish 中将所有 png 请求重定向为 jpg 请求
【发布时间】:2013-06-29 10:54:57
【问题描述】:

我想将所有 .png 请求重定向到 Varnish VCL 中的 .jpg 请求示例:http://example.com/images/boy.png(或 .PNG)到 Varnish VCL 中的 http://example.com/images/boy.jpg

【问题讨论】:

    标签: varnish varnish-vcl


    【解决方案1】:

    可能有两种情况。

    A.客户端重定向 [1],如果您想告诉客户端的浏览器图像已被移动,请使用它:

    sub vcl_recv {
      # ...
      if (req.url ~ "(?i)\.png$") {
        error 750 "http://" + req.host + regsub(req.url, "(?i)\.png$", ".jpg$");
      }
      # ...
    }
    
    sub vcl_error {
      # ...
      if (obj.status == 750) {
        set obj.http.Location = obj.response;
        set obj.status = 302;
        return(deliver);
      }
      # ...
    }
    

    B.服务器端重写 [2],如果您想在内部更改请求而不告诉客户端,请使用它:

    sub vcl_recv {
      # ...
      if (req.url ~ "(?i)\.png$") {
        set req.url = regsub(req.url, "(?i)\.png$", ".jpg$");
      }
      # ...
    }
    

    PD:请不要重复您的问题

    [1]https://www.varnish-cache.org/trac/wiki/VCLExampleRedirectInVCL

    [2]https://www.varnish-cache.org/trac/wiki/RedirectsAndRewrites

    【讨论】:

      猜你喜欢
      • 2013-02-15
      • 2016-10-23
      • 1970-01-01
      • 2018-09-22
      • 1970-01-01
      • 2017-12-19
      • 2012-12-17
      • 2013-11-26
      • 2016-11-30
      相关资源
      最近更新 更多