Py学习  »  NGINX

Nginx 设置黑白名单教程

Java基基 • 2 月前 • 154 次点击  

👉 这是一个或许对你有用的社群

🐱 一对一交流/面试小册/简历优化/求职解惑,欢迎加入芋道快速开发平台知识星球。下面是星球提供的部分资料: 

👉这是一个或许对你有用的开源项目

国产 Star 破 10w+ 的开源项目,前端包括管理后台 + 微信小程序,后端支持单体和微服务架构。

功能涵盖 RBAC 权限、SaaS 多租户、数据权限、商城、支付、工作流、大屏报表、微信公众号、ERPCRM AI 大模型等等功能:

  • Boot 仓库:https://gitee.com/zhijiantianya/ruoyi-vue-pro
  • Cloud 仓库:https://gitee.com/zhijiantianya/yudao-cloud
  • 视频教程:https://doc.iocoder.cn
【国内首批】支持 JDK 21 + SpringBoot 3.2.2、JDK 8 + Spring Boot 2.7.18 双版本 

来源:blog.csdn.net/m0_54563444


allow、deny

deny和allow指令属于ngx_http_access_module,nginx默认加载此模块,所以可直接使用。

在Nginx配置文件中定义允许访问系统的IP地址。假设您的Nginx配置文件位于 /etc/nginx/nginx.conf/etc/nginx/sites-available/default

直接配置文件中添加

server {
    listen 80;
    server_name your_domain_or_ip;
 
    # 设置白名单
    location / {
        allow 192.168.1.1;  # 允许特定IP访问
        allow 192.168.1.2;
        allow 192.168.1.3;
        allow 192.168.1.4;
        allow 192.168.1.5;
        allow 192.168.1.6;
        deny all;  # 拒绝所有其他IP访问
    }
 
    # 设置最高权限的运行维护IP
    location /admin {
        allow 192.168.1.7;  # 最高权限的运行维护IP
        deny all;
    }
 
    # 设置有限权限的维护IP
    location /limited {
        allow 192.168.1.8;  # 有限权限的维护IP1
        allow 192.168.1.9;  # 有限权限的维护IP2
        deny all;
    }
}

通过读取文件IP配置白名单

location /{
    include /home/whitelist.conf;
    #默认位置路径为/etc/nginx/ 下,
    #如直接写include whitelist.conf,则只需要在/etc/nginx目录下创建whitelist.conf
    deny all;
}

/home/目录下创建whitelist.conf,并写入需要加入白名单的IP,添加完成后查看如下:

cat /home/whitelist.conf
 
#白名单IP
allow 10.1.1.10;
allow 10.1.1.11;

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

  • 项目地址:https://github.com/YunaiV/ruoyi-vue-pro
  • 视频教程:https://doc.iocoder.cn/video/

ngx_http_geo_module

默认情况下,一般nginx是有加该模块的,ngx_http_geo_module,参数需设置在位置在http模块中。此模块可设置IP限制,也可设置国家地区限制。位置在server模块外即可。

配置文件直接添加

geo $ip_list {
    default 0;
    #设置默认值为0
    192.168.1.0/24 1;
    10.1.0.0/16    1;
}
server {
    listen       8081;
    server_name  192.168.152.100;
    
    location / {
        root   /var/www/test;
  index  index.html index.htm index.php;
  if ( $ip_list = 0 ) {
  #判断默认值,如果值为0,可访问,这时上面添加的IP为黑名单。
  #白名单,将设置$ip_list = 1,这时上面添加的IP为白名单。
  proxy_pass http://192.168.152.100:8081;
    }
}

读取文件IP配置

geo $ip_list {
    default 0;
    #设置默认值为0
    include ip_white.conf;
}
server {
    listen       8081;
    server_name  192.168.152.100;
    
    location / {
        root   /var/www/test;
  index  index.html index.htm index.php;
  if ( $ip_list = 0 ) {
   return 403;
   #限制的IP返回值为403,也可以设置为503,504其他值。
   #建议设置503,504这样返回的页面不会暴露nginx相关信息,限制的IP看到的信息只显示服务器错误,无法判断真正原因。
      }
    }
}

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

  • 项目地址:https://github.com/YunaiV/yudao-cloud
  • 视频教程:https://doc.iocoder.cn/video/

国家地区IP限制访问

安装ngx_http_geoip_module模块

ngx_http_geoip_module,参数需设置在位置在http模块中。nginx默认情况下不构建此模块,应使用 --with-http_geoip_module 配置参数启用它。对于ubuntu系统来说,直接安装 nginx-extras组件,包括几乎所有的模块。

sudo apt install nginx-extras

对于centos系统,安装模块。

yum install nginx-module-geoip

下载 IP 数据库

ngx_http_geoip_module模块依赖于IP数据库,所有数据在此数据库中读取,需要下载ip库(dat格式)。下载同时包括Ipv4和Ipv6的countrycity版本。

#下载国家IP库,解压并移动到nginx配置文件目录,
sudo wget https://dl.miyuru.lk/geoip/maxmind/country/maxmind.dat.gz
gunzip maxmind.dat.gz
sudo mv maxmind.dat /etc/nginx/GeoCountry.dat
 
sudo wget https://dl.miyuru.lk/geoip/maxmind/city/maxmind.dat.gz
gunzip maxmind.dat.gz
sudo mv maxmind.dat /etc/nginx/GeoCity.dat

配置nginx

geoip_country /etc/nginx/GeoCountry.dat;
geoip_city /etc/nginx/GeoCity.dat;
 
server {
    listen  80;
    server_name 144.11.11.33;
 
    location / {
      root  /var/www/html/;
      index index.html index.htm;
      if ($geoip_country_code = CN) {
     return 403;
   #中国地区,拒绝访问。返回403页面
  }
   }
 }

国家相关参数

  • $geoip_country_code:两位字符的英文国家码。例如:CN, US
  • $geoip_country_code3:三位字符的英文国家码。例如:CHN, USA
  • $geoip_country_name:国家英文全称。例如:China, United States

城市相关参数

  • $geoip_city_country_code:两位字符的英文国家码。例如:CN, US
  • $geoip_city_country_code3:三位字符的英文国家码。例如:CHN, USA
  • $geoip_city_country_name:国家英文全称。例如:China, United States
  • $geoip_region:地区代码,通常是两位数的数字。例如:杭州是02, 上海是23
  • $geoip_city:城市的英文名称。例如:Hangzhou
  • $geoip_postal_code:城市的邮政编码。国内这字段可能为空
  • $geoip_city_continent_code:大洲代码。国内通常是AS
  • $geoip_latitude:纬度
  • $geoip_longitude:经度

欢迎加入我的知识星球,全面提升技术能力。

👉 加入方式,长按”或“扫描”下方二维码噢

星球的内容包括:项目实战、面试招聘、源码解析、学习路线。

文章有帮助的话,在看,转发吧。

谢谢支持哟 (*^__^*)

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/178433
 
154 次点击