nginx防盗链配置
方法1:直接配置nginx.conf
location /img/ {
root /data/;
valid_referers none blocked server_names *.sohu.com sohu.com ;
if ($invalid_referer) {
rewrite ^/ http://www.sohu.com/images/error.gif
#return 403;
}
}
方法2:利用 nginx 的第三方模块 ngx_http_accesskey_module 来实现下载文件的防盗链
location /img/ {
root /data/;
valid_referers none blocked server_names *.sohu.com sohu.com ;
if ($invalid_referer) {
rewrite ^/ http://www.sohu.com/images/error.gif
#return 403;
}
}
方法2:利用 nginx 的第三方模块 ngx_http_accesskey_module 来实现下载文件的防盗链
description:
比如我的 download 目录下有一个 file.zip 的文件。对应的URI 是http://example.com/download/file.zip
使用ngx_http_accesskey_module 模块后http://example.com/download/file.zip?key=09093abeac094. 只有给定的key值正确了,才能够下载 download 目录下的 file.zip
而且 key 值是根据用户的 Ip 有关的,这样就可以避免被盗链了。
安装模块方法
下载源文件: nginx-accesskey-2.0.3.tar.gz
解压,然后在编译nginx的时候加上:
./configure --addon-module=path/to/nginx-accesskey
配置方法
需要在nginx的配置文件里面添加
location /download {
accesskey on;
accesskey_hashmethod md5;
accesskey_arg "key";
accesskey_signature "mypass$remote_addr";
}
语法:
accesskey [on|off]
默认是off。
用 on 开启这个功能。
accesskey_arg "string"
如果使用 accesskey_arg "string"
accesskey_hashmethod [md5|sha1]
默认是 md5.
这里是选择哈希的类型。随便用哪个都可以。不过在生成下载 uri 的时候需要使用相应的哈希方法。
accesskey_signature "string"
默认是accesskey_signature "$remote_addr"
这里是被签名(哈希)的字符串。
$remote_addr 表示用户的 ip。
为了很好的防盗链,这里应该给用户ip加上噪声。让别人无法猜到。
比如accesskey_signature " myPassWord $remote_addr"
生成下载URI 的方法。
假设
location /download {
accesskey on;
accesskey_hashmethod md5;
accesskey_arg "key";
accesskey_signature "mypass$remote_addr";
}
Php:
http://example.com/download/file.zip?key=<?php echo md5(“mypass “.$_SERVER['REMOTE_ADDR']); ?>
参考文献: http://wiki.codemongers.com/NginxHttpAccessKeyModule
上一篇
nginx的下载连接限制
nginx的下载连接限制
下一篇
国外站长论坛(全英文)
国外站长论坛(全英文)
发表评论:
评论:
2019-07-09 21:02
2010-02-21 22:30
2012-01-08 16:32
2009-11-13 20:59
谢谢!
2008-12-04 16:00
-HTTP_MODULES="$HTTP_MODULES $HTTP_ACCESSKEY_MODULE"
+HTTP_MODULES="$HTTP_MODULES ngx_http_accesskey_module"
2. when configure use absolute path, for example:
$ ./configure --with-http_ssl_module --add-module=src/addons/nginx-accesskey-2.0.3
2008-11-27 03:02
2008-09-04 07:32
http://blog.chinaunix.net/u2/69948/showart_1163321.html
2008-08-06 21:41
2008-07-21 04:21
NGINX不难配的,关键是多找找资料,四个字“胆大心细”就OK了
2008-07-15 04:46
2008-07-15 04:45
忘前辈指点!