Monday, 30 June 2014

Nginx no Hotlink







location ~* (\.jpg|\.png|\.gif|\.jpeg)${
if ($http_referer ~ ^(http://target-domain.com)){
rewrite ^/(.*)$ http://4.bp.blogspot.com/-1g-zXY7f3d4/U7DJC82yJyI/AAAAAAAAT_E/xJ8H0NquKrs/s1600/lens13475531_1284701688no-hotlinking.png last;
}
}


อธิบาย code
location ~* (\.jpg|\.png|\.gif|\.jpeg)$ 
เมื่อปลายทางใดๆเรียก url ภาพที่มีนามสกุลตาม .jpg .png .gif .jpeg ให้ไปทำงาน รวมถึง my-domain.com

if ($http_referer ~ ^(http://target-domain.com) ) { }
ตรวจสอบว่า url ปลายทางที่เรียกมาว่าเป็น http://target-domain.com หรือเปล่าถ้าใช่ ให้ส่ง url ของภาพ 

http://4.bp.blogspot.com/-1g-zXY7f3d4/U7DJC82yJyI/AAAAAAAAT_E/xJ8H0NquKrs/s1600/lens13475531_1284701688no-hotlinking.png 

นี้กลับไปแทน


////////////////////////////////////////////////////////////////////////////////////////////////////////////////

http://wiki.nginx.org/IfIsEvil
http://wiki.nginx.org/Pitfalls

/////////////////////////////////////////////////////////////////

Hotlink protection in Nginx

Hotlinked files can be a major cause for bandwidth leeching for some sites. Here’s how you can hotlink protect your images and other file types using a simple location directive in your Nginx configuration file :
location ~ \.(jpe?g|png|gif)$ {
     valid_referers none blocked mysite.com *.mysite.com;
     if ($invalid_referer) {
        return   403;
    }
}
Use the pipe (“|”) to separate file extensions you want to hotlink protect.
The valid_referers directive contains the list of site for whom hotlinking is allowed. Here is an explanation of the parameters for the valid_referers directive :
  • none - Matches the requests with no Referrer header.
  • blocked - Matches the requests with blocked Referrer header.
  • *.mydomain.com - Matches all the sub domains of mydomain.com. Since v0.5.33, * wildcards can be used in the server names.
You can also tweak the location directive for blocking files from a specific directory. Like this :
location /images/ {
     valid_referers none blocked mysite.com *.mysite.com;
     if ($invalid_referer) {
        return   403;
    }
}
http://nginxlibrary.com/hotlink-protection/
Post a Comment

Analytics and Statistic

Blog Archive