# Static files download

**URL:** <https://www.ruby-forum.com/t/static-files-download/189901>\
**Category:** NGINX\
**Created:** [June 9, 2010, 4:40pm UTC](https://www.ruby-forum.com/t/static-files-download/189901 "2010-06-09T16:40:44Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![axel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6de8d8/32.png) [@axel](https://www.ruby-forum.com/u/axel)\
**Post date:** [June 9, 2010, 4:40pm UTC](https://www.ruby-forum.com/t/static-files-download/189901/1 "2010-06-09T16:40:44Z")

</div>

Hi guys,

When I switched from Apache to Nginx, I didn’t notice an issue I  
introduced in my website. Static files are displayed into the browser  
instead of being downloaded. It’s the case for PDF files and more  
surprising Zip files. How comes? Did I forget some kind of  
configuration?

Thnks

Axel

---

<div class="post-metadata">

**Author:** ![axel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6de8d8/32.png) [@axel](https://www.ruby-forum.com/u/axel)\
**Post date:** [June 9, 2010, 4:44pm UTC](https://www.ruby-forum.com/t/static-files-download/189901/2 "2010-06-09T16:44:14Z")

</div>

On Wed, Jun 09, 2010 at 04:40:05PM +0200, Axel wrote:

> Hi guys,
> 
> When I switched from Apache to Nginx, I didn’t notice an issue I introduced in my website. Static files are displayed into the browser instead of being downloaded. It’s the case for PDF files and more surprising Zip files. How comes? Did I forget some kind of configuration?

Do you have this or similar line in configuration:

```
  include mime.types;

```

?

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![axel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6de8d8/32.png) [@axel](https://www.ruby-forum.com/u/axel)\
**Post date:** [June 9, 2010, 4:48pm UTC](https://www.ruby-forum.com/t/static-files-download/189901/3 "2010-06-09T16:48:56Z")

</div>

Yes I have this include in nginx.conf in the http section

Le 9 juin 2010 à 16:43, Igor S. a écrit :

---

<div class="post-metadata">

**Author:** ![axel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6de8d8/32.png) [@axel](https://www.ruby-forum.com/u/axel)\
**Post date:** [June 9, 2010, 4:59pm UTC](https://www.ruby-forum.com/t/static-files-download/189901/4 "2010-06-09T16:59:28Z")

</div>

On Wed, Jun 09, 2010 at 04:47:46PM +0200, Axel wrote:

> Yes I have this include in nginx.conf in the http section

Could you create a debug log:

> **[A debugging log](http://nginx.org/en/docs/debugging_log.html)**

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![axel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6de8d8/32.png) [@axel](https://www.ruby-forum.com/u/axel)\
**Post date:** [June 9, 2010, 8:07pm UTC](https://www.ruby-forum.com/t/static-files-download/189901/5 "2010-06-09T20:07:57Z")

</div>

I tried to review all my configuration. I do not understand why PDF  
files are returned inline to the browser. Images, videos and flash files  
are properly sent, why not pdt and zip?  
has anyone the same issue?

@Igor: here the debug log [http://pastebin.com/K4E6yvpD](http://pastebin.com/K4E6yvpD)

Thks

Le 9 juin 2010 à 16:58, Igor S. a écrit :

---

<div class="post-metadata">

**Author:** ![axel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6de8d8/32.png) [@axel](https://www.ruby-forum.com/u/axel)\
**Post date:** [June 9, 2010, 5:36pm UTC](https://www.ruby-forum.com/t/static-files-download/189901/6 "2010-06-09T17:36:21Z")

</div>

Actually, I have two active server sections. I tried on the other and  
the PDF works well. So I guess it’s more a server configuration issue:

server {  
listen 80;  
server\_name [www.xxxx.com](http://www.xxxx.com);  
root /var/www/xxxx/prod/public;  
index index.php;

```
    log_format main '$remote_addr - $remote_user [$time_local] 

```

“$request” ’  
'$status $body\_bytes\_sent “$http\_referer” ’  
‘“$http\_user\_agent” “$http\_x\_forwarded\_for”’;  
access\_log /var/log/nginx/xxxx.log main;

```
location ~/
{

  try_files $uri $uri/ @framework;

  fastcgi_pass 127.0.0.1:9000;
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME 

```

$document\_root/$fastcgi\_script\_name;

```
        include fastcgi_params;

}

    location ~* 

```

.css|.js|.jpg|.jpeg|.png|.gif|.swf|.svg|.tiff$ {  
expires 30d;  
}

```
location ~ /\. {
  deny all;
}

location @framework
{
  fastcgi_pass 127.0.0.1:9000;
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME $document_root/index.php;
  include fastcgi_params;

}

```

}

limit\_zone videos $binary\_remote\_addr 10m;

server {

```
      listen 80;
      server_name static.xxxx.com;
      root /var/www/xxxx/medias/;
expires 90d;

      location /videos/ {
        keepalive_timeout 200 190;

             #limit_conn videos 2;
             mp4;
             limit_rate_after 512k;
             limit_rate 512k;

error_page 404 = /videos/video_not_found.png;
      }

```

}

Tks

Axel

Le 9 juin 2010 à 16:58, Igor S. a écrit :

---

<div class="post-metadata">

**Author:** ![axel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6de8d8/32.png) [@axel](https://www.ruby-forum.com/u/axel)\
**Post date:** [June 10, 2010, 12:06am UTC](https://www.ruby-forum.com/t/static-files-download/189901/7 "2010-06-10T00:06:12Z")

</div>

Hello.

It’s passed to fastcgi because of location / that captures uri to pass  
to fastcgi.

I will say, that: It’s not the best solution what Igor suggest. It  
will get rid of the problem, but is not a real solution.

You should use:

location ~ / {  
try\_files $uri $uri/ @framework;  
}

location ~ .._/._.php$ {  
return 403;  
}

location ~ .php$ {  
fastcgi\_pass 127.0.0.1:9000;  
fastcgi\_index index.php;  
fastcgi\_param SCRIPT\_FILENAME  
$document\_root/index.php;  
include fastcgi\_params;  
}

> I tried to review all my configuration. I do not understand why PDF files are returned inline to the browser. Images, videos and flash files are properly sent, why not pdt and zip?  
> has anyone the same issue?

You have it in debug log, in part where nginx test location. Because  
nginx test all you location that you have in your config, the uri with  
pdf file is captured by / location and proxed to fastcgi. With Igor  
suggestion, pdf will be added to static content location. But I will  
suggest, to properly configured the location that are responsible for  
capturing php file, because you will have a problem with all different  
types of files, that you don’t put in that static location. like doc,  
xls, odt .. whatever.

BTW, it’s not better (for reader, at least :} )?:

- location ~\*  
.css|.js|.jpg|.jpeg|.png|.gif|.swf|.svg|.tiff|.pdf$ {

- location ~\* .(css|js|jpg|jpeg|png|gif|swf|svg|tiff|pdf)$ {

---

<div class="post-metadata">

**Author:** ![axel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6de8d8/32.png) [@axel](https://www.ruby-forum.com/u/axel)\
**Post date:** [June 9, 2010, 8:12pm UTC](https://www.ruby-forum.com/t/static-files-download/189901/8 "2010-06-09T20:12:39Z")

</div>

On Wed, Jun 09, 2010 at 08:06:35PM +0200, Axel wrote:

> I tried to review all my configuration. I do not understand why PDF files are returned inline to the browser. Images, videos and flash files are properly sent, why not pdt and zip?  
> has anyone the same issue?
> 
> @Igor: here the debug log [http://pastebin.com/K4E6yvpD](http://pastebin.com/K4E6yvpD)

/inc/BC\_RV\_2010.pdf is passed to fastcgi and it returns  
“Content-Type: text/html; charset=utf8”

You should add .pdf to static location:

- location ~\* .css|.js|.jpg|.jpeg|.png|.gif|.swf|.svg|.tiff$  
{

- location ~\*  
.css|.js|.jpg|.jpeg|.png|.gif|.swf|.svg|.tiff|.pdf$ {

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![axel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6de8d8/32.png) [@axel](https://www.ruby-forum.com/u/axel)\
**Post date:** [June 10, 2010, 10:11am UTC](https://www.ruby-forum.com/t/static-files-download/189901/9 "2010-06-10T10:11:28Z")

</div>

Hello,

thanks for your answers. The thing is that I use a framework (Kohana)  
with rewrite rules. With your system Grzegorz, an URL like  
[XXXX](http://www.xxxx.com/video/test_1.htm) (rewritten by the framework in  
video.php?id=1) will not be parsed as PHP, will it? I found and adapted  
this server configuration for Kohana. Obviously it doesn’t work  
properly.

I also tried to have a very simple / location (like yours), a .php  
location and a kohana location. But this [http://www.xxxx.com/](http://www.xxxx.com/) (with auto  
index to index.php) doesn’t work either. When U browse this URL, the  
index.php is downloaded instead of being parsed.

Axel

Le 10 juin 2010 Ã 00:05, Grzegorz S. a Ã©crit :

---

<div class="post-metadata">

**Author:** ![axel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6de8d8/32.png) [@axel](https://www.ruby-forum.com/u/axel)\
**Post date:** [June 10, 2010, 10:17am UTC](https://www.ruby-forum.com/t/static-files-download/189901/10 "2010-06-10T10:17:54Z")

</div>

On Thu, Jun 10, 2010 at 12:05:06AM +0200, Grzegorz S. wrote:

> location ~ / {  
> The location above will handle all requests.

> ```
> fastcgi_param SCRIPT_FILENAME $document_root/index.php;
> 
> ```
> 
> ## suggestion, pdf will be added to static content location. But I will
> 
> Best Regards  
> Grzegorz SieÅ„ko
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![axel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6de8d8/32.png) [@axel](https://www.ruby-forum.com/u/axel)\
**Post date:** [June 10, 2010, 10:39am UTC](https://www.ruby-forum.com/t/static-files-download/189901/11 "2010-06-10T10:39:34Z")

</div>

Ok i try this and let U know.  
Tkss

Axel

Le 10 juin 2010 Ã 10:17, Igor S. a Ã©crit :

---

<div class="post-metadata">

**Author:** ![axel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6de8d8/32.png) [@axel](https://www.ruby-forum.com/u/axel)\
**Post date:** [June 10, 2010, 12:23pm UTC](https://www.ruby-forum.com/t/static-files-download/189901/12 "2010-06-10T12:23:11Z")

</div>

Hi again guys,

thanks for your help. Actually I wrote my entire configuration file  
again from scratch. It works very well now: [http://pastebin.com/pCL2dy9Z](http://pastebin.com/pCL2dy9Z)  
I’ll post this configuration somewhere for Kohana users because it’s  
been was a pain to get it work.

Static files are now sent straight from Nginx, not fastcgi and mime  
types are working fine !!!

Tks a lot again

Axel

Le 10 juin 2010 Ã 10:17, Igor S. a Ã©crit :

---

<div class="post-metadata">

**Author:** ![axel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6de8d8/32.png) [@axel](https://www.ruby-forum.com/u/axel)\
**Post date:** [June 10, 2010, 2:16pm UTC](https://www.ruby-forum.com/t/static-files-download/189901/13 "2010-06-10T14:16:08Z")

</div>

On Thu, Jun 10, 2010 at 12:22:04PM +0200, Axel wrote:

> thanks for your help. Actually I wrote my entire configuration file again from scratch. It works very well now: [http://pastebin.com/pCL2dy9Z](http://pastebin.com/pCL2dy9Z)  
> I’ll post this configuration somewhere for Kohana users because it’s been was a pain to get it work.

This is wrong configuration.

1. All these

are not macros, but run-time directives. They will run every request.  
It’s much better to set them in place:

```
  fastcgi_param SERVER_TYPE prod;
  fastcgi_param MEDIAS_PATH /var/www/xxxx/medias;

```

1. This

is one of the worst configuration practice.  
Never do this, never repeat this.

The right way is:

```
location / {
    try_files $uri @kohana;
}

location @kohana {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_param SCRIPT_FILENAME $document_root/index.php;
    fastcgi_param DOCUMENT_URI /index.php$uri;
    ... other fastcgi_params
}

```

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![axel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6de8d8/32.png) [@axel](https://www.ruby-forum.com/u/axel)\
**Post date:** [June 10, 2010, 3:10pm UTC](https://www.ruby-forum.com/t/static-files-download/189901/14 "2010-06-10T15:10:34Z")

</div>

Igor,

I’m trying to follow your advise but look:

# ROUTING TO KOHANA IF REQUIRED

location / {  
try\_files $uri $uri/ @kohana;  
}

# HANDLES THE REWRITTEN URLS TO KOHANA CONTROLLER

location @kohana  
{  
fastcgi\_pass 127.0.0.1:9000;  
fastcgi\_index index.php;  
include fastcgi\_params;  
include hom\_params;  
fastcgi\_param SCRIPT\_FILENAME $document\_root/index.php;  
}

# FOR PHP FILES NOT HANDLED BY KOHANA

location ~\* .php$ {  
fastcgi\_pass 127.0.0.1:9000;  
fastcgi\_index index.php;  
fastcgi\_param SCRIPT\_FILENAME $document\_root$fastcgi\_script\_name;  
include fastcgi\_params;  
include hom\_params;  
}

Nearly everything works at this stage EXCEPT:

when I browse an URL like “[XXXX](http://www.xxxx.com/share.php)”, this file  
(share.php) does not exist in the document root therefore this URL  
should be handled by @kohana.  
As an answer, I get a “No input file specified”. I’m pretty sure the  
.php location is taking over @kohana. I tried to add a break in @kohana  
but no success.

Do you see what I mean?

Le 10 juin 2010 à 14:15, Igor S. a écrit :

---

<div class="post-metadata">

**Author:** ![axel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6de8d8/32.png) [@axel](https://www.ruby-forum.com/u/axel)\
**Post date:** [June 10, 2010, 3:21pm UTC](https://www.ruby-forum.com/t/static-files-download/189901/15 "2010-06-10T15:21:41Z")

</div>

On Thu, Jun 10, 2010 at 03:09:34PM +0200, Axel wrote:

> # HANDLES THE REWRITTEN URLS TO KOHANA CONTROLLER
> 
> location ~\* .php$ {  
> when I browse an URL like “[XXXX](http://www.xxxx.com/share.php)”, this file (share.php) does not exist in the document root therefore this URL should be handled by @kohana.  
> As an answer, I get a “No input file specified”. I’m pretty sure the .php location is taking over @kohana. I tried to add a break in @kohana but no success.
> 
> Do you see what I mean?

The single “break” is meanless directive.

You should add try\_files test in .php$ location:

location ~\* .php$ {

- 

```
try_files $uri @kohana;

```

fastcgi\_pass 127.0.0.1:9000;

Please read also the order of location processing:

> **[How nginx processes a request](http://nginx.org/en/docs/http/request_processing.html#simple_php_site_configuration)**
>
> nginx first decides which server should process the request. Let’s start with a simple configuration where all three virtual servers listen on port \*:80:

> > 1. This
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![axel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6de8d8/32.png) [@axel](https://www.ruby-forum.com/u/axel)\
**Post date:** [June 10, 2010, 3:55pm UTC](https://www.ruby-forum.com/t/static-files-download/189901/16 "2010-06-10T15:55:04Z")

</div>

Awesome guys, thanks a lot. Here’s the updated conf:

[http://pastebin.com/cBkFh88c](http://pastebin.com/cBkFh88c)

Thks Again

Axel

Le 10 juin 2010 à 15:23, Edho P Arief a écrit :

---

<div class="post-metadata">

**Author:** ![axel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6de8d8/32.png) [@axel](https://www.ruby-forum.com/u/axel)\
**Post date:** [June 10, 2010, 3:24pm UTC](https://www.ruby-forum.com/t/static-files-download/189901/17 "2010-06-10T15:24:02Z")

</div>

On Thu, Jun 10, 2010 at 8:09 PM, Axel [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Â Â Â Â # HANDLES THE REWRITTEN URLS TO KOHANA CONTROLLER  
> Â Â Â Â location ~\* .php$ {  
> when I browse an URL like “[XXXX](http://www.xxxx.com/share.php)”, this file (share.php) does not exist in the document root therefore this URL should be handled by @kohana.  
> As an answer, I get a “No input file specified”. I’m pretty sure the .php location is taking over @kohana. I tried to add a break in @kohana but no success.
> 
> Do you see what I mean?

add

try\_files $uri @kohana;

## to the location ~\* .php$ { } block

O\< ascii ribbon campaign - stop html mail - [www.asciiribbon.org](http://www.asciiribbon.org)
