文档中心 CDN Pro 边缘访问控制

边缘访问控制

更新时间:2023-06-28 17:20:31

在边缘实施访问控制

访问控制通过对访客身份进行识别和过滤,可以有效屏蔽未经授权用户的访问流量,以保护数据内容免受侵害。同时,访问控制在缓解一些常见的7层攻击方面也发挥着至关重要的作用。CDN Pro 的指令支持配置多种访问控制策略。这些指令大多是我们基于 NGINX 开源版本进行改进增强的。其中,我们还引入了一个专有的 eval_func指令来支持定制算法。以下是一些常见的访问控制策略配置示例。

  • 使用 allow 指令和 deny 指令设置客户端IP黑白名单:
allow 123.0.0.1/8;
allow 234.12.34.56;
deny all;
  • 使用 valid_referers 指令检查 Referer 请求标头的合法性:
valid_referers none blocked server_names
               *.example.com example.* www.example.org/galleries/
               ~\.google\.;
if ($invalid_referer) {
    return 403;
}

  • 使用 if 增强指令检查http请求头或URL query查询参数的合法性:
if ($http_my_token != 'authorized' && $arg_my_token != 'authorized') {
    return 403;
}
  • 使用 auth_request 指令检查远端鉴权服务器的鉴权结果:
location /protected/ {
    auth_request /auth; # 2xx to grant access, 401 or 403 to reject
    ...
}

location = /auth { # calls a remote server to authenticate the request
    origin_pass remote-auth-server;
    proxy_method HEAD; # specify method required by the remote server
    proxy_pass_request_body off; # remove the request body, if any
    origin_set_header Content-Length "" policy=overwrite;
    # forward the original request URI to the remote server
    origin_set_header X-Original-URI $request_uri;
  • 使用 NGINX 内置指令 secure_linksecure_link_md5 实现URL下载防盗链。该模块能够从请求中提取信息,采用定义的加密串信息进行进行MD5 HMAC编码,以校验请求的合法性,同时还可以指定过期时间。只有在验证了 MD5 值合法并且请求未过期,边缘服务器才会让请求通过。详情请参考 NGINX 官方文档
location /s/link {    
 secure_link $arg_md5,$arg_expires;     #这里配置了2个URL问号后参数,一个是arg_md5,一个是arg_expires
 secure_link_md5 "$SECRET(MySecret)$secure_link_expires$uri";     #这里自定义了1个MySecret的加密串
 if ($secure_link = "") { 
 return 403;     #资源不存在或哈希比对失败响应403
 }    
 if ($secure_link = "0") { 
 return 410;   #时间戳过期响应401
 }
}
  • 使用全新指令 eval_func 实现更复杂的加密算法。下面是一个使用SHA256实现HMAC认证码验证的示例:
eval_func $binhash HMAC $secret_key $request_uri SHA256;
eval_func $b64hash BASE64_ENCODE $binhash;
# assume the client passes the hash through the X-Hash header
if ($b64hash != $http_x_hash) {
    return 403;
}
本篇文档内容对您是否有帮助?
有帮助
我要反馈
提交成功!非常感谢您的反馈,我们会继续努力做到更好!