Varnish forward client IP-address to backend

apache-2.2debian-squeezevarnish

I have set up varnish as a proxy to redirect HTTP-request to backend-servers running apache.
I would like to have the clients IP-addresses in my apache logs instead of the varnish server IP-address. Here is my varnish configuration file:

backend $my_backend {
    .host = "192.168.0.103";
    .port = "80";
}
sub vcl_recv {
} else if (req.http.host == "$my_domain_name") {
    set req.backend = $my_backend;
    if (req.request == "POST") {
        if (req.http.X-Forwarded-For) {
          set req.http.X-Real-Forwarded-For = req.http.X-Forwarded-For ", " regsub(client.ip, ":.*", "");
          unset req.http.X-Forwarded-For;
       } else {
          # Simply use the client IP
          set req.http.X-Real-Forwarded-For = regsub(client.ip, ":.*", "");
       }
        return(pipe);
    }
    return(lookup);
}
}

On the backend apache config file I have this

RPAFenable On
RPAFsethostname On
RPAFproxy_ips $varnish_proxy_ip
RPAFheader X-Real-IP

The problem is that the instruction RPAFheader is not recognized on Debian6:

root@$hostname:~# apache2ctl configtest
Invalid command 'RPAFheader', perhaps misspelled or defined by a module not included in the server configuration
Action 'configtest' failed.
The Apache error log may have more information.
root@$hostname:~# 

Have anyone set up mod_rpaf on debian to give me a hand on this troublemod_rpaf

Thank you very much for your help!

Best Answer

Ok, I will answer my own question to help people who may have the same problem:

First add the following lines into varnish configuration file (default.vcl)

sub vcl_recv {
   if (req.http.host == "myDomain.net") {
       set req.http.host = "myDomain.net";
       set req.backend = myBackend;
       # Compatiblity with Apache log
       remove req.http.X-Forwarded-For;
       set req.http.X-Forwarded-For = client.ip;
       # No cache for POST requests
       if (req.request == "POST") {
           return(pipe);
       }   
       return(lookup);
   }
}

Then add personalized logs format for apache while configuring your vhost

LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" varnishcombined
 ...
 CustomLog      ${APACHE_LOG_DIR}/access.log varnishcombined

That's it!

Related Topic