# GeoIP data in access\_log Nginx

**URL:** <https://www.ruby-forum.com/t/geoip-data-in-access-log-nginx/242792>\
**Category:** NGINX\
**Created:** [July 27, 2015, 11:10am UTC](https://www.ruby-forum.com/t/geoip-data-in-access-log-nginx/242792 "2015-07-27T11:10:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rafa\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/958977/32.png) [@Rafa\_F](https://www.ruby-forum.com/u/Rafa_F)\
**Post date:** [July 27, 2015, 11:10am UTC](https://www.ruby-forum.com/t/geoip-data-in-access-log-nginx/242792/1 "2015-07-27T11:10:35Z")

</div>

Hello World,

I would like to know if is possible to put GeoIP data (country for  
exemple)  
in my log access (nginx)  
I enabled the GeoIP module in my nginx (configure) and i would like to  
use  
the “$geoip\_country\_name” and “$geoip\_city” in my accesslog  
I tried to add the two variables in my log format (main) but without  
success

log\_format main  
'$host $remote\_addr - $remote\_user [$time\_local] “$request” ’  
'$status $body\_bytes\_sent “$http\_referer” ’  
‘“$http\_user\_agent” “$http\_x\_forwarded\_for” “$request\_time”  
“$upstream\_cache\_status” “$geoip\_country\_name” “$geoip\_city”’;

Result :

[my.domain.fr](http://my.domain.fr) xxx.xxx.xxx.xxx - - [22/Jul/2015:17:14:21 +0200] “GET  
/test.html HTTP/1.0” 404 564 “-” “Mozilla/5.0 (Windows NT 6.3; WOW64)  
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134  
Safari/537.36”  
“0.001” “HIT” “-” “-”

But i would like this one :

[my.domain.fr](http://my.domain.fr) xxx.xxx.xxx.xxx - - [22/Jul/2015:17:14:21 +0200] “GET  
/test.html HTTP/1.0” 404 564 “-” “Mozilla/5.0 (Windows NT 6.3; WOW64)  
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134  
Safari/537.36”  
“0.001” “HIT” “FR” “Paris”

Thanks for your help,

Best regards,  
Jugurtha

Posted at Nginx Forum:

> **[GeoIP data in access\_log Nginx](https://forum.nginx.org/read.php?2,260566,260566#msg-260566)**
>
> Hello World, I would like to know if is possible to put GeoIP data (country for exemple) in my log access (nginx) I enabled the GeoIP module in my nginx (configure) and i would like to use the "$geoip\_country\_name" and...

---

<div class="post-metadata">

**Author:** ![Jugurtha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a3d4f5/32.png) [@Jugurtha](https://www.ruby-forum.com/u/Jugurtha)\
**Post date:** [July 27, 2015, 7:02pm UTC](https://www.ruby-forum.com/t/geoip-data-in-access-log-nginx/242792/2 "2015-07-27T19:02:00Z")

</div>

Hello!

On Mon, Jul 27, 2015 at 05:10:22AM -0400, Jugurtha wrote:

> '$host $remote\_addr - $remote\_user [$time\_local] “$request” ’  
> '$status $body\_bytes\_sent “$http\_referer” ’  
> ‘“$http\_user\_agent” “$http\_x\_forwarded\_for” “$request\_time”  
> “$upstream\_cache\_status” “$geoip\_country\_name” “$geoip\_city”’;

This is expected to work fine.

> Result :
> 
> [my.domain.fr](http://my.domain.fr) xxx.xxx.xxx.xxx - - [22/Jul/2015:17:14:21 +0200] “GET  
> /test.html HTTP/1.0” 404 564 “-” “Mozilla/5.0 (Windows NT 6.3; WOW64)  
> AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36”  
> “0.001” “HIT” “-” “-”

Most likely geo information is not available for some reason.  
First of all I would recommend to check if appropriate GeoIP  
databases are present on your system and loaded into nginx using the  
geoip\_country (for $geoip\_country\_name) and geoip\_city (for  
$geoip\_city) configuration directives.

–  
Maxim D.

> **[nginx](http://nginx.org/)**

---

<div class="post-metadata">

**Author:** ![Jugurtha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a3d4f5/32.png) [@Jugurtha](https://www.ruby-forum.com/u/Jugurtha)\
**Post date:** [July 28, 2015, 2:43pm UTC](https://www.ruby-forum.com/t/geoip-data-in-access-log-nginx/242792/3 "2015-07-28T14:43:10Z")

</div>

Hello Maxim,

Thanks for the reply, you are the best 😉

I would like to add the GeoIP data into my log access to facilitate  
processing in Kibana (ELK stack)  
Maybe is not the good way because i know it possible to do this with  
LogStash (GeoIP Filter), and this would reduce pressure Nginx (what do  
you  
think ?)

#Conf GEOIP (Load GeoIP Lib) =\> Downloaded on maxmind website  
geoip\_country /var/lib/GeoIP/GeoIP.dat;  
geoip\_city /var/lib/GeoIP/GeoLiteCity.dat;

#My log format  
log\_format main  
'$host $remote\_addr - $remote\_user [$time\_local] “$request” ’  
'$status $body\_bytes\_sent “$http\_referer” ’  
‘“$http\_user\_agent” “$http\_x\_forwarded\_for” “$request\_time”  
“$upstream\_cache\_status” “$gzip\_ratio” “$geoip\_country\_code”’;

=\> it works well with “$geoip\_country\_code”

[my.domain.fr](http://my.domain.fr) xxx.xxx.xxx.xxx - - [28/Jul/2015:14:29:38 +0200] “GET  
/test.jpg  
HTTP/1.0” 200 270225 “-” “Wget/1.12 (linux-gnu)” “-” “0.048” “MISS” “-”  
“FR”

Merci, thanks again.  
Best regards,  
Jugurtha

Posted at Nginx Forum:

> **[Re: GeoIP data in access\_log Nginx](https://forum.nginx.org/read.php?2,260566,260586#msg-260586)**
>
> Hello World, I would like to know if is possible to put GeoIP data (country for exemple) in my log access (nginx) I enabled the GeoIP module in my nginx (configure) and i would like to use the "$geoip\_country\_name" and...
