# Passing $geoip variables ( $geoip\_city\_country\_code ) via proxy\_set\_header not working

**URL:** <https://www.ruby-forum.com/t/passing-geoip-variables-geoip-city-country-code-via-proxy-set-header-not-working/212924>\
**Category:** NGINX\
**Created:** [October 18, 2011, 3:41am UTC](https://www.ruby-forum.com/t/passing-geoip-variables-geoip-city-country-code-via-proxy-set-header-not-working/212924 "2011-10-18T03:41:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [October 18, 2011, 3:41am UTC](https://www.ruby-forum.com/t/passing-geoip-variables-geoip-city-country-code-via-proxy-set-header-not-working/212924/1 "2011-10-18T03:41:35Z")

</div>

Hello,

We’ve come across a problem with trying to pass $geoip\_city\_country\_code  
(and all of the other $geoip\_ variables) to an upstream via  
proxy\_set\_header.

The geoip module is configured correctly and working. We use the exact  
same variable name for logging.

The applicable configuration is:

http {  
geoip\_city /usr/local/share/GeoIP/GeoLiteCity.dat;

```
proxy_set_header X-Forwarded-For 

```

$proxy\_add\_x\_forwarded\_for;  
proxy\_set\_header X-GeoIP-Country-Code  
$geoip\_city\_country\_code;

```
include /etc/nginx/vhosts/*.vhost;

```

}

/etc/nginx/vhosts/test.vhost:

server {  
listen 80;

location / {  
proxy\_pass [http://127.0.0.1:9232](http://127.0.0.1:9232);  
}  
}

From the localhost (127.0.0.1), I use ‘nc’ to listen to 9232 and from  
there I connect via a browser to nginx.

Here’s the output after I connect to nginx via a browser.

$ nc -l 9232  
GET / HTTP/1.0  
Host: test  
X-Forwarded-For: 172.16.n.n  
Connection: close  
Cache-Control: max-age=0  
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1  
(KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.1  
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,_/_;q=0.8  
Accept-Encoding: gzip,deflate,sdch  
Accept-Language: en-US,en;q=0.8  
Accept-Charset: ISO-8859-1,utf-8;q=0.7,\*;q=0.3

If I set “proxy\_set\_header X-GeoIP-Country-Code True;”, then the HTTP  
header shows up as expected.

$ nc -l 9232  
GET / HTTP/1.0  
Host: test  
X-Forwarded-For: 172.16.n.n  
X-GeoIP-Country-Code: True  
Connection: close  
Cache-Control: max-age=0  
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1  
(KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.1  
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,_/_;q=0.8  
Accept-Encoding: gzip,deflate,sdch  
Accept-Language: en-US,en;q=0.8  
Accept-Charset: ISO-8859-1,utf-8;q=0.7,\*;q=0.3

This has been tested with 1.1.1 and 1.1.6 and it isn’t working on either  
version.

Here’s the output to ‘nginx -V’

nginx: nginx version: nginx/1.1.1  
nginx: built by gcc 4.1.2 20071124 (Red Hat 4.1.2-42)  
nginx: TLS SNI support disabled  
nginx: configure arguments: --prefix=/var/nginx  
–sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf  
–error-log-path=/var/log/nginx/error.log  
–http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid  
–lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-pcre  
–with-http\_ssl\_module --with-http\_stub\_status\_module  
–with-http\_geoip\_module

What is interesting is that this webpage details doing essentially the  
same thing and have it work.

> **[Using GeoIP With Nginx On Debian Squeeze/Ubuntu 11.04](https://www.howtoforge.com/using-geoip-with-nginx-on-debian-squeeze-ubuntu-11.04)**
>
> Using GeoIP With Nginx On Debian Squeeze/Ubuntu 11.04
> 
> This tutorial explains how to use the GeoIP module with nginx on 
> Debian Squeeze/Ubuntu 11.0...

For us, the upstream is another webserver.

Any ideas?

Thanks,  
Rob

---

<div class="post-metadata">

**Author:** ![Robert\_Suh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@Robert\_Suh](https://www.ruby-forum.com/u/Robert_Suh)\
**Post date:** [October 18, 2011, 7:49am UTC](https://www.ruby-forum.com/t/passing-geoip-variables-geoip-city-country-code-via-proxy-set-header-not-working/212924/2 "2011-10-18T07:49:15Z")

</div>

On Mon, Oct 17, 2011 at 06:40:35PM -0700, Robert Suh wrote:

> GET / HTTP/1.0  
> If I set “proxy\_set\_header X-GeoIP-Country-Code True;”, then the HTTP header  
> shows up as expected.  
> Accept-Encoding: gzip,deflate,sdch  
> nginx: configure arguments: --prefix=/var/nginx --sbin-path=/usr/sbin/nginx  
> –conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log  
> –http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid  
> –lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-pcre  
> –with-http\_ssl\_module --with-http\_stub\_status\_module --with-http\_geoip\_module
> 
> What is interesting is that this webpage details doing essentially the same  
> thing and have it work.
> 
> [http://www.howtoforge.com/using-geoip-with-nginx-on-debian-squeeze-ubuntu-11.04](http://www.howtoforge.com/using-geoip-with-nginx-on-debian-squeeze-ubuntu-11.04)
> 
> For us, the upstream is another webserver.
> 
> Any ideas?

I believe, GeoIP returns empty string for 172.16.x networks, and nginx  
does tno pass header field with no value.

–  
Igor S.

---

<div class="post-metadata">

**Author:** ![Robert\_Suh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@Robert\_Suh](https://www.ruby-forum.com/u/Robert_Suh)\
**Post date:** [October 18, 2011, 6:54pm UTC](https://www.ruby-forum.com/t/passing-geoip-variables-geoip-city-country-code-via-proxy-set-header-not-working/212924/3 "2011-10-18T18:54:10Z")

</div>

Thank you Igor, a colleague was able to find a forum posting about the  
realip module.

Using the realip module we were able to test the geoip variables passing  
via proxy\_set\_header.

It works as expected now.

Thanks again,  
Rob
