I currently have a configuration setup of the following:

Load Balancer to Varnish Caches (x2 servers) to Web Nodes (x3 servers) (Apache, PHP)

I currently have set req.http.X-Forwarded-For = client.ip in Varnish, which should pass along the IP address to the Apache nodes. The Apache nodes also run mod_rpaf which should help the server use the X-Forward-IP instead of the Varnish cache IPs.

This part is fine for me, but I'm running into a different issue. Currently, only the load balancer is receiving the requests and asking Varnish caches for the pages. Meaning that Apache only logs the IP address of the Load Balancer. However, the load balancer also sends an X-Forwarded-For header in the request to Varnish. How can I pass this header along to the Apache servers?


If you're just interested in the "real" client IP, then take the req.http.X-Forwarded-For = client.ip setting out of Varnish completely.

Varnish will then have no interaction with the header - it should then pass the header as set by the load balancer through to Apache, allowing Apache to see and log the "real" client IP.

  • Wow, thanks, I totally should have caught onto this one. – Steven Lu Feb 26 '12 at 23:30
  • Also, if you don't know how to configure Apache to see real IP, read it here: Real IP through Varnish for Apache – user149170 Dec 11 '12 at 15:28

Run nginx -V and make sure you have --with-http_realip_module

If so add the following into your /etc/nginx/nginx.conf HTTP head section

real_ip_header X-Forwarded-For;

Once done service nginx restart (or /etc/init.d/nginx restart)

This was tested on varnish 3.0.4 and 100% working.

******:varnishd -V
varnishd (varnish-3.0.4 revision 9f83e8f)
  • Use if Varnish and NGINX run on the same machine. Otherwise ensure that the ip address you specify is the ip address of the machine that runs Varnish. – sridhar pandurangiah Apr 4 '14 at 7:10

The selected answer is not entirely correct (at least for Varnish 3, apache on AWS, you need to place varnish settings in like this:

sub vcl_recv {

  set req.backend = dsbalancer;

 if (req.restarts == 0) {
    if (req.http.x-forwarded-for) {
            set req.http.X-Forwarded-For = req.http.X-Forwarded-For;
    } else {
                set req.http.X-Forwarded-For = client.ip;


then add a directive in your httpd.conf LogFormat to log it..

  • just pointing out that x-forwarded-for is all in lowercase in the if (and has to be set in lowercase in front-end too or vanish wont see it) – Antony Gibbs Jul 23 '18 at 0:34

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.