我正在使用http://nginx.org/en/docs/http/ngx_http_secure_link_module.html Nginx安全链接模块来保护静态文件下载。它适用于静态文件。
但是当我试图用php文件实现它时,它无法正常工作。基本上我通过ajax请求使用它,如
http://www.example.com/dev/serve.php?h=hash&t=timestamp
当我在下方查看时,它没有任何限制,但它没有哈希和时间戳:
http://www.example.com/dev/serve.php
对于静态文件,它正常工作,即在访问没有散列和时间戳的任何图像时,它返回指定的错误请求响应:
http://www.example.com/dev/image.png
服务器配置:
#
# The default server
#
server {
listen 80;
server_name www.example.com;
location / {
root /box;
index index.php index.html index.htm;
}
error_page 404 /404.html;
location = /404.html {
root /box;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
root /box;
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location /dev/ {
root /box;
secure_link $arg_h,$arg_s;
secure_link_md5 "secret$uri$secure_link_expires$remote_addr";
if ($secure_link = "") {
return 403;
}
if ($secure_link = "0") {
return 410;
}
}
}
我的问题:
答案 0 :(得分:1)
过了一会儿,我找到了解决方案:
#
# The default server
#
server {
listen 80;
server_name www.example.com;
location / {
root /box;
index index.php index.html index.htm;
}
error_page 404 /404.html;
location = /404.html {
root /box;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
root /box;
secure_link $arg_h,$arg_s;
secure_link_md5 "secret$uri$secure_link_expires$remote_addr";
if ($secure_link = "") {
return 403;
}
if ($secure_link = "0") {
return 410;
}
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
请注意,该解决方案适用于php文件,几乎没有修改,它也适用于其他文件。