用Nginx禁止指定IP、国外IP访问我的网站 
想要实现这个功能有很多方法,下面我就来介绍基于 Nginx 的 ngx_http_geoip2 模块来禁止国外 IP 访问网站。
①安装 geoip2 扩展依赖:[root@fxkj ~]# yum install libmaxminddb-devel -y
②下载 ngx_http_geoip2_module 模块:[root@fxkj tmp]# git clone https://github.com/leev/ngx_http_geoip2_module.git[ro tmp]#
③解压模块到指定路径我这里解压到 /usr/local 目录下: [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 模块首先说明下环境,我的 nginx 版本是 1.16,在网上查了下安装 ngx_http_geoip2 模块至少需要 1.18 版本及以上,因此此次安装我是升级 nginx1.18,添加 ngx_http_geoip2 模块。 下载 nginx 1.18 版本: [root@fxkj ~]# yum install libmaxminddb-devel -y 解压 nginx1.18 软件包,并升级为 nginx1.18,添加 ngx_http_geoip2 模块。 需要注意: - 升级 nginx,添加 nginx 模块,只需要编译,然后 make。不需要 make instll,不然线上的 nginx 会被新版本 nginx 完完整整的替换掉。
- 编译前需要看下 nginx 当前安装了哪些模块。
[root@fxkj tmp]# /usr/local/nginx/sbin/nginx -Vnginx version: nginx/1.16.0built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)built with OpenSSL 1.0.2k-fips 26 Jan 2017TLS SNI support enabledconfigure arguments: 下载地址: 深入解析docker文件分层原理 nginx使用内置模块配置限速限流的方法实例 |