在varnish {default.vcl}中添加移动检测后,varnish无法启动

时间:2017-02-19 04:32:51

标签: mobile device varnish

我的网站有一个基本的default.vcl,它正在运行。

我添加了下面的代码来检测并为用户提供不同的主题,如果来自PC或手机。添加以下代码以检测移动设备后,清漆无法启动。

如何解决此问题,以便清漆检测到移动设备,因此如果从移动设备访问移动主题,则可以在同一网址中提供移动主题



    /*
    *
    * First, set up a backend to answer the request if there's not a cache hit.
    *
    */
    backend default {

        # Set a host.
        .host = "xx.xx.xx.xx";

        # Set a port. 80 is normal Web traffic.
        .port = "xxxx";
    }
    /*
    *
    * Next, configure the "receive" subroutine.
    *
    */
    acl admin_ip {
       "xx.xx.xx.xx";
    }
    include "devicedetect.vcl";
    sub vcl_recv {
            call devicedetect;
            if (req.request == "PURGE") {
            if (!client.ip ~ admin_ip) {
                error 405 "You can't do this, muggle!";
            }
                return(lookup);
       }
    if (!req.backend.healthy) {
        unset req.http.Cookie;
      }
    set req.http.X-Forwarded-For = client.ip;

        # Use the backend we set up above to answer the request if it's not cached.
        #set req.backend = default;

        if (req.url ~ "^/user/login" ||
            req.url ~ "^/oc-admin" ||
            req.url ~ "^/item/new" ||
            req.request == "POST")
        {
            return (pass);
        }
        if (req.url ~ "(?i)\.(pdf|asc|dat|txt|doc|xls|ppt|tgz|csv|png|gif|jpeg|jpg|ico|swf|css|js)(\?.*)?$") {
        unset req.http.Cookie;
        unset req.http.Cache-Control;
        unset req.http.Max-Age;
        unset req.http.Pragma;
        unset req.http.Cookie;
      }
       if(req.http.Cookie) {
         if (req.http.Cookie !~ "(sessionid|XXXid)" ) {
           remove req.http.Cookie;
         }
      }
        # Pass the request along to lookup to see if it's in the cache.
        return(lookup);
    }
    /*
    *
    * Next, let's set up the subroutine to deal with cache misses.
    *
    */
    sub vcl_miss {

        # We're not doing anything fancy. Just pass the request along to the
        # subroutine which will fetch something from the backend.
        return(fetch);
    }
    /*
    *
    * Now, let's set up a subroutine to deal with cache hits.
    *
    */
    sub vcl_hit {

        # Again, nothing fancy. Just pass the request along to the subroutine
        # which will deliver a result from the cache.
        return(deliver);
    }
    /*
    *
    * This is the subroutine which will fetch a response from the backend.
    * It's pretty fancy because this is where the basic logic for caching is set.
    *
    */
    sub vcl_fetch {

    if (req.http.X-UA-Device) {
            if (!beresp.http.Vary) { # no Vary at all
                set beresp.http.Vary = "X-UA-Device";
            } elseif (beresp.http.Vary !~ "X-UA-Device") { # add to existing Vary
                set beresp.http.Vary = beresp.http.Vary + ", X-UA-Device";
            }
        }
       #unset beresp.http.expires; # for cloudfront since it prefers cache-control
                                  # header over expires

       if (req.url ~ "(?i)\.(pdf|asc|dat|txt|doc|xls|ppt|tgz|csv|png|gif|jpeg|jpg|ico|swf|css|js)(\?.*)?$") {
        unset beresp.http.set-cookie;
       }
       if (req.http.Content-Type ~ "(image|audio|video|pdf|flash)") {
            set beresp.do_gzip = false;
       }
       if (req.http.Content-Type ~ "text") {
            set beresp.do_gzip = true;
       }
      # Varnish determined the object was not cacheable
        if (beresp.ttl  0) {
        set resp.http.X-Varnish-Cache = "HIT";
      }
      else {
        set resp.http.X-Varnish-Cache = "MISS";
      }

      return (deliver);
    }

1 个答案:

答案 0 :(得分:0)

您可以尝试使用

启动清漆
varnishd -F -f /etc/varnish/default.vcl

这样您就可以看到所有消息/错误。顺便说一下:你加了devicedetect.vcl。没有这个文件,很难说出现了什么问题;)