天天看点

nginx 整合 ngx_http_accesskey_module 模块的应用

原发于http://www.linux-ch.com/post-25.html

现有一文件分享系统,利用nginx平台直接打开目录功能,可自由浏览并选择文件进行下载.同时开通ftp,可以上传文件与大家分享.这一切实现起 来并不困难,但在一段时间后发现分享系统的流量较高,后通过分析日志发现竟然是来自各地迅雷的链接.在这之后我曾将迅雷的agent给过滤掉,效果还不 错,但随之而来的问题是:平时我们也在使用迅雷,对于小文件没什么影响,但对于动不动就上G的文件,对于浏览器的下载功能是个严酷的考验. 现在利用ngx_http_accesskey_module模块,并配合php,一个能防止下载工具我链接分享功能的文件分享系统复活了~

    nginx的编译配置请参考http://wiki.nginx.org/NginxHttpAccessKeyModule

    下面分享下关于我的配置:

     nginx:

        location /downloads {

#                autoindex on;

#                autoindex_localtime on;

#                autoindex_exact_size off;

                accesskey             on;

                accesskey_hashmethod  md5;

                accesskey_arg         "key";

                accesskey_signature   "mypass$remote_addr";

        }

php源码:

<?php

$downfile=$_GET["downfile"];

$dir0='/web/html/downloads';

$dir1=str_replace('..','',$_GET['dir1']);

$dir=$dir0.$dir1;

if ($downfile) {

        $filename =basename($downfile);

        $file=fopen($dir."/".$filename,"rb");

        Header("Content-type: application/octet-stream");

        Header("Accept-Ranges: bytes");

        Header("Accept-Length: ".filesize($dir."/".$filenamee));

        Header("Content-Disposition: p_w_upload; filename=".$filename);

        echo fread($file,filesize($dir."/".$filename));

        fclose($file);

        exit;

}

?>

<title>SZA</title>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8";>

<STYLE type="text/css">

body {font-family: "sans-serif", "serif"; font-size: 12px;}

BODY { background-color:#A2B5CD }

a:link {color: #BFEFFF; text-decoration: none}

a:visited {color: #080808; text-decoration: none}

a:hover {color: #FFFFFF; text-decoration: underline}

input {font-family: "sans-serif", "serif";font-size: 12px;}

td {font-family: "sans-serif", "Verdana"; font-size: 12px;}

.title {font-family: "Verdana", "Tahoma";font-size: 20px;font-weight: bold; color=black}

</STYLE>

</head>

<body>

<table width="100%"  cellspacing="1" cellpadding="3">

  <tr>

    <td class="title" align="center">神州IT架构文件分享系统</td>

  </tr>

  <tr><td align="right">上传请转至ftp,帐号:ftp 密码:ftpkey  </td></tr>

</table>

<hr>

$ipkey= md5("mypass".$_SERVER['REMOTE_ADDR']);

        echo '<form action="" method="post">';

        echo '<tr>';

        echo '<td>例如你现在的一个链接是:"https://www.linux-ch.com/downloads/iso/rhel- server-6.0-i386-dvd.iso<font color="red">?key=d85b4aa593bkdkfia450ce6a55766e6b"</font></td>';

        echo '<tr><td>只要将后面的"<font color="red">?key=d85b4aa593bkdkfia450ce6a55766e6b</font>"删除掉,再提 交到下面进行更新>就行了</td></tr>';

        echo '<td>';

        echo "输入需要更新的链接:";

        echo '<input type="text" name="url" style="font-family:Verdana,Arial; font-size: 9pt; width:60%">';

        echo '<input type="submit" value="更新" style="font-family:Verdana,Arial; font-size: 9pt;background-color:#A2B5CD ">';

        echo '</td>';

        echo '</tr>';

        echo '<tr>'; 

        $durl=$_POST['url'];   

        if (!isset($durl) or empty($durl)) {

                echo " <td> </td></tr>\n";

        } else {

                echo "<tr><td>新的下载地址是: </td></tr>\n";

                echo "<tr><td><a href=\"".($durl)."?key=".$ipkey."\">".($durl)."?key=".$ipkey." </a></td></tr>\n";

        echo "</tr>\n";

        echo '</form>';

        echo '<td><b>';

        echo "文件列表:";

        echo '</b></td>';

<table width="100%" border="0" cellpadding="3" cellspacing="1">

$dirs=@opendir($dir);

while ($file=@readdir($dirs)) {

        $b="$dir/$file";

        $a=@is_dir($b);

        if($a=="1"){

                if($file!=".."&&$file!=".")  {

                        echo "<tr>\n";

                        echo "  <td><a href=\"?dir1=".rawurlencode($dir1)."/".rawurlencode($file)."\">$file</a></td>\n";

                        echo "</tr>\n";

                } else {

                        echo "\n";

                }

@closedir($dirs);

        if($a=="0"){

//                $size=@filesize("$dir/$file");

                $size = exec ('stat -c %s '. escapeshellarg ("$dir/$file"));

                if($size >= 1073741824) {

                        $size = round($size / 1073741824 * 100) / 100 . " GB";

                } elseif($size >= 1048576) {

                        $size = round($size / 1048576 * 100) / 100 . " MB";

                } elseif($size >= 1024) {

                        $size = round($size / 1024 * 100) / 100 . " KB";

                        $size = $size . " B";

//                $lastsave=@date("Y-n-d H:i:s",filemtime("$dir/$file"));

                $lastsave=@date("Y-n-d H:i:s",exec('date +%s -r'. escapeshellarg ("$dir/$file")));

        echo "<tr>\n";

        echo "<td>$file</td>\n";

        echo "<td>$lastsave</td>\n";

        echo "<td>$size</td>\n";

        echo "<td><a href=\" https://www.linux-ch.com/downloads".rawurlencode($dir1)." /".rawurlencode($file)."?key=".$ipkey."\">[下载]</a></td> \n";

</body>

</html>

    源码原来是使用urlencode这个参数对链接进行转换的,遇到空格就出问题,后改用rawurlencode将问题解决.

    php的源码是在网上找回来修改加工的,在此我要感谢3t,有他的帮助我才能改好这些文件,当然也要感谢将源码发布出来前辈,这样我才有机会去修改这些,谢谢.