清漆尾随斜线改写

时间:2017-11-09 13:44:24

标签: varnish-vcl varnish-4

我试图设置清漆来处理我的默认网址,即example.url.co.uk。

如果它命中example.url.co.uk或example.url.co.uk/我希望它重定向或重写到example.url.co.uk/site,它在tomcat应用程序中将其发送到登录页面。

sub vcl_recv {

if ((req.url ~ "") || (req.url ~"^/")) {
       set req.http.location = "https://example.url.co.uk/site" + req.url;
       return (synth(750, "Found"));
     }

}


sub vcl_synth {
    if (resp.status == 301) {
        set resp.http.Location = req.http.x-redir;
        return (deliver);
    }

    if (resp.status == 750) {
        set resp.http.Location = req.http.location;
        set resp.status = 302;
        return (deliver);
    }

}

然而,当我使用我的尝试时,我得到了example.url.co.uk/site/site/site/site ...

所以我显然陷入了困境,我一直在试图寻找合适的解决方案一周。请保存我自己的愚蠢,我确定!

1 个答案:

答案 0 :(得分:0)

问题是req.url ~ ""匹配everthing。因为它被用作正则表达式。你的req.url ~ "^/"太开放了。

将其更改为:

if (req.url == "" || req.url ~"^/$") {

应该修复它,验证它是空的还是/它自己。

这是一个简单的varnishtest来验证行为:

varnishtest "Test Redirect"

server s1 {
    rxreq
    txresp
    rxreq
    txresp
} -start

varnish v1 -vcl+backend { 


sub vcl_recv {

if (req.url == "" || req.url ~"^/$") {
       set req.http.location = "https://example.url.co.uk/site" + req.url;
       return (synth(750, "Found"));
     }

}


sub vcl_synth {
    if (resp.status == 301) {
        set resp.http.Location = req.http.x-redir;
        return (deliver);
    }

    if (resp.status == 750) {
        set resp.http.Location = req.http.location;
        set resp.status = 302;
        return (deliver);
    }

}


} -start

client c1 {
    txreq -url "/" 
    rxresp
    expect resp.status == 302
} -run


client c1 {
    txreq -url "/site" 
    rxresp
    expect resp.status == 200
} -run

您可以使用varnishtest test.vtc

运行它