# X-accel-redirect serving html download page instead of file

**URL:** <https://www.ruby-forum.com/t/x-accel-redirect-serving-html-download-page-instead-of-file/222415>\
**Category:** NGINX\
**Created:** [August 28, 2012, 3:30am UTC](https://www.ruby-forum.com/t/x-accel-redirect-serving-html-download-page-instead-of-file/222415 "2012-08-28T03:30:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [August 28, 2012, 3:30am UTC](https://www.ruby-forum.com/t/x-accel-redirect-serving-html-download-page-instead-of-file/222415/1 "2012-08-28T03:30:04Z")

</div>

I have a script that uses x-accel-redirect on a nginx server to send  
large  
files (150mb+). The script serves downloads fine as long as their is a  
space  
in the name of the directory but not when the directory is a single  
word.  
Anyone know why it would be doing so when the file exists and the path  
is  
correct?

Layout: /Dir/file.exe /Dir2/file.exe /Another Dir/file.exe

If it’s /Another Dir/file.exe it serves the file perfectly, however if  
its  
/Dir/file.exe it serves the html of the download page. Anyone seen this  
before?

nginx config:  
server {  
access\_log logs/access\_log;  
access\_log on;  
limit\_conn gulag 3;  
error\_log /var/log/nginx/vhost-error\_log crit;  
listen 80 default;  
server\_name [www.my.com](http://www.my.com) [my.com](http://my.com);  
root /home/mysite/public\_html;  
#root /usr/share/nginx/html;  
autoindex off;  
index index.php index.html index.htm;

#rewrite ^._/([-\w.]+)/([-\w.]+).zip$  
/download.php?model=$1&file=$2.zip last;  
 #rewrite ^._/([-\w.]+)/([-\w.]+).exe$  
/download.php?model=$1&file=$2.exe last;  
#rewrite ^._/([-\w.]+)/([-\w.]+).nbh$  
/download.php?model=$1&file=$2.nbh last;  
 #rewrite ^._/([-\w.]+)/([-\w.]+).rar$  
/download.php?model=$1&file=$2.rar last;

error\_page 503 /503.html;  
location = /503.html {  
root /home/mysite/public\_html/errors;  
}

error\_page 504 /504.html;  
location = /504.html {  
root /home/mysite/public\_html/errors;  
}

# pass the PHP scripts to FastCGI server

```
    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:8888;
   #fastcgi_pass unix:/var/run/nginx-fcgi.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME

```

/home/mysite/public\_html$fastcgi\_script\_name;  
include /etc/nginx/fastcgi\_params;

```
   fastcgi_intercept_errors off;
    }

```

}

php code:

\<?php /\*File Download Script\*/ $fileserver\_path = "./shipped/"; // change this to the directory your files reside $fileurlpath = "http://my.com/shipped/"; $req\_file = basename($\_GET['file']); $model = $\_GET['model']; $thisfile = basename(\_\_FILE\_\_); $msg=""; $error=FALSE; $path = $model."/".$req\_file; if (empty($req\_file)) { $error=TRUE; $msg .= "Usage: $thisfile?model=\<model name\>&file=\<file\_to\_download\>  
"; } if (! isset($\_GET["model"])) { $error=TRUE; $msg .= "No Model information present. Unable to continue.  
"; } else { $fileserver\_path .= $model."/"; $fileurlpath .= $model."/"; /\* check if file exists \*/ if (!file\_exists($fileserver\_path.$req\_file)) { $error=TRUE; $msg .= "$req\_file doesn't exist."; } } /\* no web spamming \*/ if (!preg\_match("/^[a-zA-Z0-9.\_-]+$/", $req\_file, $matches)) { $error=TRUE; $msg .= "Spamming! I can't do that. Sorry."; } if(! ($error)) { //Hotlink Code if(eregi($\_SERVER["HTTP\_HOST"], str\_replace("www.", "", strtolower($\_SERVER["HTTP\_REFERER"])))) { if (! isset($\_GET['send\_file'])) { Header("Refresh: 5; url=http://my.com/$thisfile?model=$model&file=$req\_file&send\_file=yes"); } else { header("Cache-Control: public"); header('Content-Description: File Transfer'); header("Content-type: application/octet-stream"); //header('Content-Type: application/force-download'); //header('Content-Length: ' . filesize($fileserver\_path.$req\_file)); header('Content-Disposition: attachment; filename=' . $req\_file); header("Content-Transfer-Encoding: binary"); header("X-Accel-Redirect: /shipped/" . $path); exit; } //More Hotlink Code } else { Header("Refresh: ; url=http://my.com/$thisfile?model=$model&file=$req\_file"); } //End Hotlink } ?\>

Posted at Nginx Forum:

> **[X-accel-redirect serving html download page instead of file.](https://forum.nginx.org/read.php?2,230161,230161#msg-230161)**
>
> I have a script that uses x-accel-redirect on a nginx server to send large files (150mb+). The script serves downloads fine as long as their is a space in the name of the directory but not when the directory is a single word. Anyone know why it would...

---

<div class="post-metadata">

**Author:** ![wideawake](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d26b3c/32.png) [@wideawake](https://www.ruby-forum.com/u/wideawake)\
**Post date:** [August 28, 2012, 3:56am UTC](https://www.ruby-forum.com/t/x-accel-redirect-serving-html-download-page-instead-of-file/222415/2 "2012-08-28T03:56:56Z")

</div>

Hey,

Try adding this to your nginx conf where applicable:

add\_header Content-Disposition attachment;

Regards,

Justin D. [http://www.twitter.com/jdorfman](http://www.twitter.com/jdorfman)

NetDNA [http://www.netdna.com](http://www.netdna.com)  
The Science of Acceleration

---

<div class="post-metadata">

**Author:** ![wideawake](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d26b3c/32.png) [@wideawake](https://www.ruby-forum.com/u/wideawake)\
**Post date:** [August 28, 2012, 4:05am UTC](https://www.ruby-forum.com/t/x-accel-redirect-serving-html-download-page-instead-of-file/222415/3 "2012-08-28T04:05:42Z")

</div>

I tried adding what you suggested like so

location = /shipped {  
add\_header Content-Disposition attachment;  
root /home/mysite/public\_html/shipped;  
internal;  
}

and by just adding the line to the site config. Neither of which worked  
unfortunately.

Posted at Nginx Forum:

> **[Re: X-accel-redirect serving html download page instead of file.](https://forum.nginx.org/read.php?2,230161,230163#msg-230163)**
>
> I have a script that uses x-accel-redirect on a nginx server to send large files (150mb+). The script serves downloads fine as long as their is a space in the name of the directory but not when the directory is a single word. Anyone know why it would...

---

<div class="post-metadata">

**Author:** ![wideawake](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d26b3c/32.png) [@wideawake](https://www.ruby-forum.com/u/wideawake)\
**Post date:** [August 28, 2012, 4:31am UTC](https://www.ruby-forum.com/t/x-accel-redirect-serving-html-download-page-instead-of-file/222415/4 "2012-08-28T04:31:58Z")

</div>

I checked my configs and add\_header is after the root directive like so:

location = /shipped {  
root /home/mysite/public\_html/shipped;  
internal;  
add\_header Content-Disposition attachment;  
}

Not sure if order matters or if it should be above or below the internal  
directive. I know Maxim and Piotr S. will know 😉

Regards,

Justin D. [http://www.twitter.com/jdorfman](http://www.twitter.com/jdorfman)

NetDNA [http://www.netdna.com](http://www.netdna.com)  
The Science of Acceleration

---

<div class="post-metadata">

**Author:** ![wideawake](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d26b3c/32.png) [@wideawake](https://www.ruby-forum.com/u/wideawake)\
**Post date:** [August 28, 2012, 5:02am UTC](https://www.ruby-forum.com/t/x-accel-redirect-serving-html-download-page-instead-of-file/222415/5 "2012-08-28T05:02:57Z")

</div>

I noticed that if I use this in my config then shipped/Dir 1/file.exe  
returns file not found in browser and shipped/dir/file.exe still returns  
proper file extension (exe, etc) but contains the html content of the  
download page.

location = /files {  
root /home/mysite/public\_html/shipped;  
internal;  
add\_header Content-Disposition attachment;  
}

header(“X-Accel-Redirect: /files/” . $path);

While removing that and using “header(“X-Accel-Redirect: /shipped/” .  
$path);” the actual file location works for shipped/Dir 1/file.exe but  
not  
for shipped/dir/file.exe.

I’m at a lose as to why it’s not working properly.

Posted at Nginx Forum:

> **[Re: X-accel-redirect serving html download page instead of file.](https://forum.nginx.org/read.php?2,230161,230165#msg-230165)**
>
> I have a script that uses x-accel-redirect on a nginx server to send large files (150mb+). The script serves downloads fine as long as their is a space in the name of the directory but not when the directory is a single word. Anyone know why it would...

---

<div class="post-metadata">

**Author:** ![wideawake](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d26b3c/32.png) [@wideawake](https://www.ruby-forum.com/u/wideawake)\
**Post date:** [December 4, 2012, 3:08pm UTC](https://www.ruby-forum.com/t/x-accel-redirect-serving-html-download-page-instead-of-file/222415/6 "2012-12-04T15:08:34Z")

</div>

I’ve correct this bug adding an expire header like :

location ^~ /protected-file/ {  
expires 30s;  
add\_header Pragma public;  
add\_header Cache-Control “public, must-revalidate,  
proxy-revalidate”;  
gzip off;  
internal;  
alias /;  
}

And my PHP headers are :

header(“X-Accel-Redirect: /protected-file”.realpath($realpath));  
header(“X-Accel-Buffering: yes”);  
header(‘Content-Length: ‘.$size);  
header("Content-Type: “);  
header('Content-Disposition: attachment; filename=”’.$filename.’"');

Nginx MIME types are /etc/nginx/mime.conf (from normal config)

Posted at Nginx Forum:

> **[Re: X-accel-redirect serving html download page instead of file.](https://forum.nginx.org/read.php?2,230161,233566#msg-233566)**
>
> I have a script that uses x-accel-redirect on a nginx server to send large files (150mb+). The script serves downloads fine as long as their is a space in the name of the directory but not when the directory is a single word. Anyone know why it would...

---

<div class="post-metadata">

**Author:** ![wideawake](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d26b3c/32.png) [@wideawake](https://www.ruby-forum.com/u/wideawake)\
**Post date:** [September 3, 2012, 8:31pm UTC](https://www.ruby-forum.com/t/x-accel-redirect-serving-html-download-page-instead-of-file/222415/7 "2012-09-03T20:31:20Z")

</div>

Hello!

On Mon, Aug 27, 2012 at 09:29:47PM -0400, wideawake wrote:

> before?  
> I would suggest that the reason is that rewrites which are  
> commented out in the config you’ve posted:

> #rewrite ^._/([-\w.]+)/([-\w.]+).zip$  
> /download.php?model=$1&file=$2.zip last;  
> #rewrite ^._/([-\w.]+)/([-\w.]+).exe$  
> /download.php?model=$1&file=$2.exe last;  
> #rewrite ^._/([-\w.]+)/([-\w.]+).nbh$  
> /download.php?model=$1&file=$2.nbh last;  
> #rewrite ^._/([-\w.]+)/([-\w.]+).rar$  
> /download.php?model=$1&file=$2.rar last;

aren’t really commented out. And any X-Accel-Redirect results in  
rewrite match, and a download.php output being returned instead.  
Obvious solution would be to actually comment them out (or, if  
they are needed, to place them into a location which doesn’t match  
URIs returned in X-Accel-Redirect).

(Files/directories with space work as regular expressions above  
doesn’t match them.)

Maxim D.
