简体   繁体   中英

How do i use Apache for Upload and Nginx for Secure Download

I have a website, for file sharing which is in perl, now there is file index.dl which is used for download request. In apache it creates a very high load, the load goes almost at 100.

Now i can not use nginx proxy.

I want to set apache to handle all the uploads, and nginx to handle the download requests.

I have checked there is a module "http-secure-link-module" so i have installed that as well.

Now our site generates the file download link like

xyz.com/d/$hash/filename.xyz

now this is the code that generates the link

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($_);
}

and i have setup my nginx config as

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

So now the problem is, when i tried to visit any link, it gives 404 error. how do i get file download working?

this directive secure_link_secret , has been deprecated as of nginx 0.8.50, see: http://wiki.nginx.org/HttpSecureLinkModule

I made a code using the correct directive:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM