静看光阴荏苒
不管不顾不问不说也不念

Nginx增加GeoIP模块实现屏蔽某个国家的IP访问

需要用到这个功能的人,我想都不是一般人,站估计也不是一般的站233

今天这篇文章还是老样子以CentOS7+宝塔为例,接下来所有操作都是基于宝塔的Nginx完成的。其实给Nginx增加上这个功能的同时,还可以顺带把宝塔的Nginx给升级了,宝塔现在只提供1.14.0版本的Nginx,今天我们可以用这个方法把Nginx升级到1.15.1,并且宝塔面板不受任何影响,在面板内依旧可以控制Nginx的各项操作。

首先使用Xshell登录到机器内,输入如下命令查看nginx当前编译的参数和模块:

nginx -V

如图所示,把configure arguments后面的值都复制下来:

因为只要是安装了宝塔并且使用宝塔安装过Nginx的机器,依赖环境都是完整的,所以我们可以直接把1.15.1版本的Nginx拿来编译:

wget http://nginx.org/download/nginx-1.15.1.tar.gz
tar -xzvf nginx-1.15.1.tar.gz
cd nginx-1.15.1
./configure --user=www --group=www --prefix=/www/server/nginx --with-openssl=/www/server/nginx/src/openssl --add-module=/www/server/nginx/src/ngx_devel_kit --add-module=/www/server/nginx/src/lua_nginx_module --add-module=/www/server/nginx/src/ngx_cache_purge --add-module=/www/server/nginx/src/nginx-sticky-module --add-module=/www/server/nginx/src/nginx-http-concat --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_gunzip_module --with-stream --with-stream_ssl_module --with-ipv6 --with-http_sub_module --with-http_flv_module --with-http_addition_module --with-http_realip_module --with-http_mp4_module --with-http_geoip_module --with-ld-opt=-Wl,-E
make

注:

1、这里的编译参数就直接粘贴我们之前复制的,只不过我们要增加一个GeoIP的模块,所以在原先的基础上再增加如下参数就行:

--with-http_geoip_module

2、只编译不安装,也就是只make不make install。

编译完成之后,我们要来安装geoip库,geoip库这个包在EPEL源里面,所以先装EPEL:

yum -y install epel-release

然后直接yum安装即可:

yum -y install geoip-devel

现在我们打开宝塔面板的后台,把nginx停止运行:

回到Xshell中去,备份宝塔的nginx然后使用我们刚编译好的nginx替换掉宝塔面板的nginx:

mv /www/server/nginx/sbin/nginx /www/server/nginx/sbin/nginx-lala.im
cp objs/nginx /www/server/nginx/sbin/nginx

确保一切正常,使用如下命令查看一下geoip模块是否已经加载:

ldd /www/server/nginx/sbin/nginx

如图所示,有libGeoIP回显就说明我们已经成功:

现在回到宝塔面板中,编辑Nginx的主配置文件,找到http段,在图片所示部分添加如下代码:

geoip_country /usr/share/GeoIP/GeoIP.dat;

添加位置如图所示:

完成之后重启一下Nginx,接着我们编辑站点的伪静态设置,写入如下规则:

location / {
default_type text/html;
charset utf-8;
if ($geoip_country_code = CN) {
return 502 "您所在的国家禁止访问本站!";
}
}

如图所示:

至此,大功告成,效果如图:

写在最后:

这个方法可以说屏蔽的最彻底,但是也不能说百分百就能全部屏蔽掉。比如一些广播IP还是无法精准识别,漏网之鱼总会有一些。另外使用本篇文章中的方法,你可以自由给Nginx添加更多其他的模块,这不局限于GeoIP。

赞(0)
未经允许不得转载:荒岛 » Nginx增加GeoIP模块实现屏蔽某个国家的IP访问
分享到: 更多 (0)

评论 19

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #1

    之前看到的教程都是用maxmind库来安装,安装是成功了,配置后却启动不了,按照你的方法一次成功 :oops:

    欧文斯6年前 (2018-07-13) Google Chrome 67.0.3396.99 Google Chrome 67.0.3396.99 Windows 10 x64 Edition Windows 10 x64 Edition回复
    • IPv6模块如何加载呢

      欧文斯6年前 (2018-07-13) Google Chrome 67.0.3396.99 Google Chrome 67.0.3396.99 Windows 10 x64 Edition Windows 10 x64 Edition回复
      • /usr/share/GeoIP/
        这目录里面有ipv6的库,但是我不清楚Nginx的配置文件是否可以同时加载两个库。。。

        LALA6年前 (2018-07-14) Google Chrome 66.0.3359.181 Google Chrome 66.0.3359.181 Windows 10 x64 Edition Windows 10 x64 Edition回复
        • 试过了,IPv6 包含 IPv4,所以只要启用 IPv6 即可,同样的,City 库包含 Country 库,启用 City 就可以了

          欧文斯6年前 (2018-07-16) Google Chrome 67.0.3396.99 Google Chrome 67.0.3396.99 Windows 10 x64 Edition Windows 10 x64 Edition回复
          • 我被你说的有点分不清楚了= =

            LALA6年前 (2018-07-16) Google Chrome 66.0.3359.181 Google Chrome 66.0.3359.181 Windows 10 x64 Edition Windows 10 x64 Edition
  2. #2

    按你的教程一次就成功了,隔壁吐槽的教程怎么都成不了

    TNT6年前 (2018-08-02) Google Chrome 67.0.3396.99 Google Chrome 67.0.3396.99 Windows 10 x64 Edition Windows 10 x64 Edition回复
    • 多谢老哥5星好评,冒昧的问一句隔壁吐槽是啥???

      LALA6年前 (2018-08-03) Google Chrome 67.0.3396.99 Google Chrome 67.0.3396.99 Windows 10 x64 Edition Windows 10 x64 Edition回复
      • 寡人吐槽胜地博客

        TNT6年前 (2018-08-06) Google Chrome 67.0.3396.99 Google Chrome 67.0.3396.99 Windows 10 x64 Edition Windows 10 x64 Edition回复
  3. #3

    想问下博主,现在的版本是GEOIP2的版本了,数据库格式是mmdb的格式,不再是dat格式的数据库了,我已经添加了geoip2模块到nginx中,应该如何应用到网站上呢?

    5年前 (2019-01-09) Google Chrome 71.0.3578.98 Google Chrome 71.0.3578.98 Windows 10 x64 Edition Windows 10 x64 Edition回复
    • 你添加的是这个模块吗?https://github.com/leev/ngx_http_geoip2_module
      如果是这个的话,把:($geoip_country_code = CN)
      改为:
      ($geoip2_data_country_code = CN)
      看看行不行。

      LALA5年前 (2019-01-10) Google Chrome 70.0.3538.110 Google Chrome 70.0.3538.110 Windows 10 x64 Edition Windows 10 x64 Edition回复
  4. #4

    大佬,按你的教程安装时成功了,运行ldd /www/server/nginx/sbin/nginx 也显示有GeoIP,但是设置了伪静态后,依然可以用中国IP直接访问

    yuuui5年前 (2019-05-16) Google Chrome 74.0.3729.131 Google Chrome 74.0.3729.131 Windows 10 x64 Edition Windows 10 x64 Edition回复
    • 伪静态问题,或者可能ip库里面的ip没更新。。

      LALA5年前 (2019-05-17) Google Chrome 72.0.3626.121 Google Chrome 72.0.3626.121 Windows 10 x64 Edition Windows 10 x64 Edition回复
      • 已解决,是套了CF无法获取访客真实IP导致
        另外请教大佬,能不能设置返回一个指定的html页面,而不是502?

        yuuui5年前 (2019-05-17) Google Chrome 74.0.3729.131 Google Chrome 74.0.3729.131 Windows 10 x64 Edition Windows 10 x64 Edition回复
        • 写个nginx规则应该可以实现。

          LALA5年前 (2019-05-18) Google Chrome 73.0.3683.103 Google Chrome 73.0.3683.103 Windows 10 x64 Edition Windows 10 x64 Edition回复

分享创造快乐

广告合作资源投稿