Passing $geoip variables ( $geoip_city_country_code ) via proxy_set_header not working

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;
}
}

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.

For us, the upstream is another webserver.

Any ideas?

Thanks,
Rob

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

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.

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