Static files download

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

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.
http://sysoev.ru/en/

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

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

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:
http://nginx.org/en/docs/debugging_log.html


Igor S.
http://sysoev.ru/en/

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

Thks

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

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;
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 :

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)$ {

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

/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.
http://sysoev.ru/en/

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 (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/ (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 :

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
[email protected]
nginx Info Page


Igor S.
http://sysoev.ru/en/

Ok i try this and let U know.
Tkss

Axel

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

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
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 :

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
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

    set $server_type prod;
    set $medias_path /var/www/xxxx/medias;

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

    if (!-e $request_filename) {
    rewrite ^(.+)$ /index.php$1 last;
    break;
    }

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.
http://sysoev.ru/en/

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”, 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 :

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”, 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:
http://nginx.org/en/docs/http/request_processing.html#simple_php_site_configuration

  1. This

nginx mailing list
[email protected]
nginx Info Page


Igor S.
http://sysoev.ru/en/

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

http://pastebin.com/cBkFh88c

Thks Again

Axel

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

On Thu, Jun 10, 2010 at 8:09 PM, Axel [email protected] wrote:

    # HANDLES THE REWRITTEN URLS TO KOHANA CONTROLLER
    location ~* .php$ {
when I browse an URL like “XXXX”, 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