【问题标题】:nginx hotlink special redirectnginx 热链接特殊重定向
【发布时间】:2013-12-09 10:31:25
【问题描述】:

首先,对不起我的英语。

我尝试在我的网站上做一些特殊的热链接阻止。

例如我有图片

http://s1.example.com/i/img_123.jpg 如果有人在您自己的网站上以代码的形式使用此图像,请不要重定向。但是如果他直接访问这个图片,nginx会重定向到 http://example.com/z/img_123.jpg

我怎样才能得到它?

【问题讨论】:

  • 我不明白您为什么要重定向到图像但返回 html。
  • 我喜欢用这张图片+其他内容、广告重定向到 html。等

标签: redirect nginx hotlinking


【解决方案1】:

这应该可以完成工作:

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;
    }
  }
}

编辑 - 我的答案的第二个版本

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;
}

【讨论】:

  • 你好,不是我的意思。它不应该阻止引用者的视图。不过谢谢你的回答。
  • 它不会阻止一切。它允许来自“valid_referers”列表中站点的流量。您可以添加更多域或 epxressions,例如“*.example.com”,以允许任何子域。
【解决方案2】:

我的问题有答案:)

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

【讨论】:

  • 您在这里所做的是将没有“推荐”的客户重定向到其他站点。来自任何其他站点的客户将被允许。我认为您应该使用更具体的内容更新更新 $http_referer。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多