Nginx黑名单用于限制指定的IP访问Nginx

1、在nginx.conf的http中加入黑名单配置文件:

include website/blockip.conf;

  • 例(在http下新增以下第4行):
    http {
      include       mime.types;
      default_type  application/octet-stream;
      #新增内容
      include website/blockip.conf;
    

2、然后在conf目录下,创建website/blockip.conf

mkdir /usr/local/nginx/conf/website
touch /usr/local/nginx/conf/website/blockip.conf

3、编辑blockip.conf文件,在blockip.conf中增加需要封禁的ip(将IP更改为需要禁用访问的IP),我的配置如下:

[root@nrffnginx-1 website]# pwd
/usr/local/nginx/conf/website
[root@nrffnginx-1 website]# cat blockip.conf
deny 42.96.128.0/17;
deny 42.120.0.0/16;
deny 42.121.0.0/16;
deny 42.156.128.0/17;
deny 110.75.0.0/16;
deny 110.76.0.0/19;
deny 110.76.32.0/20;
deny 110.76.48.0/20;
deny 110.173.192.0/20;
deny 110.173.208.0/20;
....此处省略

4、最后reload nginx即可生效:

/usr/local/nginx/sbin/nginx -s reload

Copyright © 运维知识库 all right reserved,powered by Gitbook文件修订时间: 2023-09-19 10:45:38

results matching ""

    No results matching ""