array(2) { ["docs"]=> array(0) { } ["count"]=> int(0) } 111string(0) "" int(1) int(10) int(70) int(8640000) string(13) "likecs_art_db" array(1) { ["query"]=> array(1) { ["match_all"]=> object(stdClass)#28 (0) { } } } array(1) { ["createtime.keyword"]=> array(1) { ["order"]=> string(4) "desc" } } int(10) int(0) int(8640000) array(2) { ["docs"]=> array(0) { } ["count"]=> int(0) } nginx location 限制ip或ip段访问 - 爱码网
rutor

指定目录的IP访问限制

实现重点

正则表达式中()和|的使用,()代表一个原则,|代表或
nginx的location匹配规则中,有一条按照文件顺序进行正则匹配(ps:可以把需要匹配的目录放置在server模块开始的位置)
allow和deny的使用


实现的nginx配置文件

#指定目录实行白名单访问机制
location ~ ^/(test1|test2)/ {
allow 192.168.1.101;
deny all;

root /srv/;
fastcgi_param HTTPS on;
include /etc/nginx/fastcgi_params;
fastcgi_pass php5_fpm;
}

# proxy the PHP scripts to fpm
location ~ \.php$ {
root /srv/;
fastcgi_param HTTPS on;
include /etc/nginx/fastcgi_params;
fastcgi_pass php5_fpm;
}

注意事项:
  1. deny 一定要加一个ip,否则直接跳转到403,不往下执行了;如果403默认页是同一域名下,会造成死循环访问;
  2. allow的ip段
  从允许访问的段位从小到大排列,如127.0.0.0/24 下面才能是10.10.0.0/16
  24表示子网掩码:255.255.255.0
  16表示子网掩码:255.255.0.0
  8表示子网掩码:255.0.0.0
  3. deny all;结尾 表示除了上面allow的其他都禁止
如:

复制代码 代码如下:
deny 192.168.1.1;
allow 127.0.0.0/24;
allo w 192.168.0.0/16;
allow 10.10.0.0/16;
deny all;

分类:

技术点:

相关文章: