Varnish + Nginx + multiple IP addresses

Posted by adnan on Server Fault See other posts from Server Fault or by adnan
Published on 2014-06-09T04:05:41Z Indexed on 2014/06/10 3:28 UTC
Read the original article Hit count: 529

Filed under:
|
|
|

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;
}

© Server Fault or respective owner

Related posts about nginx

Related posts about Wordpress