Varnish / NGINX基于时间的限制

时间:2018-04-25 11:37:15

标签: php mysql apache nginx varnish

我正在尝试为CMS实施基于时间和地理位置的限制。以下是当前设置的样子

  • Nginx(端口:443):用于SSL终止和URL操作
  • 清漆(端口:80):从后端缓存动态内容
  • Apache,PHP,MySQL:自定义CMS在此堆栈上运行

要求是这样的:在特定时间段内,来自特定国家/地区的流量无法访问特定的URL /类别,但来自世界其他地方的流量可以无任何限制地访问相同的URL /类别。

地理位置部分似乎并不令人生畏,但我无法找到任何基于时间限制流量的信息。

当然,我可以设置两个清漆或nginx配置,其中一个配置没有限制,有一个cron作业根据我想要做什么来替换物理文件,但我希望有一个更清洁的解决方案。

据我所知,所有这些都可以用PHP处理,但是我不能从堆栈中删除Varnish,我必须安装5-6台服务器来处理负载,并且会增加我不能的成本证明。

帮助?

1 个答案:

答案 0 :(得分:0)

你可以在清漆中使用now,并在清漆中使用日期来制作403或其他

这是一个示例varnishtest案例,展示了如何使用now

varnishtest "Time Gate"

server s1 {
       rxreq
       txresp

} -start

varnish v1 -vcl+backend {
    import std;

    sub vcl_recv {
        //now is in RFC format: Thu, 26 Apr 2018 08:40:22 GMT
        //set it in a "ghost header" to convert it to a STRING otherwise it is a TIME
        // and regex won't work
        set req.http.now_string = now;

        //Check the hour
        if(req.http.now_string ~ "^.* [0-9]{4} 08") {
              // Do stuff when its 08:xx
        } else {
             // Otherwise do something else.
        }


    }

    sub vcl_deliver {
        set resp.http.x-forwarded-for = client.ip;
        set resp.http.now_string = req.http.now_string;
    }
} -start

client c1 {
    txreq  -url "/1"
    rxresp
    expect resp.http.now_string ~  "2018"


} -run

关于地理保护,您也可以使用https://github.com/varnish/libvmod-geoip此vmod在清漆中执行此操作。 (没有亲自测试)

相关问题