nginx安全下载模块无法正常工作

时间:2016-06-21 16:31:18

标签: php nginx

我尝试实施此模块以保护文件下载免受热链接影响并限制带宽窃取:ngx_http_secure_link_module

我收到403错误。我认为错误是PHP代码中的某个地方。

/etc/nginx/conf.d/default.conf

<?php
        $my_con=mysql_connect('localhost','root','');
        mysql_select_db('test');

        $getid = $_GET('id');

        if(isset($getid)){
            $id = $getid;
            $db_query = mysql_query("select * from 'image' where id='$id'");
            while($row = mysql_fetch_row($db_query)){
                $imageData = $row['image'];
            }
            header("content-type: image");
        }
        else{
            echo "Error!";  
        }
    ?>

PHP

location /downloads/ {
    secure_link $arg_md5,$arg_expires;
    secure_link_md5 "$secure_link_expires$uri$remote_addr secret";

    if ($secure_link = "") {
        return 403;
    }

    if ($secure_link = "0") {
        return 410;
    }
}

1 个答案:

答案 0 :(得分:0)

以下步骤错误

  1. 你不必把它放在引号中。这不是PHP。行情是 这里不需要。
  2. $ remote_addr和secret之间有一个空格。     要么删除这个空间。或者在PHP代码中添加空格。
  3. 在Nginx配置中使用参数($ arg_expires)不是 $ secure_link_expires
  4. 更改您的下载链接以包含变量中的文件名和建议请在所有参数之前放置您的秘密
  5. <强> PHP

    $md5 = base64url_encode(md5( "secret" . $expires . $file . $_SERVER['REMOTE_ADDR'] ));
    $download_link = "http://example.com".$file."?md5=".$md5."&expires=".$expires;
    

    nginx config

    location /downloads/ {
        secure_link $arg_md5,$arg_expires;
        secure_link_md5 secret$arg_expires$uri$remote_addr;
    
        if ($secure_link = "") {
            return 403;
        }
    
        if ($secure_link = "0") {
            return 410;
        }
    }
    

    请看我的问题。我在模块中有一个比这更大的问题。这在节点js中适用于我。我用PHP测试了我的代码。我还没有测试过你的。但我想它会起作用;)

相关问题