Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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!

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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!

share|improve this answer

Don't need to install any extra module in Apache. Just follow the instructions I have written in one of my article http://www.techinfobest.com/getting-real-client-ip-through-varnish/

share|improve this answer
    
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. –  dawud Jun 28 at 18:19
    
ok. The upper post already mention what to do in varnish but don't mention how to get real ip in your web application like PHP. in PHP normally you get client iP from $_SERVER["REMOTE_ADDR"] But after installation varnish you need to have it from $_SERVER["HTTP_X_FORWARDED_FOR"] . But you need to change your code. To get rid of from this my article explains more techinfobest.com/getting-real-client-ip-through-varnish whcih –  user224279 Jul 6 at 16:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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