Image hotlinking rewrite

Igor Sysoev is at rambler-co.ru
Thu Jun 14 13:05:10 MSD 2007


On Thu, Jun 14, 2007 at 10:53:40AM +0200, Jan ??lusarczyk wrote:

> I'm using nginx to serve static files and proxy the dynamic requests  
> to apache in the backend. It works very good and allowed me to free a  
> lot of memory. Now | need to fight bandwidth staling on one of our  
> servers and I can't solve the problem on my own.
> 
> This is the vhost config:
> 
> server {
>     listen        x.x.x.x:80;
>     server_name  www.servername.com www.otherservername.com;
>     access_log  /var/log/nginx/www.servername.com.access.log  combined;
>     #  vbulletin attachment rewrites
>     location ~* ^.+.(attachments|customavatars|archive) {
>         proxy_pass      http://apache;
>         include         /etc/nginx/proxy.conf;
>     }
>    location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|rar|bz2|doc|xls| 
> exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mov)$ {
>         root  /var/www/hosts/www.servername.com;
>         expires 1d;
>   }
>     location / {
>         proxy_pass      http://apache;
>         include         /etc/nginx/proxy.conf;
>     }
> }
> 
> I need to redirect requests for images from specific hosts (I will  
> compile the names every couple of days manually) to a specific file  
> (ie. do_not_steal_bandwidth.gif).
> Can you help me with this?

If you need to redirect referers from spefic site only, then you may use map:

http {

    map  $referer  $forbidden {

         hostnames;
         default           0;

         *.hotlink1.com    1;
         www.hotlink2.com/ 1;
         .hotlink3.com/    1;

         include   hotlink.conf;
    }

    server {

         set $referer "";
         if ($http_referer ~* '^http://([^:/]+)') {
             set $referer $1;
         }

         location ...

         location ~* ^.+\.(jpg|jpeg|gif ... {

             if ($forbidden) {
                 rewrite  ^  http://...;
             }

             root  ...
             ...
         }



hotlink.conf:

*.hotlink4.com    1;
www.hotlink5.com/ 1;
.hotlink6.com/    1;



-- 
Igor Sysoev
http://sysoev.ru/en/





More information about the nginx mailing list