前言
大家好,這里是浩道Linux,主要給大家分享Linux、Python、網(wǎng)絡(luò)通信、網(wǎng)絡(luò)安全等相關(guān)的IT知識(shí)平臺(tái)。
最近不少小伙伴反饋?zhàn)约壕S護(hù)的項(xiàng)目中,經(jīng)常在后臺(tái)可以看到來自國外IP的攻擊,令人頭疼。今天浩道跟大家分享如何通過Nginx來實(shí)現(xiàn)禁止國外IP訪問網(wǎng)站,確保你網(wǎng)站免遭國外IP攻擊了!
如果你對(duì)nginx相關(guān)知識(shí)感興趣,可以看看這篇,可以確保你快速掌握其功能與應(yīng)用場景!
想要實(shí)現(xiàn)這個(gè)功能有很多方法,下面我就來介紹基于 NGINX 的 ngx_http_geoip2 模塊 來禁止國外 IP 訪問網(wǎng)站
一、安裝 geoip2 擴(kuò)展依賴
[root@fxkj~]#yuminstalllibmaxminddb-devel-y
二、下載 ngx_http_geoip2_module 模塊
[root@fxkjtmp]#gitclonehttps://github.com/leev/ngx_http_geoip2_module.git [rotmp]#
三、解壓模塊到指定路徑
我這里解壓到/usr/local 目錄下
[root@fxkjtmp]#mvngx_http_geoip2_module//usr/local/ [root@fxkjlocal]#llngx_http_geoip2_module/ total60 -rw-r--r--1rootroot1199Aug1317:20config -rw-r--r--1rootroot1311Aug1317:20LICENSE -rw-r--r--1rootroot23525Aug1317:20ngx_http_geoip2_module.c -rw-r--r--1rootroot21029Aug1317:20ngx_stream_geoip2_module.c -rw-r--r--1rootroot3640Aug1317:20README.md
四、安裝 nginx 模塊
首先說明下環(huán)境,我的 nginx 版本是 1.16 , 在網(wǎng)上查了下 安裝 ngx_http_geoip2 模塊至少需要 1.18 版本及以上,因此此次安裝我是 升級(jí) nginx1.18,添加 ngx_http_geoip2 模塊。
下載 nginx 1.18 版本
[root@fxkj~]#yuminstalllibmaxminddb-devel-y
解壓 nginx1.18 軟件包并 升級(jí)為 nginx1.18 ,添加 ngx_http_geoip2 模塊
需要注意:
1、升級(jí) nginx, 添加 nginx 模塊 只需要 編譯 然后 make 不需要 make instll 不然線上的 nginx 會(huì)被新版本 nginx 完完整整的替換掉
2、編譯前 需要看下 nginx 當(dāng)前安裝了哪些模塊
[root@fxkjtmp]#/usr/local/nginx/sbin/nginx-V nginxversion:nginx/1.16.0 builtbygcc4.8.520150623(RedHat4.8.5-39)(GCC) builtwithOpenSSL1.0.2k-fips26Jan2017 TLSSNIsupportenabled configurearguments:–with-http_stub_status_module–prefix=/usr/local/nginx–user=nginx–group=nginx–with-http_ssl_module–with-stream
編譯安裝
[root@fxkjtmp]#tar-xfnginx-1.18.0.tar.gz [root@fxkjtmp]#cdnginx-1.18.0/ [root@fxkjnginx-1.18.0]#./configure--with-http_stub_status_module --prefix=/usr/local/nginx --user=nginx--group=nginx--with-http_ssl_module--with-stream --add-module=/usr/local/ngx_http_geoip2_module [root@fxkjnginx-1.18.0]#make [root@fxkjnginx-1.18.0]#cp/usr/loca/nginx/sbin/nginx/usr/loca/nginx/sbin/nginx1.16#備份 [root@fxkjnginx-1.18.0]#cpobjs/nginx/usr/local/nginx/sbin/#用新的去覆蓋舊的 [root@fxkjnginx-1.18.0]#pkillnginx#殺死nginx [root@fxkjnginx-1.18.0]#/usr/local/nginx/sbin/nginx#再次啟動(dòng)Nginx
查看 nginx 版本 以及安裝的模塊
[root@fxkjnginx-1.18.0]#/usr/local/nginx/sbin/nginx-V nginxversion:nginx/1.18.0 builtbygcc4.8.520150623(RedHat4.8.5-39)(GCC) builtwithOpenSSL1.0.2k-fips26Jan2017 TLSSNIsupportenabled configurearguments:–with-http_stub_status_module–prefix=/usr/local/nginx–user=nginx–group=nginx–with-http_ssl_module–with-stream–add-module=/usr/local/ngx_http_geoip2_module
五、下載最新的 IP 地址數(shù)據(jù)庫文件
模塊安裝成功后,還要在 Nginx 里指定數(shù)據(jù)庫,在安裝運(yùn)行庫時(shí)默認(rèn)安裝了兩個(gè),位于 /usr/share/GeoIP/ 目錄下,一個(gè)只有 IPv4,一個(gè)包含 IPv4 和 IPv6:
登錄 www.maxmind.com 網(wǎng)址,創(chuàng)建賬戶 下載最新的庫文件(賬戶創(chuàng)建就不演示了)
點(diǎn)擊左側(cè) ,Download Files
選擇 GeoLite2 Country ,點(diǎn)擊 Download GZIP 下載即可
上傳到 /usr/share/GeoIP/ 下并解壓
[root@fxkjlocal]#cd/usr/share/GeoIP/ [root@fxkjGeoIP]#ll total69612 lrwxrwxrwx.1rootroot17Mar72019GeoIP.dat->GeoIP-initial.dat -rw-r--r--.1rootroot1242574Oct302018GeoIP-initial.dat lrwxrwxrwx.1rootroot19Mar72019GeoIPv6.dat->GeoIPv6-initial.dat -rw-r--r--.1rootroot2322773Oct302018GeoIPv6-initial.dat -rw-r--r--1rootroot3981623Aug1202:37GeoLite2-Country.mmdb
六、配置 nginx 配置文件
修改前 先備份配置文件
[root@fxkj ~]#cp/usr/local/nginx/conf/nginx.conf/usr/local/nginx/conf/nginx.conf-bak [root@fxkj ~]#vim/usr/local/nginx/conf/nginx.conf
在 http 中添加 幾行,定義數(shù)據(jù)庫文件位置
geoip2/usr/share/GeoIP/GeoLite2-City.mmdb{ auto_reload5m; $geoip2_data_country_codecountryiso_code; } map$geoip2_data_country_code$allowed_country{ defaultyes; CNno; }

在 server 中的 location 下 添加 條件
如果滿足 IP 是國外 IP 就 執(zhí)行下面的 return 動(dòng)作,我這里定義了 3 種, 注釋了其中兩個(gè)。
當(dāng)訪問 IP 是國外 IP ,直接返回 404
if($allowed_country=yes){ #returnhttps://www.baidu.com; #return/home/japan; return404; }
修改完畢后, 檢測下配置文件,重新加載下 nginx
[root@fxkj~]#/usr/local/nginx/sbin/nginx-t nginx:theconfigurationfile/usr/local/nginx/conf/nginx.confsyntaxisok nginx:configurationfile/usr/local/nginx/conf/nginx.conftestissuccessful [roo@fxkj~]#/usr/local/nginx/sbin/nginx-sreload
7、模擬測試驗(yàn)證
使用海外節(jié)點(diǎn)的服務(wù)器去訪問網(wǎng)站
這里我的 IP 是 來自于韓國
可以看到訪問網(wǎng)站報(bào)錯(cuò) 404 Not Found
我們?cè)賮砜聪?nginx 的訪問日志
“13.125.1.194––[14/Aug/2020:16:15:51+0800]“GET/favicon.icoHTTP/1.1”404548“https://www.fxkjnj.com/”“Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/84.0.4147.125Safari/537.36”

至此,我們通過 Nginx 來實(shí)現(xiàn)禁止國外 IP 訪問網(wǎng)站 就結(jié)束了!
審核編輯:湯梓紅
-
Linux
+關(guān)注
關(guān)注
87文章
11511瀏覽量
213843 -
IP
+關(guān)注
關(guān)注
5文章
1805瀏覽量
152609 -
網(wǎng)站
+關(guān)注
關(guān)注
2文章
259瀏覽量
23555 -
nginx
+關(guān)注
關(guān)注
0文章
171瀏覽量
12612
原文標(biāo)題:網(wǎng)站經(jīng)常遭受外國IP攻擊?可以通過Nginx來禁止其訪問!
文章出處:【微信號(hào):浩道linux,微信公眾號(hào):浩道linux】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
網(wǎng)站用戶終于解決訪問網(wǎng)站慢的問題
IP屏蔽軟件作用
如何在電腦上禁止訪問QQ空間
如何訪問國外網(wǎng)站,包括github網(wǎng)站????
【NanoPi NEO試用體驗(yàn)】之安裝配置Nginx環(huán)境WEB網(wǎng)站詳解
NanoPi NEO - 可靠的Nginx 網(wǎng)絡(luò)服務(wù)器
如何通過Dataworks禁止MaxCompute 子賬號(hào)跨Project訪問
主要學(xué)習(xí)下nginx的安裝配置
蘋果禁止iPhone黑客訪問App Store應(yīng)用商店
爬蟲實(shí)現(xiàn)目標(biāo)網(wǎng)站驗(yàn)證碼登陸
配置Nginx訪問日志
如何通過Nginx實(shí)現(xiàn)遠(yuǎn)程調(diào)試本機(jī)代碼
多個(gè)相同IP設(shè)備通過網(wǎng)段隔離器實(shí)現(xiàn)不同IP的訪問映射

評(píng)論