Opensourcetechブログ

OpensourcetechによるNGINX/Kubernetes/Zabbix/Neo4j/Linuxなどオープンソース技術に関するブログです。

Nginx 1.7.10 with GeoIP moduleによるアクセスログ管理

こんにちは、鯨井貴博@opensourcetechです。

 

今回は、NginxのGeoIPモジュールを使って、

ログにクライアントのアクセス元IPアドレスから国名などの情報を記載してみます。

 

なお、OSとしては、CentOS6.6の64ビット版を使用しています。

f:id:opensourcetech:20150224003834p:plain

 

使用するモジュールはGeoIPとなり、

コンパイル時に「--with-http_geoip_module」を指定する必要があります。

http://wiki.nginx.org/Modules

f:id:opensourcetech:20150224003844p:plain

 

まずは、/tmpにnginxのtarballをダウンロードします。

cd /tmp

wget http://nginx.org/download/nginx-1.7.10.tar.gz

f:id:opensourcetech:20150224003852p:plain

 

tarballをtarで解凍します。

tar zxvf nginx-1.7.10.tar.gz

f:id:opensourcetech:20150224003859p:plain

 f:id:opensourcetech:20150224003906p:plain

 

続いてNginxの依存関係にあるパッケージをインストールします。

yum install zlib-devel gcc openssl openssl-devel pcre-devel

f:id:opensourcetech:20150224003914p:plain

 f:id:opensourcetech:20150224003921p:plain

 

そして、GeoIPモジュールを追加してconfigureスクリプトを実施します。

cd //tmp/nginx-1.7.10

./configure --with-http_geoip_module

f:id:opensourcetech:20150224003929p:plain

 

しかし、GeoIP libraryが「not found」となり、

configure errorとなってしまいました。

f:id:opensourcetech:20150224003935p:plain

 

なので、GeoIP libraryをインストールします。

GeoIP libraryのインストールには、

通常のレポジトリからではなくEpelなど別のレポジトリから行う必要があります。

cd /tmp

wget http://ftp.jaist.ac.jp/pub/Linux/Fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm

rpm -ivh epel-release-6-8.noarch.rpm

f:id:opensourcetech:20150224003942p:plain

 

rpmでダウンロードしたパッケージをインストールすると、

/etc/yum.repos.d配下にepel.repoなどのファイルが追加されます。

f:id:opensourcetech:20150224003954p:plain

 その内容は、以下のスクリーンショットのようになっています。

f:id:opensourcetech:20150224003958p:plain

 

これでGeoIP libraryが入るはず!

yum install GeoIP GeoIP-devel

f:id:opensourcetech:20150224004005p:plain

f:id:opensourcetech:20150224004012p:plain

 

改めてconfigureスクリプトを実施します。

cd /tmp/nginx-1.7.10

./configure --with-http_geoip_module

f:id:opensourcetech:20150224004020p:plain

今度は無事に成功です!

 f:id:opensourcetech:20150224004026p:plain

 

あとは、make と  make installします。

make

make install

f:id:opensourcetech:20150224004031p:plain

f:id:opensourcetech:20150224004038p:plain

 

デフォルトの場合、

Nginxの実行ファイルが/usr/local/nginx/sbin配下にあるので、

インストール確認を行います。

/usr/local/nginx/sbin -v

/usr/local/nginx/sbin -V

f:id:opensourcetech:20150224004043p:plain

 

GeoIPモジュールでは、

別途MAX MINDが提供しているデータベースが必要となりますので、

サイトよりダウンロードします。

f:id:opensourcetech:20150224004049p:plain

 

GeoLite Legacy Downloadable Databases « Maxmind Developer Site

以下の赤で囲んだものが今回しようしたデータベースです。

f:id:opensourcetech:20150224004056p:plain

 

cd /usr/local/nginx/conf

wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz

wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz

f:id:opensourcetech:20150224004104p:plain

 

gzip形式で圧縮されているので、gunzipなどで解凍します。

gunzip GeoIP.dat.gz

gunzip GeoLiteCity.dat.gz

f:id:opensourcetech:20150224004112p:plain

 

 

続いては、nginxの設定を行います。

設定ファイルは、/usr/local/nginx/conf内にあるnginx.confとなります。

そのhttpブロック内に、以下を追記します。

    geoip_country /usr/local/nginx/conf/GeoIP.dat;
    geoip_city    /usr/local/nginx/conf/GeoLiteCity.dat;

f:id:opensourcetech:20150224004119p:plain

 

また、GeoIPモジュールを使用した場合、

以下の変数が使用可能になります。

※各変数の詳細は、Module ngx_http_geoip_moduleで確認出来ます。

-------

$geoip_area_code
$geoip_city
$geoip_city_continent_code
$geoip_city_country_code
$geoip_city_country_code3
$geoip_city_country_name
$geoip_country_code
$geoip_country_code3
$geoip_country_name
$geoip_dma_code
$geoip_latitude
$geoip_longitude
$geoip_org
$geoip_postal_code
$geoip_region
$geoip_region_name

 

なので、それらを使用するよういlog_formatを編集します。

log_format geoip '["$date_gmt"] ["$geoip_area_code" "$geoip_city" "$geoip_city_continent_code" "$geoip_city_country_code" "$geoip_city_country_code3" "$geoip_city_country_name" "$geoip_country_code" "$geoip_country_code3" "$geoip_country_name" "$geoip_dma_code" "$geoip_latitude" "$geoip_longitude" "$geoip_org" "$geoip_postal_code" "$geoip_region" "$geoip_region_name"] ["$remote_addr" "$remote_port" "$request"} ["$server_name" "$server_port" "$server_protocol"]';

 

また、アクセスログの記録様式に上記を指定します。

    access_log  logs/access.log  geoip;

f:id:opensourcetech:20150224004126p:plain

これで設定は完了です。

 

 

nginxの起動/起動確認

/usr/local/nginx/sbin/nginx

netstat -tan | grep 80

ps aux | grep nginx

f:id:opensourcetech:20150224004133p:plain

 

FW(iptables)の解放

iptables -L --line-numbers

iptables -I INPUT 5 -p tcp --dport 80 -j ACCEPT

iptables -L --line-numbers

 (iptables-save > /etc/sysconfig/iptablesもやっておくといいでしょう)

f:id:opensourcetech:20150224004145p:plain

 

 

上記まで出来た状態でクライアントからNginxへアクセスをすると、

以下のように/usr/local/nginx/logs/access.logに GeoIPモジュールによる情報が記録されるようになります。

tail -f /usr/local/nginx/logs/access.log

f:id:opensourcetech:20150224004151p:plain

 

 

 

 

にほんブログ村 IT技術ブログ Linuxへ
Linux

にほんブログ村 IT技術ブログ オープンソースへ
オープンソース

Opensourcetech by Takahiro Kujirai