Redirect nginx热链接特殊重定向

Redirect nginx热链接特殊重定向,redirect,nginx,hotlinking,Redirect,Nginx,Hotlinking,首先,对不起我的英语 我尝试在我的网站上做一些特殊的热链接预防 例如,我有图像 如果有人在你自己的网站上以代码的形式使用这个图像,不要重定向。但如果他直接访问此图像,nginx将重定向到 这应该可以做到: server { root /var/www; index index.htm; server_name s1.sitename; location ~ \.(jpe?g|p

首先,对不起我的英语

我尝试在我的网站上做一些特殊的热链接预防

例如,我有图像

如果有人在你自己的网站上以代码的形式使用这个图像,不要重定向。但如果他直接访问此图像,nginx将重定向到
这应该可以做到:

server {                                                 
  root /var/www;

  index index.htm;
  server_name s1.sitename;

  location ~ \.(jpe?g|png|gif)$ {
    valid_referers blocked example.com;
    if ($invalid_referer) { 
      # returning an htm. You can replace this with an image
      return 403 /error.htm;
    }
  }
}
编辑-我答案的第二个版本
我的问题有答案:)


我不明白为什么你想重定向到一个图像,但返回一个html。我想重定向到html与此图像+其他内容,广告等。你好,不是我的意思。它不应该用referer阻止视图。但谢谢你的回答。它并没有阻止一切。它允许来自“有效推荐人”列表上站点的流量。您可以添加更多的域或ePression,如“*.example.com”,以允许任何子域。您在这里所做的是将不带“refereal”的客户端重定向到其他站点。允许来自任何其他站点的客户端。我认为你应该用更具体的东西更新$http\u referer。
location ~ \.(jpe?g|png|gif)$ {
  valid_referers blocked example.com;
  if ($invalid_referer) {
    return 403 /error.htm;
  }

  # If not invalid. we make a URL rewrite
  rewrite i/(.*) http://www.example.com/z/$1 redirect;

  # this is a suggestion. Enable expire. It's static data, and you should not
  # waste resources serving these files every single time.
  expires 30;

  break;
}
if ($http_referer = '') {
    rewrite i/(.*) http://www.example.com/z/$1 redirect;
    expires off;
    break;
}