Nginx – Varnish + Nginx + WordPress: using Varnish get’s redirect loop

nginxvarnishWordpress

I have two websites using WordPress and Nginx and was trying to put Varnish at front, but when changing the listen port of Nginx to 8080 all the websites enter in an redirect loop.

In the sites enabled I have all the different hosts separated by file, as such:

server {
    server_name xpto.xyz.com;
    root /usr/share/nginx/www/xpto.xyz.com;
    include global/common.conf;
    include global/wordpress.conf;
}

In the common.conf I have configured the port wich to listen to.

In terms of Varnish I have the following in varnish file:

DAEMON_OPTS="-a :80 \
             -T localhost:6082 \
             -f /etc/varnish/default.vcl \
             -S /etc/varnish/secret \
             -s malloc,256m"

And in the default.vcl have:

backend default {
    .host = "127.0.0.1";
    .port = "8080";
}
# Drop any cookies sent to WordPress.
sub vcl_recv {
        if (!(req.url ~ "wp-(login|admin)")) {
                unset req.http.cookie;
        }
}
# Drop any cookies WordPress tries to send back to the client.
sub vcl_fetch {
        if (!(req.url ~ "wp-(login|admin)")) {
                unset beresp.http.set-cookie;
        }
}

Can anyone help me please? Thanks in advance.

Best Answer

I think your wordpress is probably doing a redirect because it's seeing traffic come in for 127.0.0.1:8080 and wants to redirect to the canonical site name. Try using curl to retrieve the site from different layers of the stack and you should be able to verify that yourself.