Nginx配置sendfile_網(wǎng)絡(luò )編程_奇迪科技(深圳)有限公司(m.cheanjie.com)
功能: 下載 用X-Accel-Redirect把文件委托給Nginx輸出:
主要應用nginx的X-Accel-Redirect將文件直接交給nginx做處理從而減少服務(wù)器負載
網(wǎng)站在服務(wù)器上的根目錄為:/home/www
文件的真實(shí)地址為 http://m.cheanjie.com/down/file/2011/12/31/abcd.zip
下載地址為:http://m.cheanjie.com/down.php?filepath=2011/12/31/abcd.zip&filename=abcd.zip
1、只有通過(guò)php的下載地址能夠下載并且“零內存”消耗。
2、通過(guò)http://m.cheanjie.com/down/file/2011/12/31/abcd.zip 真實(shí)的路徑是不允許下載的。
3、在php里可以進(jìn)行一寫(xiě)下載權限的處理。nginx配置文檔
在配置文件中做以下設定,表示這個(gè)路徑只能在 Nginx 內部訪(fǎng)問(wèn),不能用瀏覽器直接訪(fǎng)問(wèn)防止未授權的下載。
Ngnix配置/usr/local/nginx/conf/nginx.conf
http
{
……
#如果限制并發(fā),添加下面這句:
limit_conn_zone $binary_remote_addr zone=perip:10m;#最大分配10M內存存儲會(huì )話(huà)狀態(tài)
……
}
配置每個(gè)虛擬主機的/usr/local/nginx/conf/vhost/qvdv.net.conf
server
{
……
location down{
internal;
#如果限制并發(fā),添加下面這句:
limit_conn perip 1;#每個(gè)IP的并發(fā)數(一個(gè)客戶(hù)端1個(gè)下載,超出503錯誤,防止多線(xiàn)程下載占用過(guò)多帶寬資源)
#限制速度
limit_rate_after 3m;
limit_rate 512k;#3M后,速度限制為512kb
alias /home/www/down/file/;
}
……
}
列舉php代碼:
//發(fā)送Http Header信息 開(kāi)始下載
//參數:$filename 下載文件路徑
//$showname 下載顯示的文件名
//$expire 下載內容瀏覽器緩存時(shí)間
header("Pragma: public");
header("Cache-control: max-age=".$expire);
//header('Cache-Control: no-store, no-cache, must-revalidate');
header("Expires: " . gmdate("D, d M Y H:i:s",time()+$expire) . "GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s",time()) . "GMT");
header("Content-Disposition: attachment; filename=".$showname);
header("Content-Length: ".$length);
header("Content-type: ".$type);
header('Content-Encoding: none');
header("Content-Transfer-Encoding: binary" );
header("X-Accel-Redirect: down".$fileName);
header("X-Accel-Buffering: yes");
header("Accept-Ranges: bytes");
#header("X-Accel-Limit-Rate :1024"); //速度限制 Byte/s
本文版權所有,轉載須注明:來(lái)源 http://m.cheanjie.com/qvdv-oop-821.html