ADJ網路實驗室
打印

[介紹] 在CentOS 6 的 nginx 上安裝 GeoIP Module 來 Block IP

在CentOS 6 的 nginx 上安裝 GeoIP Module 來 Block IP

有時後我們遇到DOS Attack or 特殊目地可能會想要Block 整個country or 只開放某個國家的IP才能看...
這時後使用 Nginx 來做就簡單很多...步驟如下:

(1) Install GeoIP library via yum
引用:

*** For CentOS 6 – 64-bit ***
# rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
# rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

# yum install geoip geoip-devel

After successful installation, the library will be stored in: /usr/share/GeoIP/GeoIP.dat

(2) Configure nginx
引用:

#vi /etc/nginx/conf/nginx.conf

http {
[...]
geoip_country /usr/share/GeoIP/GeoIP.dat;
map $geoip_country_code $allowed_country {
default yes;
CN no;
}

[...]
}


(3) Configure nginx virtualhost:
引用:

#vi /etc/nginx/conf.d/yourdomain.conf

server {
[...]
if ($allowed_country = no) {
return 403;
}

[...]
}


然後 nginx 重啟之後.從 CN 來的訪客就會看不到網站囉~~

2015.08.21 PS: 之前用 nginx v1.0.15 可以用...因為這個版本有把 --with-http_geoip_module 內建進去...可以最近升級到 nginx 1.6 跟 nginx 1.8 之後..
發現已經沒有內建了...所以不想麻煩的朋友...可以試著降版本就好囉....

引用:

# yum remove nginx
# yum install nginx-1.0.15


如果你要找尋相關的國家代碼. 可以參考底下網址:
http://dev.maxmind.com/geoip/legacy/codes/iso3166/






TOP

ARTERY.cn