提高php下载速度的nginx模块XSendfile配置及实例

作者:袖梨 2022-06-30

nginx配置文件

server {
listen 80;
...................
# 这个是定义读取你的文件的目录的url开头
location /files {
internal;
alias /home/lvtao.net/upload;
}
....................
}

写个简单的演示

代码如下 复制代码
//down.php?filename=lvtao.jpg

// 获取文件名
$filename = $_GET["filename"];

header ( "Content-type: application/octet-stream" );
$ua = $_SERVER ["HTTP_USER_AGENT"];
if (preg_match ( "/MSIE/", $ua )) {
$encoded_filename = rawurlencode ( $filename );
header ( 'Content-Disposition: attachment; filename="' . $encoded_filename . '"' );
} else if (preg_match ( "/Firefox/", $ua )) {
header ( "Content-Disposition: attachment; filename*="utf8''" . $filename . '"' );
} else {
header ( 'Content-Disposition: attachment; filename="' . $filename . '"' );
}

// "files"是和nginx配置文件的 files要一致
header("X-Accel-Redirect: /files/" . $filename);

相关文章

精彩推荐