nginx中自帶GeoIP模塊可以屏蔽指定IP的請求,只不過默認(rèn)沒有被編譯,打開以后我們只要再下載一個IP規(guī)則就行,Nginx服務(wù)器中配置GeoIP模塊來攔截指定國家IP。? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ?( 推薦學(xué)習(xí):nginx使用?)
最近有一個網(wǎng)站項(xiàng)目需求:需要屏蔽國內(nèi)的方問請求。花時間研究了一下這方面的資料。目前找到的最佳方法就是使用 Nginx 的 GeoIP 模塊來實(shí)現(xiàn)地區(qū)的識別。然后配置相關(guān)國家的 ISO 名稱,禁止訪問即可。記錄一下相關(guān)過程。
編譯 GeoIP 組件
maxmind 提供的免費(fèi)版數(shù)據(jù)庫已經(jīng)可以滿足需求,在使用數(shù)據(jù)庫前,需要先編譯 GeoIP 組件:
wget?http://geolite.maxmind.com/download/geoip/api/c/GeoIP-1.4.8.tar.gz ./configure make make?install
下載 IP 庫
從 maxmind 下載 IP 數(shù)據(jù)包并解壓。 這個是國家的ip數(shù)據(jù)包:
wget?http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz gunzip?GeoIP.dat.gz
這個是城市的ip數(shù)據(jù)包:
wget?http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz gunzip?GeoLiteCity.dat.gz
執(zhí)行完上面的命令后,會得到 GeoIP.dat 和 GeoLiteCity.dat 文件。將這兩個文件復(fù)制到 Nginx 的 conf 目錄。
? 版權(quán)聲明
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載。
THE END