小知识:基于Nginx禁止指定IP、国外IP访问我的网站

nginx禁止指定ip、国外ip访问我的网站

%小知识:基于Nginx禁止指定IP、国外IP访问我的网站-猿站网-插图

想要实现这个功能有很多方法,下面我就来介绍基于 nginx 的 ngx_http_geoip2 模块来禁止国外 ip 访问网站。

①安装 geoip2 扩展依赖:

?
1
[root@fxkj ~]# yum install libmaxminddb-devel -y

②下载 ngx_http_geoip2_module 模块:

?
1
2
[ro tmp]#

③解压模块到指定路径

我这里解压到 /usr/local 目录下:

?
1
2
3
4
5
6
7
8
[root@fxkj tmp]# mv ngx_http_geoip2_module/ /usr/local/
[root@fxkj local]# ll ngx_http_geoip2_module/
total 60
-rw-r–r– 1 root root  1199 aug 13 17:20 config
-rw-r–r– 1 root root  1311 aug 13 17:20 license
-rw-r–r– 1 root root 23525 aug 13 17:20 ngx_http_geoip2_module.c
-rw-r–r– 1 root root 21029 aug 13 17:20 ngx_stream_geoip2_module.c
-rw-r–r– 1 root root  3640 aug 13 17:20 readme.md

%小知识:基于Nginx禁止指定IP、国外IP访问我的网站-1猿站网-插图

④安装 nginx 模块

首先说明下环境,我的 nginx 版本是 1.16,在网上查了下安装 ngx_http_geoip2 模块至少需要 1.18 版本及以上,因此此次安装我是升级 nginx1.18,添加 ngx_http_geoip2 模块。

下载 nginx 1.18 版本:

?
1
[root@fxkj ~]# yum install libmaxminddb-devel -y

解压 nginx1.18 软件包,并升级为 nginx1.18,添加 ngx_http_geoip2 模块。

需要注意:

升级 nginx,添加 nginx 模块,只需要编译,然后 make。不需要 make instll,不然线上的 nginx 会被新版本 nginx 完完整整的替换掉。 编译前需要看下 nginx 当前安装了哪些模块。
?
1
2
3
4
5
6
[root@fxkj tmp]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.16.0
built by gcc 4.8.5 20150623 (red hat 4.8.5-39) (gcc)
built with openssl 1.0.2k-fips 26 jan 2017
tls sni support enabled
configure arguments: –with-http_stub_status_module –prefix=/usr/local/nginx –user=nginx –group=nginx –with-http_ssl_module –with-stream

编译安装:

?
1
2
3
4
5
6
7
8
9
10
11
[root@fxkj tmp]# tar -xf nginx-1.18.0.tar.gz
[root@fxkj tmp]# cd nginx-1.18.0/
[root@fxkj nginx-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@fxkj nginx-1.18.0]# make
[root@fxkj nginx-1.18.0]# cp /usr/loca/nginx/sbin/nginx /usr/loca/nginx/sbin/nginx1.16    #备份
[root@fxkj nginx-1.18.0]# cp objs/nginx /usr/local/nginx/sbin/    #用新的去覆盖旧的
[root@fxkj nginx-1.18.0]# pkill nginx     #杀死nginx
[root@fxkj nginx-1.18.0]# /usr/local/nginx/sbin/nginx    #再次启动nginx

查看 nginx 版本,以及安装的模块:

?
1
2
3
4
5
6
[root@fxkj nginx-1.18.0]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (red hat 4.8.5-39) (gcc)
built with openssl 1.0.2k-fips 26 jan 2017
tls sni support enabled
configure arguments: –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 地址数据库文件

模块安装成功后,还要在 nginx 里指定数据库,在安装运行库时默认安装了两个,位于 /usr/share/geoip/ 目录下,一个只有 ipv4,一个包含 ipv4 和 ipv6。

登录 www.maxmind.com 网址,创建账户,下载最新的库文件。(账户创建就不演示了)点击左侧,download files:

%小知识:基于Nginx禁止指定IP、国外IP访问我的网站-2猿站网-插图

选择 geolite2 country,点击 download gzip 下载即可:

%小知识:基于Nginx禁止指定IP、国外IP访问我的网站-3猿站网-插图

上传到 /usr/share/geoip/ 下并解压:

?
1
2
3
4
5
6
7
8
[root@fxkj local]# cd /usr/share/geoip/
[root@fxkj geoip]# ll
total 69612
lrwxrwxrwx. 1 root root       17 mar  7  2019 geoip.dat -> geoip-initial.dat
-rw-r–r–. 1 root root  1242574 oct 30  2018 geoip-initial.dat
lrwxrwxrwx. 1 root root       19 mar  7  2019 geoipv6.dat -> geoipv6-initial.dat
-rw-r–r–. 1 root root  2322773 oct 30  2018 geoipv6-initial.dat
-rw-r–r–  1 root root  3981623 aug 12 02:37 geolite2-country.mmdb

⑥配置 nginx 配置文件

修改前先备份配置文件:

?
1
2
[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 中添加几行,定义数据库文件位置:

?
1
2
3
4
5
6
7
8
geoip2 /usr/share/geoip/geolite2-city.mmdb {
auto_reload 5m;
$geoip2_data_country_code country iso_code;
}
map $geoip2_data_country_code $allowed_country {
default yes;
cn no;
}

%小知识:基于Nginx禁止指定IP、国外IP访问我的网站-4猿站网-插图

在 server 中的 location 下添加条件,如果满足 ip 是国外 ip,就执行下面的 return 动作,我这里定义了 3 种,注释了其中两个。

当访问 ip 是国外 ip,直接返回 404:

?
1
2
3
4
5
if ($allowed_country = yes) {
# return https://www.baidu.com;
# return /home/japan;
return 404;
}

%小知识:基于Nginx禁止指定IP、国外IP访问我的网站-5猿站网-插图

修改完毕后,检测下配置文件,重新加载下 nginx:

?
1
2
3
4
[root@fxkj ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[roo@fxkj ~]# /usr/local/nginx/sbin/nginx -s reload

⑦模拟测试验证

使用海外节点的服务器去访问网站,这里我的 ip 是来自于韩国:

%小知识:基于Nginx禁止指定IP、国外IP访问我的网站-6猿站网-插图

可以看到访问网站报错 404 not found:

%小知识:基于Nginx禁止指定IP、国外IP访问我的网站-7猿站网-插图

我们再来看下 nginx 的访问日志:

?
1
“13.125.1.194 – – [14/aug/2020:16:15:51 +0800] “get /favicon.ico http/1.1” 404 548 “https://www.fxkjnj.com/” “mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/84.0.4147.125 safari/537.36”

%小知识:基于Nginx禁止指定IP、国外IP访问我的网站-8猿站网-插图

至此,我们通过 nginx 来实现禁止国外 ip 访问网站就结束了~

%小知识:基于Nginx禁止指定IP、国外IP访问我的网站-9猿站网-插图

总结

到此这篇关于用nginx禁止指定ip、国外ip访问我的网站的文章就介绍到这了,更多相关nginx禁止国外ip访问网站内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/qq_44866828/article/details/124519559

声明: 猿站网有关资源均来自网络搜集与网友提供,任何涉及商业盈利目的的均不得使用,否则产生的一切后果将由您自己承担! 本平台资源仅供个人学习交流、测试使用 所有内容请在下载后24小时内删除,制止非法恶意传播,不对任何下载或转载者造成的危害负任何法律责任!也请大家支持、购置正版! 。本站一律禁止以任何方式发布或转载任何违法的相关信息访客发现请向站长举报,会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。本网站的资源部分来源于网络,如有侵权烦请发送邮件至:2697268773@qq.com进行处理。
建站知识

小知识:详解linux系统目录sys,tmp,usr,var!

2023-3-16 5:14:40

建站知识

小知识:linux查看用过的命令方法总结

2023-3-16 5:23:10

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索