---
title: "nginx x-sendfile下载"
author: "crystalrain0"
date: "2014-12-16T09:30:05Z"
tags: []
categories: ["移动开发"]
slug: "nginx-x-sendfile"
draft: false
lastmod: "2014-12-16T09:30:05Z"
id: 2166596
url: "https://crystalrain0.iteye.com/blog/2166596"
---

// 利用nginx直接下载文件，提高效率

    public function download\_apk\_efficient($internal\_file\_path,$file\_name){

        // And redirect user to internal location

        header("Content-Type: application/vnd.android.package-archive");

        header("Content-Disposition: attachment; filename=".$file\_name);

        header("X-Accel-Redirect: " . $internal\_file\_path);

    }

 

 

调用的：

             Module::factory("Download")-&gt;download\_apk\_efficient("/promoter\_files/". $relative\_file\_path, $out\_name); //promoter\_files是在nginx的innernal中配置的路径，例如我下载/server/hosting/mobilechannel/promoter\_download2/12/1.file，这里实际调用的是download\_apk\_efficient("/promoter\_files/12/1.file" ,$out\_name)

 

 

 

在nginx里配置：

        location /promoter\_files/ {   #/promoter\_files/是我取的名字，表示alias字段代表的路径

                internal;

                alias   /server/hosting/mobilechannel/promoter\_download2/;   #这里是文件的实际路径

        }