京东一面:如何用 Nginx 禁止国外 IP 访问网站,直接凉凉!

优采云 发布时间: 2022-06-18 06:57

  京东一面:如何用 Nginx 禁止国外 IP 访问网站,直接凉凉!

  点击上方“Java基基”,选择“设为星标”

  做积极的人,而不是积极废人!

  每天14:00更新文章,每天掉亿点点头发...

  源码精品专栏

  先来说说为啥要写这篇文章,之前看了下 Nginx 的访问日志,发现每天有好多国外的 IP 地址来访问我的网站,并且访问的内容基本上都是恶意的。因此我决定禁止国外 IP 来访问我的网站。

  图片来自 Pexels

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

  [root@fxkj ~]# yum install libmaxminddb-devel -y<br />

  基于 Spring Boot + MyBatis Plus + Vue & Element 实现的后台管理系统 + 用户小程序,支持 RBAC 动态权限、多租户、数据权限、工作流、三方登录、支付、短信、商城等功能。

  项目地址:

  [root@fxkj tmp]#  git clone https://github.com/leev/ngx_http_geoip2_module.git<br />        [ro tmp]#<br />

  基于微服务的思想,构建在 B2C 电商场景下的项目实战。核心技术栈,是 Spring Boot + Dubbo 。未来,会重构成 Spring Cloud Alibaba 。

  项目地址:

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

  [root@fxkj tmp]# mv ngx_http_geoip2_module/ /usr/local/<br />        [root@fxkj local]# ll ngx_http_geoip2_module/<br />        total 60<br />        -rw-r--r-- 1 root root  1199 Aug 13 17:20 config<br />        -rw-r--r-- 1 root root  1311 Aug 13 17:20 LICENSE<br />        -rw-r--r-- 1 root root 23525 Aug 13 17:20 ngx_http_geoip2_module.c<br />        -rw-r--r-- 1 root root 21029 Aug 13 17:20 ngx_stream_geoip2_module.c<br />        -rw-r--r-- 1 root root  3640 Aug 13 17:20 README.md<br />

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

  下载 nginx 1.18 版本:

  [root@fxkj ~]# yum install libmaxminddb-devel -y<br />

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

  需要注意:

  [root@fxkj tmp]# /usr/local/nginx/sbin/nginx -V<br /><br />        nginx version: nginx/1.16.0<br /><br />        built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)<br /><br />        built with OpenSSL 1.0.2k-fips 26 Jan 2017<br /><br />        TLS SNI support enabled<br /><br />        configure arguments: –with-http_stub_status_module –prefix=/usr/local/nginx –user=nginx –group=nginx –with-http_ssl_module –with-stream<br />

  编译安装:

  [root@fxkj tmp]# tar -xf nginx-1.18.0.tar.gz<br />        [root@fxkj tmp]# cd nginx-1.18.0/<br />        [root@fxkj nginx-1.18.0]# ./configure --with-http_stub_status_module \<br />        --prefix=/usr/local/nginx \<br />        --user=nginx --group=nginx --with-http_ssl_module --with-stream \<br />        --add-module=/usr/local/ngx_http_geoip2_module<br />        [root@fxkj nginx-1.18.0]# make<br />        [root@fxkj nginx-1.18.0]# cp /usr/loca/nginx/sbin/nginx /usr/loca/nginx/sbin/nginx1.16    #备份<br />        [root@fxkj nginx-1.18.0]# cp objs/nginx /usr/local/nginx/sbin/    #用新的去覆盖旧的<br />        [root@fxkj nginx-1.18.0]# pkill nginx     #杀死nginx<br />        [root@fxkj nginx-1.18.0]# /usr/local/nginx/sbin/nginx    #再次启动Nginx<br />

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

  [root@fxkj nginx-1.18.0]# /usr/local/nginx/sbin/nginx -V<br /><br />        nginx version: nginx/1.18.0<br /><br />        built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)<br /><br />        built with OpenSSL 1.0.2k-fips 26 Jan 2017<br /><br />        TLS SNI support enabled<br /><br />        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<br />

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

  登录 网址,创建账户,下载最新的库文件。(账户创建就不演示了)点击左侧,Download Files:

  选择 GeoLite2 Country,点击 Download GZIP 下载即可:

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

  [root@fxkj local]# cd /usr/share/GeoIP/<br />        [root@fxkj GeoIP]# ll<br />        total 69612<br />        lrwxrwxrwx. 1 root root       17 Mar  7  2019 GeoIP.dat -> GeoIP-initial.dat<br />        -rw-r--r--. 1 root root  1242574 Oct 30  2018 GeoIP-initial.dat<br />        lrwxrwxrwx. 1 root root       19 Mar  7  2019 GeoIPv6.dat -> GeoIPv6-initial.dat<br />        -rw-r--r--. 1 root root  2322773 Oct 30  2018 GeoIPv6-initial.dat<br />        -rw-r--r--  1 root root  3981623 Aug 12 02:37 GeoLite2-Country.mmdb<br />

  ⑥配置 nginx 配置文件

  修改前先备份配置文件:

  [root@fxkj ~]# cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf-bak<br />        [root@fxkj ~]# vim /usr/local/nginx/conf/nginx.conf<br />

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线