服务器端(PHP 示例)
<?php
$file = '/path/to/example.pdf';
if (file_exists($file)) {
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
} else {
header('HTTP/1.1 404 Not Found');
echo 'File not found';
}
?>
Nginx 配置优化
server {
location /downloads/ {
# 禁止缓存
add_header Cache-Control "no-cache, no-store, must-revalidate";
add_header Pragma "no-cache";
add_header Expires 0;
# 支持大文件下载
client_max_body_size 1024M;
sendfile on;
tcp_nopush on;
}
}