使用場景:某網站聽過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