使用场景:某网站听过URI引用你的页面;当用户在网站点击url时;http头部会通过referer头部,将该网站当前页面的url带上,告诉服务本次请求是由这个页面发起的
思路:通过referer模块,用invalid_referer变量根据配置判断referer头部是否合法。
目的:拒绝非正常网站访问我们站点资源
默认: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:参数
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
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:nginx防盗链处理模块referer和secure_link模块 - Python技术站