使用场景:某网站听过URI引用你的页面;当用户在网站点击url时;http头部会经过referer头部,将该网站当前页面的url带上,告诉服务本次请求是由这个页面发起的html
思路:经过referer模块,用invalid_referer变量根据配置判断referer头部是否合法。python
目的:拒绝非正常网站访问咱们站点资源nginx
默认:referer模块默认编译进nginx正则表达式
指令介绍算法
Syntax: valid_referers none | blocked | server_names | string ...; #指定的域名地址 Default: — Context: server, location Syntax: referer_hash_bucket_size size; #希到内存里。内存的大写 Default: referer_hash_bucket_size 64; Context: server, location Syntax: referer_hash_max_size size; Default: referer_hash_max_size 2048; Context: server, location
valid_referers:参数express
server { server_name refere.com; access_log logs/refere.log main; location /{ valid_referers none blocked server_name *.taohui.pub www.taohui.org.cn/nginx/ ~\.google\.; if ($invalid_referer) { return 403; } return 200 "tars\n"; } }
测试安全
[root@python vhast]# curl -H 'referer: http://refere.com.cn/ttt' refere.com <html> <head><title>403 Forbidden</title></head> <body> <center><h1>403 Forbidden</h1></center> <hr><center>nginx/1.15.9</center> </body> </html> [root@python vhast]# curl -H 'referer: http://www.taohui.pub/ttt' refere.com tars [root@python vhast]# curl -H 'referer: ' refere.com tars [root@python vhast]# curl -H '' refere.com tars [root@python vhast]# curl -H 'referer: http://www.taohui.tech' refere.com <html> <head><title>403 Forbidden</title></head> <body> <center><h1>403 Forbidden</h1></center> <hr><center>nginx/1.15.9</center> </body> </html> [root@python vhast]# curl -H 'referer: http://referer.taohui.tech' refere.com <html> <head><title>403 Forbidden</title></head> <body> <center><h1>403 Forbidden</h1></center> <hr><center>nginx/1.15.9</center> </body> </html> [root@python vhast]# curl -H 'referer: http://image.baidu.com/search/detail' refere.com <html> <head><title>403 Forbidden</title></head> <body> <center><h1>403 Forbidden</h1></center> <hr><center>nginx/1.15.9</center> </body> </html> [root@python vhast]# curl -H 'referer: http://image.google.com/search/detail' refere.com tars
Syntax: secure_link expression; #值为空,不经过 为0 为过时 为1 经过 Default: — Context: http, server, location Syntax: secure_link_md5 expression; #怎么构造原始字符串 Default: — Context: http, server, location Syntax: secure_link_secret word; Default: — Context: location