# Rancher: Load Balancer X-Forwarded

**URL:** <https://forums.suse.com/t/rancher-load-balancer-x-forwarded/7138>\
**Category:** Rancher 1.x\
**Created:** [August 18, 2017, 8:44am UTC](https://forums.suse.com/t/rancher-load-balancer-x-forwarded/7138 "2017-08-18T08:44:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sjkeerthi](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/sjkeerthi/32/2878_2.png) [@sjkeerthi](https://forums.suse.com/u/sjkeerthi)\
**Post date:** [August 18, 2017, 8:44am UTC](https://forums.suse.com/t/rancher-load-balancer-x-forwarded/7138/1 "2017-08-18T08:44:29Z")

</div>

Hi

I have an apache container which is behind the Rancher LB when I access the apache log I could see the source IP address as LB’s internal IP.

How could I get the real source client IP address on apache log.

I tried various methods like adding  
http-request add-header X-Forwarded-For %[src] on Custom haproxy.cfg

I tried by adding on my apache container with  
LogFormat “%{X-Forwarded-For}i %l %u %t “%r” %\>s %b “%{Referer}i” “%{User-Agent}i”” combined

Still, I could see the source IP address as my LB

localhost:80 10.42.247.56 - - [18/Aug/2017:08:23:34 +0000] “GET /backend/api-auth/login/?next=/backend/ HTTP/1.1” 200 1436 “[http://xxxx.com/backend/](http://xxxx.com/backend/)” “Mozilla/5.0 (X11; Fedora; Linux x86\_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36”

---

<div class="post-metadata">

**Author:** ![sjkeerthi](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/sjkeerthi/32/2878_2.png) [@sjkeerthi](https://forums.suse.com/u/sjkeerthi)\
**Post date:** [August 22, 2017, 6:00am UTC](https://forums.suse.com/t/rancher-load-balancer-x-forwarded/7138/2 "2017-08-22T06:00:45Z")

</div>

After I add my URL on the Rancher LB I could see on /etc/haproxy/haproxy.cfg

```
   backend 80_stage_test_com_backend
   acl forwarded_proto hdr_cnt(X-Forwarded-Proto) eq 0
   acl forwarded_port hdr_cnt(X-Forwarded-Port) eq 0
   http-request add-header X-Forwarded-Port %[dst_port] if forwarded_port
   http-request add-header X-Forwarded-Proto https if { ssl_fc } forwarded_proto
   mode http
   server 1d3c763bac284447354f6488cfdc777763382c97 10.42.125.61:80 

```

**On my Apache Container I added**

```
RemoteIPHeader X-Forwarded-For
LogFormat "%{X-Forwarded-For}i %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\ " combine
LogFormat "%{X-Forwarded-For}i %h %l %u %t \"%r\" %s %b \"%{Referer}i\" \"%{User-agent}i\"" combined-forwarded

```

**Apache Log**

```
localhost:80 10.42.247.56 - - [18/Aug/2017:08:17:50 +0000] "GET /backend HTTP/1.1" 200 2533 "-" "Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36"
localhost:80 10.42.247.56 - - [18/Aug/2017:08:18:09 +0000] "GET /backend HTTP/1.1" 200 2533 "-" "Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36"
localhost:80 10.42.247.56 - - [18/Aug/2017:08:19:16 +0000] "GET /backend/api-auth/logout/?next=/backend/ HTTP/1.1" 302 452 "http://stage.test.com/backend" "Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36"
localhost:80 10.42.247.56 - - [18/Aug/2017:08:19:16 +0000] "GET /backend/ HTTP/1.1" 403 4991 "http://stage.test.com/backend" "Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36"
localhost:80 10.42.247.56 - - [18/Aug/2017:08:23:34 +0000] "GET /backend/api-auth/login/?next=/backend/ HTTP/1.1" 200 1436 "http://stage.test.com/backend/" "Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36"

```

Still I could not able to get the source IP address on my access.log have no clue really what else I need to do get the client source IP address

---

<div class="post-metadata">

**Author:** ![davalex](https://avatars.discourse-cdn.com/v4/letter/d/54ee81/32.png) [@davalex](https://forums.suse.com/u/davalex)\
**Post date:** [August 22, 2017, 8:21pm UTC](https://forums.suse.com/t/rancher-load-balancer-x-forwarded/7138/3 "2017-08-22T20:21:29Z")

</div>

Hi,

Try adding

> defaults  
> option forwardfor if-none

to the “Custom haproxy config” on the load balancer.

I had to use this config when running a ELB infront of Rancher’s load balances.

---

<div class="post-metadata">

**Author:** ![sjkeerthi](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/sjkeerthi/32/2878_2.png) [@sjkeerthi](https://forums.suse.com/u/sjkeerthi)\
**Post date:** [August 23, 2017, 1:17am UTC](https://forums.suse.com/t/rancher-load-balancer-x-forwarded/7138/4 "2017-08-23T01:17:16Z")

</div>

Hi

I tried with above mentioned option on Rancher LB, Still I get the same source IP Address as my LB’s IP Address.

 ![Screenshot from 2017-08-23 06-44-12](https://us1.discourse-cdn.com/flex022/uploads/suse/original/2X/e/e6708e461a0256c0b7be60c8df8bb7c4907f20db.png)

**My Container Apache Conf File**

\<VirtualHost \*:80\>

```
ServerName localhost
WSGIProcessGroup localhost
WSGIPassAuthorization On
WSGIDaemonProcess localhost python-path=/trunk/django/
WSGIScriptAlias /backend /trunk/django/wsgi.py process-group=localhost
WSGIApplicationGroup %{GLOBAL}

Alias "/uploads" "/trunk/django/uploads"
Alias "/backend/uploads" "/trunk/django/uploads"
	
RemoteIPHeader X-Forwarded-For
LogFormat "%{X-Forwarded-For}i %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" combine
LogFormat "%{X-Forwarded-For}i %h %l %u %t \"%r\" %s %b \"%{Referer}i\" \"%{User-agent}i\"" combined-forwarded

<Directory /trunk/django/uploads>
       Require all granted
</Directory>

<Directory /trunk/django>
     <Files wsgi.py>
      Require all granted
     </Files>
</Directory>
</VirtualHost>
```

---

<div class="post-metadata">

**Author:** ![etlweather](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/etlweather/32/419_2.png) [@etlweather](https://forums.suse.com/u/etlweather)\
**Post date:** [August 24, 2017, 12:02am UTC](https://forums.suse.com/t/rancher-load-balancer-x-forwarded/7138/5 "2017-08-24T00:02:41Z")

</div>

You need to tell Apache that you trust the proxy to tell you the X-Forwarded-For header…

For Apache HTTPd, in your `VirtualHost` configuration:

```auto
RemoteIPHeader X-Forwarded-For
RemoteIPInternalProxy 10.0.0.0/8

```

You don’t need to modify the log format, it will log the Remote IP automatically.

For Nginx:

```auto
server {
 ...
 set_real_ip_from 10.0.0.0/8;
 real_ip_header X-Forwarded-For;
...
}

```

There aren’t any configuration needed for HAProxy, by default, the Rancher LB HAProxy has the `X-Forwarded-For` header set.

---

<div class="post-metadata">

**Author:** ![sjkeerthi](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/sjkeerthi/32/2878_2.png) [@sjkeerthi](https://forums.suse.com/u/sjkeerthi)\
**Post date:** [August 24, 2017, 1:48pm UTC](https://forums.suse.com/t/rancher-load-balancer-x-forwarded/7138/6 "2017-08-24T13:48:30Z")

</div>

As you said I tried with below configuration still I get the LB IP Address

```
<VirtualHost *:80>

ServerName localhost
WSGIProcessGroup localhost
WSGIPassAuthorization On
WSGIDaemonProcess localhost python-path=/trunk/django/
WSGIScriptAlias /backend /trunk/django/wsgi.py process-group=localhost
WSGIApplicationGroup %{GLOBAL}

Alias "/uploads" "/trunk/django/uploads"
Alias "/backend/uploads" "/trunk/django/uploads"

RemoteIPHeader X-Forwarded-For
RemoteIPInternalProxy 10.0.0.0/8
	
<Directory /trunk/django/uploads>
       Require all granted
</Directory>

<Directory /trunk/django>
     <Files wsgi.py>
      Require all granted
     </Files>
</Directory>
</VirtualHost>

```

# Still the Apache Log

localhost:80 10.42.3.76 - - [24/Aug/2017:13:42:59 +0000] “GET /backend HTTP/1.1” 403 4972 “-” "Mozilla/5.0 (X11; Linux x86\_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36"  
localhost:80 10.42.3.76 - - [24/Aug/2017:13:44:16 +0000] “GET /backend/api-auth/login/?next=/backend/ HTTP/1.1” 200 1436 “[http://stage.xxxxx.com/backend](http://stage.xxxxx.com/backend)” “Mozilla/5.0 (X11; Linux x86\_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36”

---

<div class="post-metadata">

**Author:** ![etlweather](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/etlweather/32/419_2.png) [@etlweather](https://forums.suse.com/u/etlweather)\
**Post date:** [August 24, 2017, 1:58pm UTC](https://forums.suse.com/t/rancher-load-balancer-x-forwarded/7138/7 "2017-08-24T13:58:16Z")

</div>

My container is FROM `httpd:2.4-alpine`.

I realized I also load the module `remoteip`

`LoadModule remoteip_module modules/mod_remoteip.so`

My `common` log format is the following (but I don’t think I changed that from the default):

```auto
<IfModule log_config_module>
    LogFormat "%v:%p %a %l %u %t \"%r\" %>s %b" common
    CustomLog /proc/self/fd/1 common
</IfModule>

```

---

<div class="post-metadata">

**Author:** ![sjkeerthi](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/sjkeerthi/32/2878_2.png) [@sjkeerthi](https://forums.suse.com/u/sjkeerthi)\
**Post date:** [August 24, 2017, 2:06pm UTC](https://forums.suse.com/t/rancher-load-balancer-x-forwarded/7138/8 "2017-08-24T14:06:50Z")

</div>

My container is ubuntu 16.04 with Apache 2.4 the default apache.conf I cloud see the below conf and I have enabled the remoteip module using on my DockerFile RUN a2enmod remoteip

```
LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
```
