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

使用Nginx禁止国外IP访问我的网站...

时间:2023-03-13 16:18:58 科技观察

先说说我为什么要写这篇文章。之前看了Nginx的访问日志,发现每天都有很多国外的IP地址访问我的网站。而且访问的内容基本都是恶意的。所以我决定禁止外国IP访问我的网站。图片来自Pexels。有很多方法可以实现这个功能。下面介绍一下基于Nginx的禁止国外IP访问网站的ngx_http_geoip2模块。①安装geoip2扩展依赖:[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--1root3640.Aug11AD77:2安装nginx模块先说明一下环境。我的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模块。注意:升级nginx,添加nginx模块,只需要编译然后make即可。不需要makeinstll,否则网上的nginx会被新版的nginx完全取代。在编译之前,需要查看nginx当前安装了哪些模块。[root@fxkjtmp]#/usr/local/nginx/sbin/nginx-Vnginxversion:nginx/1.16.0builtbygcc4.8.520150623(RedHat4.8.5-39)(GCC)builtwithOpenSSL1.0.2k-fips26Jan2017TLSSNIsupportenabledconfigurearguments:–with_stat_us/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#Backup[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/本地/nginx/sbin/nginx-Vnginx版本:nginx/1.18.0builtbygcc4.8.520150623(RedHat4.8.5-39)(GCC)builtwithOpenSSL1.0.2k-fips26Jan2017TLSSNIsupportenabledconfigurearguments:–with-http_stub_status_module–prefix=/usr/userlocal/inginx=nginx–g–ro=ngwith-http_ssl_module–with-stream–add-module=/usr/local/ngx_http_geoip2_module⑤下载最新的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.1rootroot19Mar72019GeoIP-IPvdinrat-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;}在locat下添加条件ion在服务器中,如果IP是外网IP,直接执行下面的return动作即可,我这里定义了3种,注释了其中两种。访问IP为国外IP时,直接返回404:if($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-sreload⑦模拟测试验证使用海外节点服务器访问网站。这里我的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访问网站,以及到此结束~作者:运维小哥编辑:陶家龙来源:头条网/i6860736292339057156/