Issues with Varnish 3.0 + Multiple Wordpress sites

Fabio Fraga [DS] fabio at dataspace.com.br
Mon Apr 3 03:51:06 CEST 2017


Hey, folks.

I have a setup on CentOS 6.8 server with 1 single ip address and including
Varnish + Nginx + php-fpm. Php works on 7.0 version.

My customer had a single website and the setup works fine so far. But he
ask to include a two new websites. My headache starts here.

When i set the backends pointing to hostname and port (in nginx), varnish
redirects to the first site. But, when i set the sub vcl_recv correctly
(using regexp), i get the correct websites.
My issue is on wp-admin. I can post text content, but i cant post images
(got http error on wordpress).
But if i remove the configuration of new backends, all things works fine.

Where am i going wrong?

Below my default.vcl.

===============================

backend default {

        .host = “w.x.y.z”;

        .port = "8081";

        .connect_timeout = 60s;

        .first_byte_timeout = 60s;

        .between_bytes_timeout = 60s;

     }

backend bk1 {

        .host = “xyz.com.br";

        .port = "8081";

        .connect_timeout = 60s;

        .first_byte_timeout = 60s;

        .between_bytes_timeout = 60s;

     }

backend bk2 {

        .host = “abc.com.br";

        .port = "8084";

        .connect_timeout = 60s;

        .first_byte_timeout = 60s;

        .between_bytes_timeout = 60s;

     }

backend bk3 {

        .host = “def.com.br";

        .port = "8083";

        .connect_timeout = 60s;

        .first_byte_timeout = 60s;

        .between_bytes_timeout = 60s;

     }

acl purge {

  "localhost";

  "127.0.0.1";

  "w.x.y.z";

}


sub vcl_recv {


  if (req.http.host ~ "^(www\.)?xyz\.com\.br$") {

        set req.backend = bk1;

        return (lookup);

   }

  if (req.http.host ~ "^(www\.)?abc\.com\.br$") {

        set req.backend = bk2;

        return (lookup);

   }

  if (req.http.host ~ "^(www\.)?def\.com\.br$") {

        set req.backend = bk3;

        return (lookup);

   }


  if (req.restarts == 0) {

    if (req.http.x-forwarded-for) {

      set req.http.X-Forwarded-For =

      req.http.X-Forwarded-For + ", " + client.ip;

    } else {

      set req.http.X-Forwarded-For = client.ip;

    }

  }


  if (req.http.Accept-Encoding) {

    if (req.url ~ "\.(jpg|jpeg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") {

      # No point in compressing these

      remove req.http.Accept-Encoding;

    } elsif (req.http.Accept-Encoding ~ "gzip") {

      set req.http.Accept-Encoding = "gzip";

    } elsif (req.http.Accept-Encoding ~ "deflate") {

      set req.http.Accept-Encoding = "deflate";

    } else {

      # unknown algorithm

      remove req.http.Accept-Encoding;

    }

  }


  if (req.request == "PURGE") {

    if ( !client.ip ~ purge ) {

      error 405 "Not allowed.";

    }

    return (lookup);

  }


  if (req.request != "GET" &&

    req.request != "HEAD" &&

    req.request != "PUT" &&

    req.request != "POST" &&

    req.request != "TRACE" &&

    req.request != "OPTIONS" &&

    req.request != "DELETE") {

      return (pipe);

  }


  if (req.request != "GET" && req.request != "HEAD") {

    return (pass);

  }


  if ( req.http.cookie ~ "wordpress_logged_in" ) {

    return(pass);

  }


  if (

    !(req.url ~ "wp-(login|admin)")

    && !(req.url ~ "&preview=true" )

  ){

    unset req.http.cookie;

  }


  if (req.http.Authorization || req.http.Cookie) {

    return (pass);

  }


  if (

    req.url ~ "preview"

    || req.url ~ "nocache"

    || req.url ~ "\.css$"

    || req.url ~ "\.js$"

    || req.url ~ "\.jpg$"

    || req.url ~ "\.jpeg$"

    || req.url ~ "\.gif$"

    || req.url ~ "\.png$"

  ) {

    return (pass);

  }


  return (lookup);

}


sub vcl_hit {


  if (req.request == "PURGE") {

    purge;

    error 200 "Purged.";

  }

  return (deliver);

}


sub vcl_miss {

  if (req.request == "PURGE") {

    purge;

    error 200 "Purged.";

  }

  return (fetch);

}


sub vcl_fetch {

  set beresp.http.Vary = "Accept-Encoding";


  if (!(req.url ~ "wp-(login|admin)") && !req.http.cookie ~
"wordpress_logged_in" ) {

    unset beresp.http.set-cookie;

    set beresp.ttl = 5m;

  }


  if (beresp.ttl <= 0s ||

    beresp.http.Set-Cookie ||

    beresp.http.Vary == "*") {

      set beresp.ttl = 120 s;

      return (hit_for_pass);

  }


  return (deliver);


}


sub vcl_hash {


if (req.http.host) {

 hash_data(req.http.host);

} else {

 hash_data(server.ip);

}

}


sub vcl_deliver {

  if (obj.hits > 0) {

    set resp.http.X-Cache = "HIT";

  } else {

    set resp.http.X-Cache = "MISS";

  }

}

=======================
Thanks for help,


Fabio Fraga Machado
phone: (48) 4052-8300 <http://up2it.com.br/assinatura/tel//4840528300>
web: www.dataspace.com.br
email: fabio at dataspace.com.br
skype: boinkbr
  <https://facebook.com/dataspace.brasil> <https://twitter.com/data_space>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.varnish-cache.org/lists/pipermail/varnish-misc/attachments/20170402/65207e77/attachment-0001.html>


More information about the varnish-misc mailing list