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