本文先容如何利用GeoIP模塊讓nginx實(shí)現(xiàn)限制某個(gè)地域用戶會(huì)見的成果。nginx要加上 --with-http_geoip_module 參數(shù)舉辦編譯。
nginx -V
假如你在輸出界面看到了 --with-http_geoip_module,那么就說明nginx已經(jīng)編譯了GeoIP模塊。
2、接下來我們安裝GeoIP數(shù)據(jù)庫
在Debian/Ubuntu系統(tǒng),我們可以執(zhí)行下面的呼吁舉辦安裝:
apt-get install geoip-database libgeoip1
安裝完成之后,GeoIP數(shù)據(jù)庫會(huì)被安裝在 /usr/share/GeoIP/GeoIP.dat。
這個(gè)GeoIP.dat是GeoIP數(shù)據(jù)庫文件,利用apt-get呼吁安裝的話這個(gè)文件不是最新的,我們可以從 http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz 這里下載最新的GeoIP數(shù)據(jù)庫文件。
mv /usr/share/GeoIP/GeoIP.dat /usr/share/GeoIP/GeoIP.dat_bak
cd /usr/share/GeoIP/
wget
http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
gunzip GeoIP.dat.gz
3、此刻來設(shè)置nginx.conf文件
vi /etc/nginx/nginx.conf
將下面的內(nèi)容添加進(jìn) http {} 區(qū)域,而且要放在任何 include 語句之前。
geoip_country /usr/share/GeoIP/GeoIP.dat;
map $geoip_country_code $allowed_country {
default yes;
FK no;
FM no;
EH no;
}
上面這些語句是除了 FK,F(xiàn)M,EH這三個(gè)地域的用戶答允其它地域的用戶會(huì)見。
也可以只答允部門地域用戶會(huì)見:
geoip_country /usr/share/GeoIP/GeoIP.dat;
map $geoip_country_code $allowed_country {
default no;
FK yes;
FM yes;
EH yes;
}
上面這些語句是除了 FK,F(xiàn)M,EH這三個(gè)地域的用戶其它地域的用戶都不答允會(huì)見。
上面的語句只是配置了一個(gè) $allowed_country 變量,要最終實(shí)現(xiàn)克制配置的地域用戶會(huì)見,我們要對(duì) $allowed_country 變量舉辦判定處理懲罰。
在 server {} 區(qū)域里添加以下內(nèi)容:
if ($allowed_country = no) {
return 403;
}
也可以針對(duì)某個(gè)特定url舉辦限制:
location /special {
if ($allowd_country = no) {
return 403;
}
}
4、重啟nginx
/etc/init.d/nginx reload
這樣我們就實(shí)現(xiàn)了nginx限制某個(gè)地域用戶會(huì)見的成果。
,臺(tái)灣主機(jī) 臺(tái)灣伺服器