how to used xsendfile with nginx?

hi can anyone tell me how to used xsendfile with nginx?

here is my config and code.hope someone can help me. Thank you !!!




# nginx.conf

# ...

sendfile on;

tcp_nopush on;

tcp_nodelay on;

# ...






# vhost.conf

# ...

location /uploads {

    internal;

    alias /data/wwwroot/yii/backend/files;

}

# ...






[root@dev files]# pwd

/data/wwwroot/yii/backend/files

[root@dev files]# ls -l

-rwxrwxrwx 1 www www 5459 1月  26 09:55 1.jpg






// action Get_files

        $requestPath = '1.jpg';

        $realFile = '/data/wwwroot/yii/backend/files/1.jpg';

        return Yii::$app->response->xSendFile(

            $realFile, 

            null, 

            [

                'xHeader' => 'X-Accel-Redirect:/uploads/' . $requestPath . ';'

            ]

        );


// if i used 

        //return Yii::$app->response->xSendFile($realFile);

// 0 byte file download






# access log

10.0.2.2 - - [28/Jan/2016:07:13:21 +0800] "GET /site/get_files HTTP/2.0" 404 329 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.82 Safari/537.36"






# nginx error log

2016/01/28 07:13:21 [error] 7015#0: *1 open() "/data/wwwroot/yii/backend/files/1.jpg;: /data/wwwroot/yii/backend/files/1.jpg" failed (2: No such file or directory), client: 10.0.2.2, server: ez.wsgzc.com, request: "GET /site/get_files HTTP/2.0", upstream: "fastcgi://unix:/dev/shm/php-cgi.sock", host: "admin.yii.com"



and chrome show




ERR_INVALID_RESPONSE






nginx -v

nginx version: nginx/1.9.9


php -v

PHP 7.0.2 (cli) (built: Jan 13 2016 18:05:48) ( NTS )

Copyright (c) 1997-2015 The PHP Group

Zend Engine v3.0.0, Copyright (c) 1998-2015 Zend Technologies

    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies

    with Xdebug v2.4.0RC3, Copyright (c) 2002-2015, by Derick Rethans


php -m

[PHP Modules]

bcmath

Core

ctype

curl

date

dom

exif

fileinfo

filter

ftp

gd

gettext

gmagick

hash

iconv

intl

json

libxml

mbstring

mcrypt

memcache

memcached

mysqli

mysqlnd

openssl

pcntl

pcre

PDO

pdo_mysql

pdo_sqlite

Phar

posix

redis

Reflection

session

shmop

SimpleXML

soap

sockets

SPL

sqlite3

standard

sysvsem

tokenizer

xdebug

xml

xmlreader

xmlrpc

xmlwriter

Zend OPcache

zip

zlib


[Zend Modules]

Xdebug

Zend OPcache



I haven’t tested this on nginx but I noticed above that you set the xHeader to the “/uploads/fileName” thy to use just the fileName without the “uploads” folder because the root and the internal redirects gets concatenated.

fixed

nginx config




location /uploads {

		internal;

                alias /data/wwwroot/yii/backend/files;

	}



controller




return Yii::$app->response->xSendFile(

            $realPath

            , 'youWantShowName.jpg'

            , [

                'mimeType' => $mimeType,

                'inline' => false,

                'xHeader' => "X-Accel-Redirect" // !!!don't set   'xHeader' => 'X-Accel-Redirect:/uploads/' . $requestPath . ';'

            ]

        );



thank you .