toby1kenobi
toby1kenobi

Reputation: 1701

Varnish redirect loop (trying to send non-www to www)

I'm trying to redirect requests with no 'www' to www.mydomain.com (a WordPress site) in Varnish, but for some reason can only create an infinite loop of redirects. In my Varnish vcl I have:

sub vcl_recv {
    if (req.http.host == "example.com") {
        set req.http.host = "www.example.com";
        error 750 "http://" + req.http.host + req.url;
    }

    ...

and:

sub vcl_error {
    if (obj.status == 750) {
        set obj.http.Location = obj.response;
        set obj.status = 301;
        return(deliver);
    }
}

(this is taken from examples I've seen online). In Fiddler the responses seem to show Varnish doing the right thing:

HTTP/1.1 301 http://www.example.com/
Server: Varnish
Location: http://example.com/
Accept-Ranges: bytes
Date: Wed, 17 Dec 2014 12:42:56 GMT
X-Varnish: 1488505994
Age: 0
Via: 1.1 varnish
Connection: close

but nonetheless, it loops until the browser gives up. What am I overlooking? Any tips much appreciated.

Toby

Upvotes: 0

Views: 697

Answers (1)

Jorge Nerín
Jorge Nerín

Reputation: 766

It seems that you're using Varnish <4, as in 4 the syntax is sightly different.

But the problem is that even though your vcl seems to be correct, the log seems to show the opposite thing, a request for http://www.example.com/ being redirected to http://example.com/, and later you say that your primary domain is www. The vcl and wordpress disagree about the main domain.

Make sure that you are reloading and using the correct vcl, and also make sure that you have written the correct hosts in the vcl and that wordpress agrees.

Show a varnishlog trace if you aren't able to find the problem.

Upvotes: 1

Related Questions