Determine whether access is allowed through the referer of the request
The setting method is: under location, set the allowed referer (valid_referers
), judge the current referer, if it is an invalid referer, return 403;
- Example:
fdl.com
The domain name allows access to static resources. If it is not this domain name, 403 will be returned. If it is direct access, it is also allowed.
- Configure nginx
# 防盗链
server {
listen 80;
server_name fdl.com daolian.com;
location / {
root html/static;
index index.html;
}
location ~*/(js|css|img) {
# 静态资源只允许 fdl.com 或者直接访问
valid_referers none fdl.com;
if ($invalid_referer) {
return 403;
}
root html/static;
index index.html;
}
}
- Set the local host
127.0.0.1 fdl.com
127.0.0.1 daolian.com
- Visit sites separately
- fdl.com, successful
- daolian.com static resource access failed
- fdl.com, successful
- Direct access is also OK