• Home
  • Archive
  • Tools
  • Contact Us

The Customize Windows

Technology Journal

  • Cloud Computing
  • Computer
  • Digital Photography
  • Windows 7
  • Archive
  • Cloud Computing
  • Virtualization
  • Computer and Internet
  • Digital Photography
  • Android
  • Sysadmin
  • Electronics
  • Big Data
  • Virtualization
  • Downloads
  • Web Development
  • Apple
  • Android
Advertisement
You are here:Home » How To Configure Nginx Access Log With GeoIP (Ubuntu 16.04)

By Abhishek Ghosh October 17, 2016 1:22 pm Updated on November 17, 2016

How To Configure Nginx Access Log With GeoIP (Ubuntu 16.04)

Advertisement

We can virtually use Nginx access log like analytics software. Here is How To Configure Nginx Access Log With GeoIP on Ubuntu 16.04. Usage Commands Also Shown. Access Log is Powerful & Configurable Tool. Many new users sub-utilise this powerful tool of Nginx.

 

Requirements To Proceed To Read This How To Configure Nginx Access Log With GeoIP Guide

 

This guide is written and tested on Ubuntu 16.04 LTS with apt version of nginx-extras. You can definitely work with custom build of Nginx on Ubuntu or different server operating system like CentOS provided that your Nginx GeoIP module compiled as dynamic module (else you need to change one directive) and using PHP 7.x (else you need to change one another directive’s file name). We tested this guide on Nginx 1.11.5, HTTPS, HTTP/2 site. You must NOT have access log off under SSL directives. We will describe as Ubuntu 16.04’s default file paths.

how-to-configure-nginx-access-log-with-geoip-ubuntu-16-04

 

Requirements To Proceed To Read This How To Configure Nginx Access Log With GeoIP Guide

 

First, we need to check whether ngx_http_geoip_module is actually compiled on your nginx installation. You can run the command nginx -V (V is capital letter, small letter on command will show version only) to find from the output :

Advertisement

---

Vim
1
2
3
4
configure arguments: --with-cc-opt='-g -O2 -fstack-protector-strong -Wformat -Werror=format-security
...
--with-http_geoip_module=dynamic
...

We are talking about these official documents :

Vim
1
2
3
http://nginx.org/en/docs/http/ngx_http_log_module.html#access_log
https://www.nginx.com/resources/admin-guide/logging-and-monitoring/
http://nginx.org/en/docs/http/ngx_http_geoip_module.html

First, we need to download geoip-database for country and city database. Ubuntu has a package for but it lacks city’s data. MaxMind’s geoip-database is a non-free software. We can use some of their distributed stuffs for free of cost. Just follow these commands like a copy-pasting machine :

Vim
1
2
3
4
5
6
mkdir /etc/nginx/geoip
cd /etc/nginx/geoip
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
gunzip GeoIP.dat.gz
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
gunzip GeoLiteCity.dat.gz

Now open /etc/nginx/nginx.conf with any text editor like vi or nano:

Vim
1
nano /etc/nginx/nginx.conf

You will notice that the beginning of the file these stuffs are written on that file :

Vim
1
2
3
4
5
user www-data;
worker_processes 1;
worker_priority  15;
pid /run/nginx.pid;
worker_rlimit_nofile 8192;

Add this line there :

Vim
1
load_module "modules/ngx_http_geoip_module.so";

After that stanza, there is events and http. We have to add more two lines inside that http :

Vim
1
2
geoip_country /etc/nginx/geoip/GeoIP.dat;
geoip_city /etc/nginx/geoip/GeoLiteCity.dat;

So you have added these on /etc/nginx/nginx.conf :

Vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
...
...
load_module "modules/ngx_http_geoip_module.so";
 
events {
...
...
       }
 
http {
...
...
       geoip_country /etc/nginx/geoip/GeoIP.dat;
       geoip_city /etc/nginx/geoip/GeoLiteCity.dat;
...
      }

Save the file. Run config test with nginx -t command. Restart Nginx with service nginx restart or whatever command you use. Again open /etc/nginx/nginx.conf and go to :

Vim
1
nano /etc/nginx/nginx.conf

Within the http directive, we have to add this kind of basic directive to catch the Country name and Country code. We can catch City name, Area name, Pin code too. This is fat-free easy version :

Vim
1
2
3
4
5
6
log_format analytics '$remote_addr - $upstream_cache_status [$time_local]  '
                    '"$request" $status $body_bytes_sent $query_string'
                    '"$http_referer" "$http_user_agent"'
                    '"$geoip_country_name" "$geoip_country_code"';
access_log /var/log/nginx/access.log analytics;
error_log /var/log/nginx/error.log warn;

So our log will be written on /var/log/nginx/access.log. Run config test with nginx -t command. Restart Nginx with service nginx restart or whatever command you use.

Here is a slightly complicated but detailed logging for human readable log :

Vim
1
log_format analytics '[$time_iso8601] » [$upstream_cache_status] » [$remote_addr]' '[$request $request_time $request_length] » [$status] » [$body_bytes_sent] »' '[$http_referer] » [$http_user_agent]' '[$geoip_country_name » $geoip_country_code » $geoip_region » $geoip_city » $geoip_latitude » $geoip_longitude]' '$query_string';

But the above can make your server slightly slower. For that reason, we are suggesting to use the fat-free easy version. You can even strip off "$geoip_country_code" from it. We kept it to make it using with any other tool for log analysis.

We need too add some fastcgi directives as well. If your Nginx host file is /etc/nginx/sites-available/default, open that file :

Vim
1
nano  /etc/nginx/sites-available/default

You will notice that, either there is :

Vim
1
include fastcgi.conf;

or

Vim
1
include fastcgi_params;

is present. Add a line below it :

Vim
1
include geoip_params;

There can be multiple instances of include fastcgi.conf or include fastcgi_params. You have to add include geoip_params below each. But where is our geoip_params file? Let us create it :

Vim
1
touch /etc/nginx/geoip_params

We need t copy-paste this large stuff in that /etc/nginx/geoip_params file :

Vim
1
2
3
4
5
6
7
8
9
10
11
12
proxy_set_header GEOIP_COUNTRY_CODE $geoip_country_code;
proxy_set_header GEOIP_COUNTRY_CODE3 $geoip_country_code3;
proxy_set_header GEOIP_COUNTRY_NAME $geoip_country_name;
proxy_set_header GEOIP_CITY_COUNTRY_CODE $geoip_city_country_code;
proxy_set_header GEOIP_CITY_COUNTRY_CODE3 $geoip_city_country_code3;
proxy_set_header GEOIP_CITY_COUNTRY_NAME $geoip_city_country_name;
proxy_set_header GEOIP_REGION $geoip_region;
proxy_set_header GEOIP_CITY $geoip_city;
proxy_set_header GEOIP_POSTAL_CODE $geoip_postal_code;
proxy_set_header GEOIP_CITY_CONTINENT_CODE $geoip_city_continent_code;
proxy_set_header GEOIP_LATITUDE $geoip_latitude;
proxy_set_header GEOIP_LONGITUDE $geoip_longitude;

For that reason, open /etc/nginx/geoip_params :

Vim
1
nano /etc/nginx/geoip_params

Copy-paste it. Save it. Run config test with nginx -t command. Restart Nginx with service nginx restart or whatever command you use. We have completed all the steps. It is really big set of work than result.

We have kept all the configuration files of this guide on GitHub as project. A logrotate file is kept which is not needed for who are using Ubuntu 16.04 LTS with apt version of nginx-extras.

Now cd to /var/log/nginx/ :

Vim
1
cd /var/log/nginx/

You will get country when possible to detect, here is a copy-paste from our log :

Vim
1
54.164.253.92 - - [17/Oct/2016:18:07:12 +0530]  "GET /2015/07/multimeter-guide-for-dummies HTTP/1.1" 301 178 -"-" "Mozilla/5.0 (compatible; proximic; +http://www.proximic.com/info/spider.php)""United States"

 

Analyzing Nginx Access Log With GeoIP With Commands

 

This will give total number of access as list against HTTP response codes (200 is OK) :

Vim
1
cat /var/log/nginx/access.log | cut -d '"' -f3 | cut -d ' ' -f2 | sort | uniq -c | sort -rn

Basically guide on how to configure Nginx access log with GeoIP is not for counting country visits via complex commands and hundred of tools (the command is not local, it will take some time to get executed to give a list) :

Vim
1
cat /var/log/nginx/access.log | grep -oe '^[0-9.]\+' | perl -ne 'system("geoiplookup $_")' | grep -v found | grep -oe ', [A-Za-z ]\+$' | sort | uniq -c | sort -n

We can logically serve translated version of file according to the country or show special message with plain PHP from front end like EU cookie policy information or use tools such as goaccess to monitor and analyze Nginx logs.

Tagged With geo ip , ubuntu server 16 04 nginx with geoip module , nginx geoip to log , nginx geoip logs , nginx geoip access log , nginx geoip , https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1839 d3cdhCejngWNXEeBRjq6L_GwxxfJFB3WWj83TKjqFaDVmUEN4p97qPXx7P8Wdnk1 d31b7cbcae85671e577210f3ca9258549296e01b&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme , how to set the logging on nginx container to error log and access log? , geoip logging nginx , configure geoip module in nginx ubuntu
Facebook Twitter Pinterest

Abhishek Ghosh

About Abhishek Ghosh

Abhishek Ghosh is a Businessman, Surgeon, Author and Blogger. You can keep touch with him on Twitter - @AbhishekCTRL.

Here’s what we’ve got for you which might like :

Articles Related to How To Configure Nginx Access Log With GeoIP (Ubuntu 16.04)

  • Nginx WordPress Installation Guide (All Steps)

    This is a Full Nginx WordPress Installation Guide With All the Steps, Including Some Optimization and Setup Which is Compatible With WordPress DOT ORG Example Settings For Nginx.

  • Installing GeoIP Module On Apache2, Ubuntu 16.04

    Installing GeoIP Module On Apache2, Ubuntu 16.04 Needs Some Manual Steps After Running Installation Command. Sample PHP File Supplied To Test.

  • Join/Merge Multiple Log Files For Big Data Analysis

    Here Are The Ways To Join/Merge Multiple Log Files For Big Data Analysis, Store Them To OpenStack Based Cloud Storage And Delete Old Files.

  • WordPress Multisite on Nginx on Ubuntu 14.04 on HP Cloud

    Here is a Step by Step Guide on Setting Up WordPress Multisite on Nginx on Ubuntu 14.04 on HP Cloud with All Commands and the Configuration.

performing a search on this website can help you. Also, we have YouTube Videos.

Take The Conversation Further ...

We'd love to know your thoughts on this article.
Meet the Author over on Twitter to join the conversation right now!

If you want to Advertise on our Article or want a Sponsored Article, you are invited to Contact us.

Contact Us

Subscribe To Our Free Newsletter

Get new posts by email:

Please Confirm the Subscription When Approval Email Will Arrive in Your Email Inbox as Second Step.

Search this website…

 

Popular Articles

Our Homepage is best place to find popular articles!

Here Are Some Good to Read Articles :

  • Cloud Computing Service Models
  • What is Cloud Computing?
  • Cloud Computing and Social Networks in Mobile Space
  • ARM Processor Architecture
  • What Camera Mode to Choose
  • Indispensable MySQL queries for custom fields in WordPress
  • Windows 7 Speech Recognition Scripting Related Tutorials

Social Networks

  • Pinterest (24.3K Followers)
  • Twitter (5.8k Followers)
  • Facebook (5.7k Followers)
  • LinkedIn (3.7k Followers)
  • YouTube (1.3k Followers)
  • GitHub (Repository)
  • GitHub (Gists)
Looking to publish sponsored article on our website?

Contact us

Recent Posts

  • Hybrid Multi-Cloud Environments Are Becoming UbiquitousJuly 12, 2023
  • Data Protection on the InternetJuly 12, 2023
  • Basics of BJT TransistorJuly 11, 2023
  • What is Confidential Computing?July 11, 2023
  • How a MOSFET WorksJuly 10, 2023
PC users can consult Corrine Chorney for Security.

Want to know more about us?

Read Notability and Mentions & Our Setup.

Copyright © 2023 - The Customize Windows | dESIGNed by The Customize Windows

Copyright  · Privacy Policy  · Advertising Policy  · Terms of Service  · Refund Policy