zoukankan      html  css  js  c++  java
  • nginx屏蔽某段IP、某个国家的IP

    nginx中可通过写入配置文件的方法来达到一定的过滤IP作用,可使用deny来写。

    deny的使用方法可用于前端服务器无防护设备的时候过滤一些异常IP,过滤的client ip会被禁止再次访问,起到一定的阻断作用。

    但是,在现在这个网络环境非常负载的情况,实际阻断的IP可能都是一些代理模拟出来的,所以遇到大范围异常攻击的时候还是选用前端的防护设备如FW、DDOS等才可进行防护。

    1、首先遍历nginx_access log来查看恶意IP地址:

    awk '{print $1}' /var/log/nginx/access.log |sort |uniq -c|sort -n

    2、在nginx目录 /etc/nginx创建blocksip.conf文件,并写入内容:

    deny 192.168.1.1;                                   #过滤单个IP
    deny 192.168.1.0/24;                                 #过滤整个地址段
    deny all;                                            #过滤所有IP
    allow 192.168.1.1;                                   #与deny all;组合是指除192.168.1.1外其他都过滤

    3、nginx.conf文件将该配置加入http{}标签末尾:

    vim nginx.conf
    http{
    
    
        include blocksip.conf;
    }

    nginx -t

    nginx -s reload

    4、使用deny过的IP访问nginx代理的应用服务,可在error.log中查看forbidden日志

    如果想屏蔽某个地区的 IP 访问的话,用 iptables 把来自某个国家的 IP 重定向到预定页面不是特别灵活的办法,如果只有一个 IP 可用而有多个网站在同一 VPS 上怎么办?用 iptable 屏蔽某个网站的话也会屏蔽同一 VPS 上的其他网站的访问。所以正统的办法还是用 GeoIP 配合对应的 web 服务器模块,比如:apache + mod_geoip 或者 nginx + http_geoip_module 等。

    安装 MaxMind 的 GeoIP 库

    MaxMind 提供了免费的 IP 地域数据库(GeoIP.dat),不过这个数据库文件是二进制的,需要用 GeoIP 库来读取,所以除了要下载 GeoIP.dat 文件外(见下一步),还需要安装能读取这个文件的库。

    wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP.tar.gz
    tar -zxvf GeoIP.tar.gz
    cd GeoIP-1.4.6
    ./configure
    make; make install

     刚才安装的库自动安装到 /usr/local/lib 下,所以这个目录需要加到动态链接配置里面以便运行相关程序的时候能自动绑定到这个 GeoIP 库:

    echo '/usr/local/lib' > /etc/ld.so.conf.d/geoip.conf
    ldconfig

    下载 IP 数据库

    MaxMind 提供了免费的 IP 地域数据库,这个数据库是二进制的,不能用文本编辑器打开,需要上面的 GeoIP 库来读取:

    wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
    gunzip GeoIP.dat.gz

    安装 Nginx

    因为要用到 http_geoip_module 模块,系统自带的 nginx 一般不带这个模块,所以要下载 nginx 源代码后自行编译:

    wget http://nginx.org/download/nginx-0.9.6.tar.gz
    tar zxvf nginx-0.9.6.tar.gz
    cd nginx-0.9.6
    ./configure --without-http_empty_gif_module --with-poll_module 
    --with-http_stub_status_module --with-http_ssl_module 
    --with-http_geoip_module
    make; make install

    配置 Nginx

    最后是配置 nginx,在相关地方加上如下的配置就可以了:

    # vi /etc/nginx/nginx.conf
    
    http {
    ...
    geoip_country /home/vpsee/GeoIP.dat;
    fastcgi_param GEOIP_COUNTRY_CODE $geoip_country_code;
    fastcgi_param GEOIP_COUNTRY_CODE3 $geoip_country_code3;
    fastcgi_param GEOIP_COUNTRY_NAME $geoip_country_name;
    ...
    }
    
    server {
    ...
            location / {
                root   /home/vpsee/www;
                if ($geoip_country_code = CN) {
                    root /home/vpsee/cn;
                }
                ...
            }
    ...
    }

    这样,当来自中国的 IP 访问网站后就自动访问到预定的 /home/vpsee/cn 页面。关于 Nginx + GeoIP 还有很多有用的用法,比如做个简单的 CDN,来自中国的访问自动解析到国内服务器、来自美国的访问自动转向到美国服务器等。MaxMind 还提供了全球各个城市的 IP 信息,还可以下载城市 IP 数据库来针对不同城市做处理。

    参考: http://www.vpsee.com/2011/03/install-nginx-with-geoip-module-for-country-targeting/

        https://www.jb51.net/article/86850.htm

  • 相关阅读:
    从零开始写代码AdaBoost算法的python实现
    从零开始写代码 ID3决策树Python
    redis有启动,但是其他主机telnet 不通的问题
    关于js查找和筛选和循环的几种方式(find();findIndex();filter();some();every();forEach;map();for/in)
    linux 设置tomcat 重启清空 catalina.out 断舍离
    旋转数组 断舍离
    nginx 普通用户启动配置 && springbootswagger 报错 Unable to infer base url 断舍离
    CentOS yum 直接安装最新的nginx【转】 断舍离
    买卖股票的最佳时机 II 断舍离
    swagger 的 pathmapping 配置的理解 断舍离
  • 原文地址:https://www.cnblogs.com/xiaoyou2018/p/9644845.html
Copyright © 2011-2022 走看看