nginx文件下载验证服务器配置

使用 nginx: X-Accel-Redirect 的特性

nginx.conf

server {
        listen       8080;
        charset utf-8;
        server_name down.lren.org;
        root   D:/download/web;
        #所有文件都通过dl.php 页面处理
        location / {
            rewrite ^/(.*) /dl.php?file=$1 last ;
        }
        #文件真实下载目录,拒绝公网访问
        location /file/ {
            internal;
        }
        #调用php页面验证
        location ~ \.php$ {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            #include fcgi.conf; 
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params; 
        }
        error_page 404 500 502 503 504 /404.html; 
}

dl.php (需要启动php-cgi支持)
启动php-cgi命令,在php/php.ini目录下cmd.exe运行

c:/php/php-cgi.exe -b 127.0.0.1:9000 -c c:/php/php.ini
d:/phpstudy_pro/Extensions/php/php7.3.4nts/php-cgi.exe -b 127.0.0.1:9000 -c d:/phpstudy_pro/Extensions/php/php7.3.4nts/php.ini
<?php
$file=$_GET['file'];
if($_GET['sign'] == '5fdb19c709d67') {
     header( "Content-Type: application/octet-stream");
     header( "X-Accel-Redirect: /file/".$file);
}else{
    die('File Not Found');
}

调用地址 验证参数可自己定义

http://down.lren.org/down/fs2.zip?sign=5fdb19c709d67