LoginSignup
50
54

More than 5 years have passed since last update.

CentOSでGeoIPを利用してHTTPアクセスを制御する

Last updated at Posted at 2014-10-14

GeoIP

maxmindが提供する、IPアドレスから地理情報を確認することができるサービス(GeoIP)。

無償版と有償版が用意されている。

料金体系

GeoIP2 Databases

Database Lite 都市
精度の高い情報 -
頻繁な更新 -
月額 free $12 $90
大陸
都市 - -
郵便コード - -
緯度経度 - -

詳しい料金体系

Databaseの精度の確認

こちらに国名を入力して確認できる。
https://www.maxmind.com/ja/geoip2-city-accuracy?country=&resolution=postal

GeoIP2 Liteを使用しApacheのアクセスを国別に制限する

OSはCentOS6.5を使用する。

パッケージインストール

yum install mod_geoip GeoIP GeoIP-devel GeoIP-data zlib-devel

データベース取得

cd /usr/share/GeoIP/
mv GeoIP.dat GeoIP.dat_org
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.mmdb.gz
gunzip GeoIP.dat.gz

Apacheモジュール設定

/etc/httpd/conf.d/geoip.conf
<IfModule mod_geoip.c>
  GeoIPEnable On
  GeoIPScanProxyHeaders On
  GeoIPDBFile /usr/share/GeoIP/GeoIP.dat
  GeoIPDBFile /usr/share/GeoIP/GeoIP.dat MemoryCache
  GeoIPDBFile /usr/share/GeoIP/GeoIP.dat CheckCache
</IfModule>

#ログ出力したい用
LogFormat "%a [%h] %u %t %D \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"   GeoIP_Address=\"%{GEOIP_ADDR}e\" Country_Code=\"%{GEOIP_COUNTRY_CODE}e\" Country_Name=\"%{GEOIP_COUNTRY_NAME}e\"" combined

国別制限

/etc/httpd/conf.d/vhost.conf
<Location "/">
  Order Deny,Allow
  SetEnvIf GEOIP_COUNTRY_CODE JP BlockCountry
  #以下どちらか設定
  Allow from env=BlockCountry #日本のみのアクセス許可
  Deny from env=BlockCountry #日本からのアクセス拒否
</Location>

データベース更新仕込み

#!/bin/sh

GEOIP_MIRROR="http://geolite.maxmind.com/download/geoip/database"
GEOIPDIR=/usr/share/GeoIP
TMPDIR=

DATABASES="GeoLiteCity GeoLiteCountry/GeoIP asnum/GeoIPASNum GeoIPv6"

if [ -d "${GEOIPDIR}" ]; then
        cd $GEOIPDIR
        if [ -n "${DATABASES}" ]; then
                TMPDIR=$(mktemp -d geoipupdate.XXXXXXXXXX)

                echo "Updating GeoIP databases..."

                for db in $DATABASES; do
                        fname=$(basename $db)

                        wget --no-verbose -t 3 -T 60 "${GEOIP_MIRROR}/${db}.dat.gz" -O "${TMPDIR}/${fname}.dat.gz"
                        gunzip -fdc "${TMPDIR}/${fname}.dat.gz" > "${TMPDIR}/${fname}.dat"
                        mv "${TMPDIR}/${fname}.dat" "${GEOIPDIR}/${fname}.dat"
                        chmod 0644 "${GEOIPDIR}/${fname}.dat"
                done
                [ -d "${TMPDIR}" ] && rm -rf $TMPDIR
        fi
fi

GeoIPテスト

IPアドレスを入力することで、情報を確認することができます。
https://www.maxmind.com/ja/geoip-demo

アクセスすることで自端末の情報が確認できます。
http://dev.maxmind.com/geoip/geoip2/javascript/

参考サイト

IPアドレスから地理的位置情報のまとめ
http://tokkono.cute.coocan.jp/blog/slow/index.php/web-technology/ip-geolocation-restful-apis/

その他サービス
http://www.ipligence.com/free-ip-database?download_form

インストール参考
http://www.tecmint.com/install-mod_geoip-for-apache-in-rhelcentos-6-35-8/
http://www.tabimoba.net/entry/2013/11/20/112905#.VD0mtNTkdoE

50
54
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
50
54