nginx防火墙与cloudflare

我想防止在我的网站hotlingking。

不过,我也有cloudflare服务我的网站。 我不想使用cloudflare盗链保护function,因为它不能排除我允许图像链接的外部域(以及将图像redirect到热链接拒绝的图像)。 因此,我想防止在我的服务器级别的盗链。

问题是,当我在我的服务器启用它,cloudflare将redirect到热链接拒绝的图像,无论什么是域,其中包括我自己的域。 这是我使用的代码:

location ~* \.(gif|png|jpe?g)$ { # prevent hotlink valid_referers none blocked ~.google. ~.bing. ~.yahoo. allowed-domain.com, server_names ~($host); if ($invalid_referer) { rewrite (.*) /static/images/hotlink-denied.jpg redirect; # drop the 'redirect' flag for redirect without URL change (internal rewrite) } } location = /static/images/hotlink-denied.jpg { } 

有没有办法绕过图像上的云雾?

图像是您想要通过CDN提供的关键静态资源。 这可以减less您的服务器负载,并使您的网站加载速度更快的访问者。 这可能不适合一些使用情况。

我使用CloudFlare进行热链接保护。 像你一样,当我closures它时,Nginx的热链接保护不起作用。 我的configuration看起来和你的非常相似。

如果要启用盗链,启用盗链的最佳方法是遵循CloudFlare指令 。

您目前可以使用URL中的“hotlink-ok”参数来指定某些链接正常的URL。 因此,例如,您可以设置一个名为“hotlink-ok”的目录,并将要链接的图像放入其中。 就像是:

http://www.yoursite.com/images/hotlink-ok/someimage.jpg

选项二

下一个选项是创build一个子域,将其托pipe在CloudFlare下,但closures热链接保护。 目前,这是在控制面板最右侧的“内容保护”选项卡中完成的。

你有两个子选项

  • 使用CloudFlare CDN托pipe和提供图像(橙色云)。 这会减less您的服务器负载和带宽,但使图像可供任何人使用。
  • closurescaching(灰色云),所以CloudFlare只是作为一个DNS服务器。 像你现在这样使用Nginx进行热链接保护。

我的Nginxconfiguration

这里是我的Nginxconfiguration,就像你的一样,不能通过CloudFlare工作。

 # Cache images on the client. Don't log errors or access. Block hotlinking. location ~* \.(jpg|jpeg|png|gif|css|js|ico|woff|woff2)$ { valid_referers none blocked server_names ~($host) ~(googleusercontent|google|bing|yahoo); if ($invalid_referer) { rewrite (.*) /stop-stealing-images.png redirect; # drop the 'redirect' flag for redirect without URL change (internal rewrite) } # Set up caching - 8 days for static resources. Remove the old unnecessary Pragma and hide the server version add_header Cache-Control "public, max-age=691200, s-maxage=691200"; more_clear_headers Server; more_clear_headers "Pragma"; more_clear_headers "Expires"; } # This allows the "stop stealing images" image to be hotlinked location = /stop-stealing-images.png { } 

检查日志中的cloudflare引用string,包括你在那里。 如果不存在,您只需将其包含在有效的查阅器行中即可正常工作。 🙂