我有一个网站,用于perl中的文件共享,现在有文件index.dl用于下载请求。在apache中它会产生非常高的负载,负载几乎达到100。
现在我不能使用nginx代理。
我想设置apache来处理所有上传,并设置nginx来处理下载请求。
我检查过有一个模块" http-secure-link-module"所以我也安装了它。
现在我们的网站生成文件下载链接,如
xyz.com/d/$hash/filename.xyz
现在这是生成链接的代码
sub genDirectLink
{
my ($file,$mode,$mins,$fname)=@_;
require HCE_MD5;
my $hce = HCE_MD5->new($c->{dl_key},"mysecret");
my $usr_id = $ses->getUser ? $ses->getUserId : 0;
my $dx = sprintf("%d",($file->{file_real_id}||$file->{file_id})/$c->{files_per_folder});
my $hash = &encode32( $hce->hce_block_encrypt(pack("SLLSA12ASC4L",
$file->{srv_id},
$file->{file_id},
$usr_id,
$dx,
$file->{file_real},
$mode||'f',
$c->{down_speed},
split(/\./,$ses->getIP),
time+60*$mins)) );
#$file->{file_name}=~s/%/%25/g;
#$file->{srv_htdocs_url}=~s/\/files//;
my ($url) = $file->{srv_htdocs_url}=~/(http:\/\/.+?)\//i;
$fname||=$file->{file_name};
return "$url:182/d/$hash/$fname";
}
sub encode32
{
$_=shift;
my($l,$e);
$_=unpack('B*',$_);
s/(.....)/000$1/g;
$l=length;
if($l & 7)
{
$e=substr($_,$l & ~7);
$_=substr($_,0,$l & ~7);
$_.="000$e" . '0' x (5-length $e);
}
$_=pack('B*', $_);
tr|\0-\37|A-Z2-7|;
lc($_);
}
我已将我的nginx配置设为
server {
listen 182;
server_name myhost.com www.myhost.com;
location / {
root /home/user/domains/hostname.com/public_html/files;
}
location /d/ {
secure_link_secret mysecret;
if ($secure_link = "") {
return 403;
}
rewrite ^ /files/$secure_link;
}
location /files/ {
internal;
}
}
所以现在的问题是,当我试图访问任何链接时,它会给出404错误。 我如何让文件下载工作?
答案 0 :(得分:1)
此指令secure_link_secret
,自nginx 0.8.50起已被弃用,请参阅:http://wiki.nginx.org/HttpSecureLinkModule
我使用正确的指令制作了代码:
server {
listen 182;
server_name example.com;
location / {
root /home/fileshare/domains/fileshare4u.com/public_html/files;
}
location ~^/d/(?P<hash>[^\/]+)/(?P<url>.*)$ {
secure_link $hash,$arg_e;
secure_link_md5 KEY$url$arg_e;
if ($secure_link = "") {
return 403;
}
if ($secure_link = "0") {
return 403;
}
rewrite ^ /files/$url last;
}
location /files/ {
alias /tmp/;
internal;
}
}
perl hash generate:
use Digest::MD5 qw(md5 md5_hex md5_base64);
# http://search.cpan.org/~kazuho/MIME-Base64-URLSafe-0.01/lib/MIME/Base64/URLSafe.pm
use MIME::Base64::URLSafe;
$uri = "sitemap_artistas_1.xml.gz"; # FILE NAME
$key = "KEY"; # KEY
$expire = time() + 30; # 30s
$hash = urlsafe_b64encode(md5("$key$uri$expire"));
$domain = "example.com";
$port = "182";
return "http://$domain:$port/d/$hash/$uri?e=$expire";