当前位置: 首页 > 科技观察

通过Nginx来实现禁止国外IP访问网站

时间:2023-03-20 14:24:43 科技观察

使用Nginx禁止国外IP访问网站,访问的内容基本都是恶意的。因此,我决定禁止国外IP访问我的网站。有很多方法可以实现这个功能。下面介绍一下基于NGINX的ngx_http_geoip2模块,禁止国外IP访问网站。-devel-y2.下载ngx_http_geoip2_module模块[root@fxkjtmp]#gitclonehttps://github.com/leev/ngx_http_geoip2_module.git[rotmp]#3。将模块解压到指定路径。我这里解压到/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--1rootroot3640Aug1317.c-rw-r--1rootroot3640Aug1317.xREA四、DMEnginmdREAmodule先说明一下环境,我的nginx版本是1.16,网上查了一下要安装ngx_http_geoip2模块至少是1.18及以上版本,所以这次安装升级了nginx1.18,加入了ngx_http_geoip2模块。下载nginx1.18版本[root@fxkj~]#yuminstalllibmaxminddb-devel-y解压nginx1.18软件包并升级到nginx1.18,添加ngx_http_geoip2模块需要注意:1、升级nginx,添加nginx模块只需要编译即可然后make就不需要makeinstll否则网上的nginx将被新版本的nginx2完全取代,编译前需要查看nginx中当前安装了哪些模块[root@fxkjtmp]#/usr/local/nginx/sbin/nginx-Vnginx版本:nginx/1.16.0builtbygcc4.8.520150623(RedHat4.8.5-39)(GCC)builtwithOpenSSL1.0.2k-fips26Jan2017TLSSNIsupportenabledconfigurearguments:–with-http_stub_status_module–prefix=/usr/local/nginx–user=nginx–x–w_group=nginx编译安装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#Killnginx[root@fxkjnginx-1.18.0]#/usr/local/nginx/sbin/nginx#再次启动Nginx查看nginx版本和安装的模块[root@fxkjnginx-1.18.0]#/usr/local/nginx/sbin/nginx-Vnginxversion:nginx/1.18.0builtbygcc4.8.520150623(RedHat4.8.5-39)(GCC)builtwithOpenSSL1.0.2k-fips26Jan2017TLSSNIsupportenabledconfigurearguments:–with-http_stub_status_module–prefix=/usr/local/nginx–user=nginx–groupwith=http–wsl–withstream–add-module=/usr/local/ngx_http_geoip2_module5.下载最新的IP地址数据库文件模块安装成功后,还必须在Nginx中指定数据库。安装运行时库时默认安装了两个,位于/usr/share/GeoIP/目录下,一个只有IPv4,一个包括IPv4和IPv6:登录www.maxmind.com网站,创建一个账号下载最新的库文件(账号创建不演示)点击左侧,DownloadFiles选择GeoLite2Country,点击DownloadGZIP下载并上传到/usr/share/GeoIP/解压[root@fxkjlocal]#cd/usr/share/GeoIP/[root@fxkjGeoIP]#lltotal69612lrwxrwxrwx.1rootroot17Mar72019GeoIP.dat->GeoIP-initial.dat-rw-r--r--.1rootroot1242574Oct302018GeoIP-initial.datlrwxrwxrwx.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中添加几行定义数据库文件位置geoip2/usr/share/GeoIP/GeoLite2-City.mmdb{auto_reload5m;$geoip2_data_country_codecountryiso_code;}map$geoip2_data_country_code$allowed_country{defaultyes;CNno;}在服务器位置下添加条件。如果IP是国外IP,会执行下面的返回动作。我在这里定义了3种类型,并注释了其中的两种访问IP为国外IP时,直接返回404if($allowed_country=yes){#returnhttps://www.baidu.com;#return/home/japan;return404;}修改后查看配置文件,重新加载nginx[root@fxkj~]#/usr/local/nginx/sbin/nginx-tnginx:theconfigurationfile/usr/local/nginx/conf/nginx.confsyntaxisoknginx:configurationfile/usr/local/nginx/conf/nginx.conftestissuccessful[roo@fxkj~]#/usr/local/nginx/sbin/nginx-sreload7.模拟测试验证使用海外节点服务器访问网站。这里我的IP来自韩国。访问该网站时,您会看到错误404NotFound。我们再看一下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禁止外网IP访问网站,就结束了~