NG realizes anti-hotlinking

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.comThe 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.
  1. 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;
    }
}
  1. Set the local host
127.0.0.1 fdl.com
127.0.0.1 daolian.com
  1. Visit sites separately
    • fdl.com, successful
      Insert image description here
    • daolian.com static resource access failed
      Insert image description here
  • Direct access is also OK
    Insert image description here

Guess you like

Origin blog.csdn.net/futao__/article/details/128445316