【问题标题】:Varnish Redirection清漆重定向
【发布时间】:2013-03-13 21:26:35
【问题描述】:

最近我从 PHP 平台迁移到基于 Java 的新系统。新网站有漂亮的 URL,例如 -

http://mysite.com/science/2013/03/22/universe-is-older-than-previously-thought

旧网站的网址如 -mysite.com/details.php?cid=37&id=239411

对于搜索引擎结果,我们需要将所有这些包含 /details.php? 的 URL 重定向到主页,例如 urlredirect.com。我一直在查看这些示例 https://www.varnish-cache.org/trac/wiki/VCLExampleRedirectInVCL 并在我的 Varnish 配置的 redirect.vcl 中提出以下内容。

在 vcl_recv 函数中 -

 if(req.url~ "^/details.php?$" ) {
 error 301 "Moved Temporarily";
  }

但我很困惑 vcl_error 函数中应该有什么?目前是这样的-

  else if(obj.status == 301 && req.url~ "^/details.php?$"){
    set obj.http.Location = "http://bdnews24.com";
    return (deliver);
  }

我觉得就这么简单?与做过这件事的人分享经验仍然很棒。

【问题讨论】:

    标签: http url redirect varnish


    【解决方案1】:

    如果你想在 Varnish 4.0 中做到这一点,那么做到这一点的方式已经改变了一点

    #default.vcl
    
    sub vcl_recv {
      if (req.req.url~ "^/details.php?$") {
        return (synth (750, "")); #This throws a synthetic page so the request won't go to the backend
      }
    }
    
    sub vcl_synth {
      if (resp.status == 750) {
        set resp.status = 301;
        set resp.http.Location = "http://bdnews24.com";
        return(deliver);
      }
    }
    

    【讨论】:

      【解决方案2】:

      最好创建一个自定义错误代码,然后将重定向 URL 发送到那里,而不必在 vcl_error 中重复自己。一个简短的例子:

      在 vcl_recv 中:

      set req.http.x-Redir-Url = "http://newdomain.com" + req.url; 
      error 750 req.http.x-Redir-Url;
      

      在 vcl_error 中:

      if (obj.status == 750) {
          set obj.http.Location = obj.response;
          set obj.status = 301;
          return(deliver);
      }
      

      【讨论】:

      • 谢谢。是的,这样做更安全。
      • 请注意,这在 varnish 4 中不起作用! - 查看其他答案。
      猜你喜欢
      • 2016-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-28
      • 1970-01-01
      • 2017-05-29
      • 2012-04-12
      • 1970-01-01
      相关资源
      最近更新 更多