Stephan
Stephan

Reputation: 1565

Varnish 301 redirect displays a white page

We use varnish as our load balancer (among other things) but we get some strange behavior at the moment.

We have a script that gets called with some parameters, and depending on what parmas you pass, you get redirected to a different location using a 301 redirect (this it done with a php script and the header() function)

The problem is that the first time a URL is begin called the 301 redirect happens, but then the next time that same URL is called, you get a status of 200 OK, no redirect happens and just a white page is displayed.

I've added a session_start() to the top of the php script to try and stop varnish from caching the page, but nothing helped so far.

I've done some research regarding this issue, and saw that several people experience the same problem, but I wasn't able to find a solution yet.

How would I get varnish the stop caching the page?
Any help in the right direction will be appreciated.

Upvotes: 2

Views: 3021

Answers (3)

ivy
ivy

Reputation: 5559

This is rather awkward and is supposed to work correctly by default. Can you tell us what version of Varnish you are using and if you created a custom vcl file?

The bug was probably introduced in vcl_fetch. This should check for cacheability with checks like:

sub vcl_fetch {
  ...
  if (req.status >= 300 ) {
    return pass;
  }

  if ( ! obj.cacheable ) {
    return pass;
  }
  ..
}

Upvotes: 0

Chris Whittington
Chris Whittington

Reputation: 550

Could you not exclude that url from the varnish cache?

Add something like the following to your default.vcl (or whatever your varnish config file is called).

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

This should stop varnish caching that url.

Upvotes: 1

Scilence
Scilence

Reputation: 21

You could try finding the url that varnish is redirecting to and adding a query string with a randomly generated number to it.

Example:

<?php
$random_number = rand(10000, 99999999);
// This is what the redirect code MIGHT look like, but I doubt it.
header("Location: http://www.example.com/index.php?cache=$random_number");
?>

If you can find where the page is actually doing the redirecting and you add a random number query string, it should fix things. I have used this method of making sure images are not cached in the past and it always worked for me perfectly.

Oh, and if you can't find the redirect code that varnish is using itself. You could try adding this to the page that varnish loads after the 301 redirect:

<?php
$random_number = rand(10000, 99999999);
header("Location: NAME_OF_THIS_SCRIPT.php?cache=$random_number");
?>

Pretty much the same idea, just involves less hunting around. I'm not sure if this will break the load balancing function of varnish though.

Upvotes: 0

Related Questions