X-Send File и NGINX

sanu0074

Новичок
Есть модуль апача - X-Send File с помощью которого скачиваются файлы с сайта:
PHP:
header("X-Sendfile: ".realpath($route));
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$name."\"");
Но когда поставил NGINX, скачивание работать перестало.
Когда я иду по http://site.com/download/?id=95, пишет что не 80 порту: The requested URL /index.php/download/ was not found on this server.
Что нужно подправить?
 

sanu0074

Новичок
Сделал так:
Код:
header("X-Accel-Redirect: ".realpath($route));
            header("Content-Type: application/x-force-download");
            header("Content-Disposition: attachment; filename=\"".$name."\"");
И добавил в siteavialable файл
Код:
    location /download {
        root /var/site/www/_files;
        internal;
    }
И пишет теперь
404 Not Found
nginx/1.4.6 (Ubuntu)
В папке /_files есть еще под-папки, из-за этого может быть?
 

fixxxer

К.О.
Партнер клуба
location /download {
root /var/site/www/_files;
Это подразумевает, что файлы лежат в /var/site/www/_files/download.
Если "подписывание" location-а не нужно, используй вместо root alias.
 

sanu0074

Новичок
Это подразумевает, что файлы лежат в /var/site/www/_files/download.
Если "подписывание" location-а не нужно, используй вместо root alias.
я неправильно написал, папка /_files лежит на уровень выше /www, т.е.
тут скрипты:
/site/www/
а тут файлы:
/site/_files/
Как в этом случае должен выглядеть конфиг?
 

fixxxer

К.О.
Партнер клуба
PHP:
location /download/ {
   internal;
   alias /path/to/site/_files/;
}

...

X-Accel-Redirect: /download/filename.ext
Обрати внимание - в X-Accel-Redirect не нужен путь от корня, как в X-Sendfile. То есть, в X-Sendfile указывается путь на диске, а в X-Accel-Redirect - URL для "внутреннего редиректа" в соответствии с конфигом nginx.
 

sanu0074

Новичок
Результат тот-же
А то что в папке /_files есть еще под-папки, из-за этого может быть?
 

fixxxer

К.О.
Партнер клуба
Что-то неправильно делаешь.

Отлаживай.

Убери временно internal в location /download/, временно вместо отдачи заголовка выводи его на экран, и дергай получившийся URL руками, пока не получится.

Также можно в location /download/ временно написать return 200 $uri - увидишь, какой урл получается.

В моем примере должно быть так:
- файл лежит в /path/to/site/_files/folder/filename.ext
- заголовок выглядит как X-Accel-Redirect: /download/folder/filename.ext
 

fixxxer

К.О.
Партнер клуба
X-Accel-Redirect: /download/_files/content/95/arch.rar
PHP:
location /download/ {
   internal;
   alias /path/to/site/_files/;
}
Заменой префикса /download/ на /path/to/site/_files/ получаем:

/path/to/site/_files/_files/content/95/arch.rar

Что-то явно не так, да?
 

sanu0074

Новичок
Сейчас в конфиге написано так:
location /download/ {
internal;
alias /var/sites/site/_files/;
}
Заголовок такой:
X-Accel-Redirect: /download/content/95/arch.rar
Скрипт такой:
PHP:
            header("X-Accel-Redirect: /download/".$route);
            header("Content-Type: application/x-force-download");
            header("Content-Disposition: attachment; filename=\"".$file."\"");
и всеравно notfound
 

AnrDaemon

Продвинутый новичок
Ты издеваешься?
Какой файл (абсолютный путь) ты пытаешься отдать? Из твоего кода этого не видно.
 

fixxxer

К.О.
Партнер клуба
Заголовок такой:
X-Accel-Redirect: /download/content/95/arch.rar
Проверял непосредственным выводом (echo вместо header)?

Файл /var/sites/site/_files/content/95/arch.rar существует?

nginx reload не забыл? =)

X-A-R точно попадает в тот location? (проверяется как я уже и писал вставкой return в location)
 

sanu0074

Новичок
Вывел все:

X-Accel-Redirect: /download/content/95/f0rest-DWI5kGNNPWaO-file.rar
Content-Type: application/x-force-download
Content-Disposition: attachment; filename="f0rest-DWI5kGNNPWaO-file.rar"
alias /var/sites/site/_files/;
Файл точно есть в папке.
reload не забыл.

Получается, в итоге он будет обращаться по такому пути (как я понимаю download меняется на алиас)
/var/sites/site/_files/content/95/f0rest-DWI5kGNNPWaO-file.rar
Этот путь верный, но все-равно not found.
 

fixxxer

К.О.
Партнер клуба
Значит, либо с путями не так, либо не в тот location попадает - отлаживай расстановкой return-ов
 

sanu0074

Новичок
Думал может быть еррор из-за того что xsenfile у апача включен, отключил - толку ноль.
Есть какой-то способ залогать хотя бы эту ошибку?
 

sanu0074

Новичок
Сделал лог в debug режиме
2015/06/24 19:29:33 [debug] 8340#0: epoll add event: fd:8 op:1 ev:00000001
2015/06/24 19:29:33 [debug] 8340#0: epoll del event: fd:8 op:2 ev:00000000
2015/06/24 19:29:33 [debug] 8341#0: epoll add event: fd:8 op:1 ev:00000001
2015/06/24 19:29:37 [debug] 8341#0: post event 0000000000FE8360
2015/06/24 19:29:37 [debug] 8341#0: delete posted event 0000000000FE8360
2015/06/24 19:29:37 [debug] 8341#0: accept on 0.0.0.0:80, ready: 0
2015/06/24 19:29:37 [debug] 8341#0: posix_memalign: 0000000000F95A40:256 @16
2015/06/24 19:29:37 [debug] 8341#0: *1 accept: 127.0.0.1 fd:13
2015/06/24 19:29:37 [debug] 8341#0: *1 event timer add: 13: 60000:1435163437215
2015/06/24 19:29:37 [debug] 8341#0: *1 reusable connection: 1
2015/06/24 19:29:37 [debug] 8341#0: *1 epoll add event: fd:13 op:1 ev:80000001
2015/06/24 19:29:37 [debug] 8341#0: *1 post event 0000000000FE8430
2015/06/24 19:29:37 [debug] 8341#0: *1 delete posted event 0000000000FE8430
2015/06/24 19:29:37 [debug] 8341#0: *1 http wait request handler
2015/06/24 19:29:37 [debug] 8341#0: *1 posix_memalign: 0000000000FC26B0:256 @16
2015/06/24 19:29:37 [debug] 8341#0: *1 malloc: 0000000000FA34B0:1024
2015/06/24 19:29:37 [debug] 8341#0: *1 recv: fd:13 649 of 1024
2015/06/24 19:29:37 [debug] 8341#0: *1 reusable connection: 0
2015/06/24 19:29:37 [debug] 8341#0: *1 posix_memalign: 0000000000FA38C0:4096 @16
2015/06/24 19:29:37 [debug] 8341#0: *1 http process request line
2015/06/24 19:29:37 [debug] 8341#0: *1 http request line: "GET /download/?id=95 HTTP/1.1"
2015/06/24 19:29:37 [debug] 8341#0: *1 http uri: "/download/"
2015/06/24 19:29:37 [debug] 8341#0: *1 http args: "id=95"
2015/06/24 19:29:37 [debug] 8341#0: *1 http exten: ""
2015/06/24 19:29:37 [debug] 8341#0: *1 http process request header line
2015/06/24 19:29:37 [debug] 8341#0: *1 http header: "Host: site.com"
2015/06/24 19:29:37 [debug] 8341#0: *1 http header: "Connection: keep-alive"
2015/06/24 19:29:37 [debug] 8341#0: *1 http header: "Cache-Control: max-age=0"
2015/06/24 19:29:37 [debug] 8341#0: *1 http header: "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
2015/06/24 19:29:37 [debug] 8341#0: *1 http header: "User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.125 Safari/537.36"
2015/06/24 19:29:37 [debug] 8341#0: *1 http header: "Referer: http://site.com/item/f0rest"
2015/06/24 19:29:37 [debug] 8341#0: *1 http header: "Accept-Encoding: gzip, deflate, sdch"
2015/06/24 19:29:37 [debug] 8341#0: *1 http header: "Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4,uk;q=0.2"
2015/06/24 19:29:37 [debug] 8341#0: *1 http header: "Cookie: PHPSESSID=9emoliiveeln1dh4iaats3u9k0; ID=8189; HASH=%242y%2410%24bvE06NOb.hgQfjwjk2POsuEfmSN9uM5twEBtnkcjPJmO7%2FdSJnbXC; _ga=GA1.2.686341299.1434989893; _ym_visorc_28767336=w"
2015/06/24 19:29:37 [debug] 8341#0: *1 http header done
2015/06/24 19:29:37 [debug] 8341#0: *1 event timer del: 13: 1435163437215
2015/06/24 19:29:37 [debug] 8341#0: *1 generic phase: 0
2015/06/24 19:29:37 [debug] 8341#0: *1 rewrite phase: 1
2015/06/24 19:29:37 [debug] 8341#0: *1 test location: "/"
2015/06/24 19:29:37 [debug] 8341#0: *1 test location: "download"
2015/06/24 19:29:37 [debug] 8341#0: *1 test location: ~ "\.(jpg|jpeg|gif|png|webp|ico|css|bmp|swf|js|html|txt)$"
2015/06/24 19:29:37 [debug] 8341#0: *1 http finalize request: 404, "/download/?id=95" a:1, c:1
2015/06/24 19:29:37 [debug] 8341#0: *1 http special response: 404, "/download/?id=95"
2015/06/24 19:29:37 [debug] 8341#0: *1 http set discard body
2015/06/24 19:29:37 [debug] 8341#0: *1 xslt filter header
2015/06/24 19:29:37 [debug] 8341#0: *1 posix_memalign: 0000000000F99F80:4096 @16
2015/06/24 19:29:37 [debug] 8341#0: *1 HTTP/1.1 404 Not Found
Server: nginx/1.4.6 (Ubuntu)
Date: Wed, 24 Jun 2015 16:29:37 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
Content-Encoding: gzip

2015/06/24 19:29:37 [debug] 8341#0: *1 write new buf t:1 f:0 0000000000F9A0F8, pos 0000000000F9A0F8, size: 194 file: 0, size: 0
2015/06/24 19:29:37 [debug] 8341#0: *1 http write filter: l:0 f:0 s:194
2015/06/24 19:29:37 [debug] 8341#0: *1 http output filter "/download/?id=95"
2015/06/24 19:29:37 [debug] 8341#0: *1 http copy filter: "/download/?id=95"
2015/06/24 19:29:37 [debug] 8341#0: *1 image filter
2015/06/24 19:29:37 [debug] 8341#0: *1 xslt filter body
2015/06/24 19:29:37 [debug] 8341#0: *1 http postpone filter "/download/?id=95" 0000000000FA48A0
2015/06/24 19:29:37 [debug] 8341#0: *1 http gzip filter
2015/06/24 19:29:37 [debug] 8341#0: *1 malloc: 0000000000F9AF90:16384
2015/06/24 19:29:37 [debug] 8341#0: *1 gzip alloc: n:1 s:5936 a:8192 p:0000000000F9AF90
2015/06/24 19:29:37 [debug] 8341#0: *1 gzip alloc: n:1024 s:2 a:2048 p:0000000000F9CF90
2015/06/24 19:29:37 [debug] 8341#0: *1 gzip alloc: n:1024 s:2 a:2048 p:0000000000F9D790
2015/06/24 19:29:37 [debug] 8341#0: *1 gzip alloc: n:1024 s:2 a:2048 p:0000000000F9DF90
2015/06/24 19:29:37 [debug] 8341#0: *1 gzip alloc: n:512 s:4 a:2048 p:0000000000F9E790
2015/06/24 19:29:37 [debug] 8341#0: *1 gzip in: 0000000000F9A338
2015/06/24 19:29:37 [debug] 8341#0: *1 gzip in_buf:0000000000F9A1C0 ni:00000000006C6000 ai:116
2015/06/24 19:29:37 [debug] 8341#0: *1 malloc: 000000000100F380:4096
2015/06/24 19:29:37 [debug] 8341#0: *1 deflate in: ni:00000000006C6000 no:000000000100F380 ai:116 ao:4096 fl:0 redo:0
2015/06/24 19:29:37 [debug] 8341#0: *1 deflate out: ni:00000000006C6074 no:000000000100F380 ai:0 ao:4096 rc:0
2015/06/24 19:29:37 [debug] 8341#0: *1 gzip in_buf:0000000000F9A1C0 pos:00000000006C6000
2015/06/24 19:29:37 [debug] 8341#0: *1 gzip in: 0000000000F9A348
2015/06/24 19:29:37 [debug] 8341#0: *1 gzip in_buf:0000000000F9A210 ni:00000000006C6740 ai:61
2015/06/24 19:29:37 [debug] 8341#0: *1 deflate in: ni:00000000006C6740 no:000000000100F380 ai:61 ao:4096 fl:0 redo:0
2015/06/24 19:29:37 [debug] 8341#0: *1 deflate out: ni:00000000006C677D no:000000000100F380 ai:0 ao:4096 rc:0
2015/06/24 19:29:37 [debug] 8341#0: *1 gzip in_buf:0000000000F9A210 pos:00000000006C6740
2015/06/24 19:29:37 [debug] 8341#0: *1 gzip in: 0000000000F9A358
2015/06/24 19:29:37 [debug] 8341#0: *1 gzip in_buf:0000000000F9A260 ni:00000000006C6560 ai:402
2015/06/24 19:29:37 [debug] 8341#0: *1 deflate in: ni:00000000006C6560 no:000000000100F380 ai:402 ao:4096 fl:4 redo:0
2015/06/24 19:29:37 [debug] 8341#0: *1 deflate out: ni:00000000006C66F2 no:000000000100F433 ai:0 ao:3917 rc:1
2015/06/24 19:29:37 [debug] 8341#0: *1 gzip in_buf:0000000000F9A260 pos:00000000006C6560
2015/06/24 19:29:37 [debug] 8341#0: *1 free: 0000000000F9AF90
2015/06/24 19:29:37 [debug] 8341#0: *1 http chunk: 10
2015/06/24 19:29:37 [debug] 8341#0: *1 http chunk: 187
2015/06/24 19:29:37 [debug] 8341#0: *1 write old buf t:1 f:0 0000000000F9A0F8, pos 0000000000F9A0F8, size: 194 file: 0, size: 0
2015/06/24 19:29:37 [debug] 8341#0: *1 write new buf t:1 f:0 0000000000F9A4B8, pos 0000000000F9A4B8, size: 4 file: 0, size: 0
2015/06/24 19:29:37 [debug] 8341#0: *1 write new buf t:0 f:0 0000000000000000, pos 00000000006C9820, size: 10 file: 0, size: 0
2015/06/24 19:29:37 [debug] 8341#0: *1 write new buf t:1 f:0 000000000100F380, pos 000000000100F380, size: 187 file: 0, size: 0
2015/06/24 19:29:37 [debug] 8341#0: *1 write new buf t:0 f:0 0000000000000000, pos 00000000004A1BD8, size: 7 file: 0, size: 0
2015/06/24 19:29:37 [debug] 8341#0: *1 http write filter: l:1 f:1 s:402
2015/06/24 19:29:37 [debug] 8341#0: *1 http write filter limit 0
2015/06/24 19:29:37 [debug] 8341#0: *1 writev: 402
2015/06/24 19:29:37 [debug] 8341#0: *1 http write filter 0000000000000000
2015/06/24 19:29:37 [debug] 8341#0: *1 http copy filter: 0 "/download/?id=95"
2015/06/24 19:29:37 [debug] 8341#0: *1 http finalize request: 0, "/download/?id=95" a:1, c:1
2015/06/24 19:29:37 [debug] 8341#0: *1 set http keepalive handler
2015/06/24 19:29:37 [debug] 8341#0: *1 http close request
2015/06/24 19:29:37 [debug] 8341#0: *1 http log handler
2015/06/24 19:29:37 [debug] 8341#0: *1 free: 000000000100F380
2015/06/24 19:29:37 [debug] 8341#0: *1 free: 0000000000000000
2015/06/24 19:29:37 [debug] 8341#0: *1 free: 0000000000FA38C0, unused: 0
2015/06/24 19:29:37 [debug] 8341#0: *1 free: 0000000000F99F80, unused: 2307
2015/06/24 19:29:37 [debug] 8341#0: *1 free: 0000000000FA34B0
2015/06/24 19:29:37 [debug] 8341#0: *1 hc free: 0000000000000000 0
2015/06/24 19:29:37 [debug] 8341#0: *1 hc busy: 0000000000000000 0
2015/06/24 19:29:37 [debug] 8341#0: *1 tcp_nodelay
2015/06/24 19:29:37 [debug] 8341#0: *1 reusable connection: 1
2015/06/24 19:29:37 [debug] 8341#0: *1 event timer add: 13: 65000:1435163442215
2015/06/24 19:29:37 [debug] 8341#0: *1 post event 0000000000FE8430
2015/06/24 19:29:37 [debug] 8341#0: *1 delete posted event 0000000000FE8430
2015/06/24 19:29:37 [debug] 8341#0: *1 http keepalive handler
2015/06/24 19:29:37 [debug] 8341#0: *1 malloc: 0000000000FA34B0:1024
2015/06/24 19:29:37 [debug] 8341#0: *1 recv: fd:13 -1 of 1024
2015/06/24 19:29:37 [debug] 8341#0: *1 recv() not ready (11: Resource temporarily unavailable)
2015/06/24 19:29:37 [debug] 8341#0: *1 free: 0000000000FA34B0
2015/06/24 19:29:37 [debug] 8341#0: post event 0000000000FE8360
2015/06/24 19:29:37 [debug] 8341#0: delete posted event 0000000000FE8360
2015/06/24 19:29:37 [debug] 8341#0: accept on 0.0.0.0:80, ready: 0
2015/06/24 19:29:37 [debug] 8341#0: posix_memalign: 0000000000FC27C0:256 @16
2015/06/24 19:29:37 [debug] 8341#0: *2 accept: 127.0.0.1 fd:14
2015/06/24 19:29:37 [debug] 8341#0: *2 event timer add: 14: 60000:1435163437222
2015/06/24 19:29:37 [debug] 8341#0: *2 reusable connection: 1
2015/06/24 19:29:37 [debug] 8341#0: *2 epoll add event: fd:14 op:1 ev:80000001
 
Сверху