参考资料

  1. 如何设置防爬虫
  2. 如何设置HTTP响应头进行防爬虫?
  3. 如何设置动态Token头进行防爬虫?
  4. Clear-Site-Data如何中断爬虫会话?
  5. 有哪些常见的HTTP响应头误区
  6. 防爬虫策略构
  7. 有哪些新的HTTP响应头用于防爬?
  8. 拦截搜索引擎爬虫

列举的这些HTTP响应头中,哪些最有效?

有效的HTTP响应头及其操作示例:

  1. Content-Security-Policy

    • 防止XSS攻击

    • 示例:  

      Content-Security-Policy: default-src 'self'
  2. X-Content-Type-Options

    • 阻止MIME类型嗅探

    • 示例:  

      X-Content-Type-Options: nosniff
  3. X-Frame-Options

    • 防止点击劫持

    • 示例:  

      X-Frame-Options: DENY
  4. Strict-Transport-Security (HSTS)

    • 强制HTTPS

    • 示例:  

      Strict-Transport-Security: max-age=31536000; includeSubDomains
  5. X-XSS-Protection

    • 启用浏览器XSS过滤器

    • 示例:  

      X-XSS-Protection: 1; mode=block
  6. Referrer-Policy

    • 控制Referer信息

    • 示例:  

      Referrer-Policy: no-referrer
  7. Feature-Policy

    • 限制浏览器功能

    • 示例:  

      Feature-Policy: geolocation 'none'

操作示例(Nginx配置):

add_header Content-Security-Policy "default-src 'self'";
add_header X-Content-Type-Options "nosniff";
add_header X-Frame-Options "DENY";
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains";
add_header X-XSS-Protection "1; mode=block";
add_header Referrer-Policy "no-referrer";
add_header Feature-Policy "geolocation 'none'";