1

This is my first shot at making Varnish work on my dedicated server which hosts 2 domains with 2 separate IP-addresses. My simplified setup is as follows:

Nginx conf

server {
    listen ip-address-1:8080;
}

server {
    listen ip-address-2:8080;
}

Varnish vcl

backend default {
    .host = "127.0.0.1";
    .port = "80";
}

And in the varnish conf I have defined

VARNISH_LISTEN_PORT=80

Varnish and Nginx (and php-fpm) are running properly but when I try to go to my website it shows the welcome to nginx page. The headers don't have the x-varnish in it. It seems that for some reason varnish is not listening to port 80. I'm suspecting this has to do with the vcl file where it is listening to the 127.0.0.1 host. I'm running two wordpress sites. Where should I look for to get Varnish working properly?

Cheers, Adnan

EDIT:

Nginx seems to be in 8080 correctly but Varnish is not listening to the right ip address. Using Jens multiple varnish ip addresses netstat -lnp yields:

Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 46.105.40.241:8080          0.0.0.0:*                   LISTEN      21610/nginx         
tcp        0      0 5.135.166.39:8080           0.0.0.0:*                   LISTEN      21610/nginx         
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      21610/nginx         
tcp        0      0 127.0.0.1:53                0.0.0.0:*                   LISTEN      2544/named          
tcp        0      0 0.0.0.0:21                  0.0.0.0:*                   LISTEN      1195/vsftpd         
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1184/sshd           
tcp        0      0 127.0.0.1:953               0.0.0.0:*                   LISTEN      2544/named          
tcp        0      0 46.105.40.241:443           0.0.0.0:*                   LISTEN      21610/nginx         
tcp        0      0 5.135.166.39:443            0.0.0.0:*                   LISTEN      21610/nginx         
tcp        0      0 127.0.0.1:6082              0.0.0.0:*                   LISTEN      21350/varnishd      
tcp        0      0 :::80                       :::*                        LISTEN      21351/varnishd      
tcp        0      0 ::1:53                      :::*                        LISTEN      2544/named          
tcp        0      0 :::22                       :::*                        LISTEN      1184/sshd           
tcp        0      0 ::1:953                     :::*                        LISTEN      2544/named          
udp        0      0 127.0.0.1:53                0.0.0.0:*                               2544/named          
udp        0      0 ::1:53                      :::*                                    2544/named          

default.vcl

backend ikhebeenbril {
    .host = "5.135.166.39";
    .port = "8080";
}

backend sunculture {
    .host = "46.105.40.241";
    .port = "8080";
}
sub vcl_recv {

    if (server.ip == "5.135.166.39") {
        set req.backend = ikhebeenbril; 
    } else {
        set req.backend = sunculture;
    }
...
}

sub vcl_hash {
    hash_data(server.ip);
    if (req.http.host) {
        hash_data(req.http.host);
    }
    hash_data(req.url);
    if (req.http.Accept-Encoding) {
        hash_data(req.http.Accept-Encoding);
    }
    return (hash);
}

nginx server blocks

server {
        listen 5.135.166.39:80;
        listen 5.135.166.39:443 default ssl spdy;
        server_name www.ikhebeenbril.nl;
}

server {
        listen 46.105.40.241:80;
        listen 46.105.40.241:443 default ssl spdy;
        server_name www.thesunculture.com;
}
1

If you configure your Nginx to listen on two IP addresses you should use these IP addresses in Varnish as well. The best way to do that is to use two different backend records for Varnish.

backend server1 {
    .host = "IP-ADDRESS-1";
    .port = "8080";
}
backend server2 {
    .host = "IP-ADDRESS-2";
    .port = "8080";
}

Please replace IP-ADDRESS-1 and IP-ADDRESS-2 with the correct IP addresses.

To assign the backend to incoming requests you can use following example or modify it according to your requirements:

sub vcl_recv {
    if (server.ip == "IP-ADDRESS-1") {
        set req.backend = server1; 
    } else {
        set req.backend = server2;
    }
    ...
}

It could be important to use the IP addresses for generating the cache object's hash ID. Add a specific vcl_hash method to your Varnish configuration:

sub vcl_hash {
    hash_data(server.ip);
    if (req.http.host) {
        hash_data(req.http.host);
    }
    hash_data(req.url);
    if (req.http.Accept-Encoding) {
        hash_data(req.http.Accept-Encoding);
    }
    return (hash);
}

Please take care to point Varnish's backends to your configured web sites. In the given example you have configured Nginx to listen on port 8080 but you have addressed Varnish to Port 80. If you haven't removed Nginx's default web site configuration, Nginx will respond with this site.

0

This is a bit of guesswork, but I think you have some default virtual host still defined in nginx, and that is defined to listen on port 80, and therefore Varnish is unable to bind to that port.

So, check your nginx configuration, and remove the default virtual host.

If this does not solve the issue, put the full nginx and Varnish configurations in the question, and the output of netstat -lnp.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.