Skip to content


避免nginx在cdn下被缓存防盗链图

如果你CDN会缓存http 302状态的话,那么CDN就有可能会缓存防盗图,正常用户访问时会返回CDN中的”提示图”,而非正常图.
这里里解决方法是让rewrite后的302改成403状态.

nginx原配置

  1. location ~* ^.+\.(jpg|jpeg|gif|png|swf|rar|zip|css|js)$ {
  2. valid_referers none blocked *.c1gstudio.com localhost cache.baiducontent.com c.360webcache.com www.sogou.com cc.bingj.com;
  3. if ($invalid_referer) {
  4.     rewrite ^/ http://leech.c1gstudio.com/leech.gif;
  5.     return 412;
  6.     break;
  7. }
  8.                  access_log   off;
  9.                  root /opt/lampp/htdocs/c1gstudio;
  10. expires 3d;
  11. break;
  12.      }

新的nginx配置
做一个代理到虚似主机

  1. upstream leech_server {
  2.      server   192.168.0.75:80;
  3.      }
  4.  
  5.      location @leech {
  6.           proxy_set_header Host  leech.c1gstudio.com;
  7.           proxy_pass http://leech_server;
  8.      }
  9.  
  10.      location ~* ^.+\.(jpg|jpeg|gif|png|swf|rar|zip|css|js)$ {
  11.           valid_referers none blocked *.c1gstudio.com localhost cache.baiducontent.com c.360webcache.com www.sogou.com cc.bingj.com;
  12.           if ($invalid_referer) {
  13.     #rewrite ^/ http://leech.c1gstudio.com/leech.gif;
  14.     error_page 412 = @leech;
  15.     return 412;
  16.     break;
  17.           }
  18.                  access_log   off;
  19.                  root /opt/lampp/htdocs/c1gstudio;
  20. expires 3d;
  21. break;
  22.      }
  23.  
  24.      server
  25.      {
  26.              listen       80;
  27.              server_name  leech.c1gstudio.com;
  28.              index index.html index.htm index.php;
  29.              root  /opt/lampp/htdocs/transfer_url;
  30.              error_page 404 =403 /leech.gif;
  31.              access_log  off;
  32.  
  33.                  location ~* ^.+\.(jpg|jpeg|gif|png)$ {
  34.                  access_log   off;
  35.                  root /opt/lampp/htdocs/transfer_url;
  36.                  add_header Cache-Control no-cache;
  37.                  add_header Pragma no-cache;
  38.                  add_header Expires 0;
  39.                  break;
  40.                  }
  41.             
  42.      location ~/\.ht {
  43.          deny all;
  44.      }
  45.      }

Posted in Nginx.

Tagged with .


No Responses (yet)

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.