skip to Main Content

I found here and on the internet several examples how to solve this, in Varnish docs also, but I didn’t sort that.
Actually, I have some search URL on wordpress which looks like:

/search/foo/?content=bar

If I run this search from above, and then if I try to search with different content like

/search/foo/?content=other_bar

I get same results for both search because is cached from Varnish. When I test this search without Varnish everything works.

I don’t know how can I exclude this URL from Varnish cache.

Varnish works on port 80. Nginx works on port 8080, SSL termination is on the Nginx on port 443 with proxy pass to Varnish http://127.0.0.1:80. All that works well.

2

Answers


  1. Sounds like you are doing TLS termination with NGINX and then forwarding the request to Varnish Cache to do caching. This is a typical setup.

    Varnish is running a VCL (Varnish Configuration Language) file, specified by the path in it’s -f parameter. The default for this is /etc/varnish/default.vcl If you share this file with us, we can give you better advice.

    Most likely, you are slicing the URL parameters off of your URL or otherwise modifying the cache key such that variations in these parameters don’t map to distinct cache entries. As a result, you are seeing a cached entry for some other search when you search something new. This is registering as a cache hit within Varnish while really the new search should cause a cache miss and a backend fetch to get the new content.

    In your VCL, you most likely have some req.url operations or some custom vcl_hash logic that is causing this unwanted behavior. Unfortunately I cannot really give you more on what to change without seeing the VCL file.

    After changing the VCL file, you will want to do a reload to cause the VCL file to recompile: sudo systemctl reload varnish. In this case, it may also make sense to clear the cache, which you can do with a restart: sudo systemctl restart varnish

    You can take a closer look at how this behavior is occurring using varnishlog: sudo varnishlog -d -g request. Do you see some ReqURL tags where the URL is being changed by VCL?

    Login or Signup to reply.
  2. Hum.. 2 solutions for me.

    • on the search app side, you may answer something like cache-control: max-age=0 that will prevent varnish from caching it

    • on the varnish vcl, you may have something like the following, that will tell varnish to always bypass the cache mechanism

      sub vcl_recv { if (req.url ~ "^/search/foo/.*") {
      return(pass); } }

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search