Nginx系列-访问控制

297 阅读1分钟

访问控制的任务是保证网络资源不被非法访问

权限控制指令-deny 、allow

指令格式

deny IP/IP段/all
allow IP/IP段/all

规则

同一块下出现多个权限控制指令,先出现的指令设置会覆盖后出现的指令范围,未覆盖的范围依然生效,覆盖的范围以先出现的指令为准

多块下(http/server/location)出现多个权限指令,内层块中权限级别比外层块中设置的权限级别高,注意不是覆盖

即:同一块多指令范围取交集,不同块内层块指令生效

简单示例

location / {
      root   html;
      index  index.html index.htm;
}

访问/,正常显示

添加deny all; --禁止所有访问

location / {
      root   html;
      index  index.html index.htm;
      deny all;
}

访问/,展示被禁止

权限控制指令-location

指令格式

location [=|^~|~|~*] URI {...}

规则

=   精确匹配URI,完全一致才能匹配上

^~ 以指定URI开头的匹配,最大前缀匹配(非正则匹配,最大前缀匹配的一种特殊方式)

~    正则匹配URI,区分大小写

~*   正则匹配URI,不区分大小写

最大前缀匹配,匹配最长的URI对应的location

以上规则优先级依次下降,匹配到了则不再向下匹配

示例

1.root 和 alias区别

location /merch {
    root html;
    index index.html;
}

访问/merch,

将root修改为alias

location /merch {
      alias  html;
      index  index.html index.htm;
}

访问/merch,正常展示

规则如下:

配置为root 则访问/merch 匹配后实际访问的文件地址为 html/merch/index.html

配置为alias 则访问/merch 匹配后实际访问的文件地址为 html/index.html

区别在于location指令后面的URI是否被使用与找实际资源文件

2.待补充