nginx拒绝国外IP访问方法很多,比如iptables,geoip模块,域名解析等等。这些方法不会相互冲突,可以结合起来一起使用。

今天来教大家利用两个小方法解决  域名解析禁止掉海外IP访问网站。

域名解析方法:

绝大多数域名解析服务商都是提供电信联通移动海外线路区分解析的,所以我们可以充分利用这个功能,来禁止海外访问。

以阿里云DNS解析为例:

       设置A记录类型

       解析线路:境外

       记录值:127.0.0.1

设置后等30分钟后我们再用ping工具测试下境外解析,就会发现所有的海外线路都会解析至127.0.0.1这个IP上,为什么是127.0.0.1呢?因为这个是本地IP,如果有攻击海外肉鸡攻击这个网站,就会自己攻击自己。

结合我写的另外一个方法:

用脚本每周更新国外IP库,利用nginx deny功能直接拒绝这些IP地址。

#添加到crontab
0 0 * * 5 /bin/bash /root/tools/black_nginx.sh

代码内容

#!/bin/bash

rm -f legacy-apnic-latest black_`date +%F`.conf && wget http://ftp.apnic.net/apnic/stats/apnic/legacy-apnic-latest awk -F '|' '{if(NR>2)printf("%s %s/%d%s\n","deny",$4,24,";")}' legacy-apnic-latest > black_`date +%F`.conf && rm -f /usr/local/nginx/conf/black.conf && ln -s $PWD/black_`date +%F`.conf /usr/local/nginx/conf/black.conf && /etc/init.d/nginx reload

在nginx主配置文件的http段include black.conf; 这样此服务器所有网站都拒绝这些IP

经过学习更新一个方法,使用openresty:

官方下载地址:http://openresty.org/cn/download.html

推荐几个waf模块  https://github.com/unixhot/waf

https://github.com/loveshell/ngx_lua_waf

下载完waf模块,把waf文件夹移动到/usr/local/openresty/nginx/conf/

git clone https://github.com/unixhot/waf.git
cp -a ./waf/waf /usr/local/openresty/nginx/conf/ 或者 cd /usr/local/openresty/server/nginx/conf
git clone https://github.com/loveshell/ngx_lua_waf.git
mv ngx_lua_waf waf

在主配置文件夹内引入

vim /usr/local/openresty/nginx/conf/nginx.conf
...
http {
lua_shared_dict limit 10m;
lua_package_path "/usr/local/openresty/nginx/conf/waf/?.lua";
init_by_lua_file "/usr/local/openresty/nginx/conf/waf/init.lua";
access_by_lua_file "/usr/local/openresty/nginx/conf/waf/access.lua";
...
}

启动报错

nginx: [error] lua_load_resty_core failed to load the resty.core module from https://github.com/openresty/lua-resty-core; ensure you are using an OpenResty release from https://openresty.org/en/download.html (rc: 2, reason: module 'resty.core' not found:
no field package.preload['resty.core']
no file '/usr/local/openresty/nginx/conf/waf/resty/core.lua'
no file '/usr/local/openresty/site/lualib/resty/core.so'
no file '/usr/local/openresty/lualib/resty/core.so'
no file './resty/core.so'
no file '/usr/local/lib/lua/5.1/resty/core.so'
no file '/usr/local/openresty/luajit/lib/lua/5.1/resty/core.so'
no file '/usr/local/lib/lua/5.1/loadall.so'
no file '/usr/local/openresty/site/lualib/resty.so'
no file '/usr/local/openresty/lualib/resty.so'
no file './resty.so'
no file '/usr/local/lib/lua/5.1/resty.so'
no file '/usr/local/openresty/luajit/lib/lua/5.1/resty.so'
no file '/usr/local/lib/lua/5.1/loadall.so') 或者 failed to load the 'resty.core' module (https://github.com/openresty/lua-resty-core); ensure you are using an OpenResty release from https://openresty.org/en/download.html (reason: module 'resty.core' not found:
no field package.preload['resty.core']
no file '/usr/local/openresty/nginx/conf/waf/resty/core.lua'
no file '/usr/local/openresty/site/lualib/resty/core.so'
no file '/usr/local/openresty/lualib/resty/core.so'
no file './resty/core.so'
no file '/usr/local/lib/lua/5.1/resty/core.so'
no file '/usr/local/openresty/luajit/lib/lua/5.1/resty/core.so'
no file '/usr/local/lib/lua/5.1/loadall.so'
no file '/usr/local/openresty/site/lualib/resty.so'
no file '/usr/local/openresty/lualib/resty.so'
no file './resty.so'
no file '/usr/local/lib/lua/5.1/resty.so'
no file '/usr/local/openresty/luajit/lib/lua/5.1/resty.so'
no file '/usr/local/lib/lua/5.1/loadall.so') in /usr/local/openresty/nginx/conf/nginx.conf:130

  解决办法

ln -s /usr/local/openresty/server/lualib /usr/local/lib/lua
ln -s /usr/local/openresty/server/lualib/resty /usr/local/openresty/server/nginx/conf/waf/resty

https://www.cnblogs.com/cheyunhua/p/13395745.html

https://www.jianshu.com/p/bffbd9bc4c53

nginx拒绝国外IP访问的更多相关文章

  1. Nginx 拒绝指定IP访问

    来源 : http://www.ttlsa.com/nginx/nginx-deny-ip-access/   闲来无事,登陆服务器,发现有个IP不断的猜测路径.试图往服务器上传文件(木马).于是查看 ...

  2. nginx 拒绝本地ip访问

    server { listen default_server; server_name _; server_name localhost; ; }

  3. 【转载】 禁止国外IP访问你的网站

    在网站的运维过程中,我们通过网站记录的IP列表记录有时候会发现很多国外的IP的访问,如美国的IP等,而很多的服务器攻击行为的发起点很有可能在国外,此时为了服务器安全的考虑,我们可以考虑禁止国外IP访问 ...

  4. 使用 Nginx 阻止恶意 IP 访问

    找到具有明显特征的访问记录,比如: /Dec/::: +] "-" "Ouija_x.86/2.0" "-" 也许是某个开源框架的漏洞,执行 ...

  5. nginx 禁止某IP访问

    首先建立下面的配置文件放在nginx的conf目录下面,命名为blocksip.conf: deny 95.105.25.181; 保存一下. 在nginx的配置文件nginx.conf中加入:inc ...

  6. Nginx禁止使用ip访问,只允许使用域名访问

    Nginx虚拟主机配置,vhosts下面有很多域名的配置: [root@external-lb01 vhosts]# pwd/data/nginx/conf/vhosts [root@external ...

  7. Nginx禁止使用IP访问

    在nginx的访问日志中,会出现只显示IP,而不出现域名的情况,在经过尝试之后,是因为没有设置禁止IP访问导致的. 下面就是在配置文件中设置禁止IP访问,来实现日志文件中$host显示域名. vim ...

  8. Nginx 如何限定IP访问

    在nginx.conf中的server限制段中.deny IP.表示需要限制该IP不可访问.allow IP表示权该IP可以访问. 如上图.表示阻止192.168.1.122的IP的访问.那当然也可以 ...

  9. nginx限制单个IP访问配置

    最近公司做了一个砸金蛋的活动,经过几天的发酵宣传后,每天以几万的的用户数在增长,后面才发现原来有人专门为此开发了一个全自动注册的软件 一时间网站被刷得打开异常缓慢,查看日志发现大部分都是用软件在刷,于 ...

随机推荐

  1. Redis内存分析工具之redis-rdb-tools的安装与使用

    操作系统:Centos7    1.redis-rdb-tools工具是用python语言编写的,所以首先需要安装python: 安装: (1)用 wget 下载 python 2.7 并解压( 如果 ...

  2. Android官方文档翻译 九 2.2Adding Action Buttons

    Adding Action Buttons 增加动作按钮 This lesson teaches you to 这节课教给你 Specify the Actions in XML 在XML中指定动作 ...

  3. (转)Make 命令

    Make 命令 代码变成可执行文件,叫做编译(compile):先编译这个,还是先编译那个(即编译的安排),叫做构建(build). Make是最常用的构建工具,诞生于1977年,主要用于C语言的项目 ...

  4. Cplex用法

    Cplex用法 1.将问题转化为LP问题: cplex -c read mps/nw460.mps change problem type lp opt 2.将问题转化为LP问题并输出问题: cple ...

  5. 【分享数据】vm-insert的压缩比达到29倍

    vm-insert采用remote-write的http协议来接收metric数据,然后按照一定算法转发到vm-storage群集. vm-insert到vm-storage这里是用了自己的二进制协议 ...

  6. vue中$nextTick的使用

    转载 https://www.jb51.net/article/154823.htm  ,写的通俗易懂 在这里我有一个疑问,因为在vue中mounted里面执行后,dom节点是挂载上去了的,所以视图上 ...

  7. gin框架中多种数据格式返回请求结果

    返回四种格式的数据:1. []byte.string  2. json格式  3. html模板渲染  4. 静态资源设置 package main import ( "github.com ...

  8. 集合框架-工具类-Collection-toArray方法

    1 package cn.itcast.p3.toolclass.arrays.demo; 2 3 import java.util.ArrayList; 4 import java.util.Arr ...

  9. WPS修改批注部分的字体颜色?

    今天遇到一个问题,就是复制文档的时候有几块红色字体想改成黑色,怎么也改不成功,通过修改字体颜色无效,通过百度找到了解决方法记录一下. 解决方法 审阅--显示标记--点击插入和删除(去掉前面的对钩即可) ...

  10. ansible roles实践——部署zabbix-agent

    1.tasks/main.yml 2.template/zabbix_agentd.conf.j2 3.[root@master] /etc/ansible$ vim zabbix_agent.yml