Varnish mobile redirect

Kevin Pettijohn kpettijohn at tarot.com
Tue May 3 21:49:31 CEST 2011


Hello Everyone,

With some help from Tom at mobiledrupal.com I am trying to get a mobile device redirect setup. The redirects are working in varnish but I am also trying to make it so if a mobile user clicks a link on our mobile site with /?nomobi=true appended it will then not  be redirected and will be passed through to our main site where a cookie will be set to keep them on our main site. Currently my problem is that I can seem to get around the redirects that I have in place with req.url /?nomobi=true and a pass call. 

Here is my VCL for the mobile setup:


sub device_detection {
        # Default to thinking it's a PC
        set req.http.X-Device = "pc";

        # Add all possible agent strings

        # These are the most popular agent strings
        if (req.http.User-Agent ~ "iP(hone|od)" || req.http.User-Agent ~ "Android" || req.http.User-Agent ~ "Symbian" || req.http.User-Agent ~ "^BlackBerry" || req.http.User-Agent ~ "^SonyEricsson" || req.http.User-Agent ~ "^Nokia" || req.http.User-Agent ~ "^SAMSUNG" || req.http.User-Agent ~ "^LG" || req.http.User-Agent ~ "   webOS") {
            set req.http.X-Device = "mobile";
          }

        # These are some more obscure agent strings
        if (req.http.User-Agent ~ "^PalmSource"){
          set req.http.X-Device = "mobile";
        }

          if (req.http.X-Device == "mobile" && req.url ~ "^/?nomobi=true$") {
            return (pass);
          }

        # Decide if we need redirection
        if (req.http.X-Device == "mobile" && req.url !~ "^/?nomobi=true$") {
          if (req.http.host !~ "mv2.example.com" && req.url !~ "^/?nomobi=true$") {
                error 750 "mv2.example.com";
          }
          elseif (req.http.host ~ "mv2.example.com" && req.url !~ "^/?nomobi=true$"){
                error 750 "v2.example.com";
          }
      }
 }

sub vcl_recv {
 call device_detection;
}

sub vcl_error {

  if (obj.status == 750) {
    if (obj.response ~ "mv2.example.com") {
     set obj.http.Location = "http://mv2.example.com" req.url;
   }
    elsif (obj.response ~ "v2.example.com") {
     set obj.http.Location = "http://v2.example.com" req.url;
   }
    set obj.status = 302;
  }
}

I am fairly new to varnish so forgive me if I am going about this all wrong. Any insight would be much appreciated! 

-kp
__________________
Kevin Pettijohn
Operations & IT
The Daily Insight Group 
kpettijohn at tarot.com









More information about the varnish-misc mailing list