Forum: NGINX try_files and content type

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Xavier G. (Guest)
on 2009-02-14 12:00
(Received via mailing list)
Hello,

I have an issue with try_files and content-type ( nginx 0.7.34 ), it
doesn't use the cache file extention to discover the mime.type
so if we call a direct file host/sitemap.xml content type is ok, but
if I call a directory host/faq/ the returned content-type is the
default.

My default_type is set to application/octet-stream, my application
is directory base ( host/faq/, host/register/ ), I only have a few
call to files ( host/robots.txt host/sitemap.xml )

Most of the content is text/html but I can be sometimes for exemple
text/xml or application/pdf.

For instance /cachedir/faq/host.index.html is the cache file for
http://host/faq
  ( I use host in the cache file name to handle languages )
I could have a pdf version of the faq at the following url
http://host/faq/pdf/
  then the cache file would be /cachedir/faq/pdf/host.index.pdf

the problem is that once cached try_file would return the default_type
for all those urls.

I don't know if this is the expected beavhiour, I expected try_files
to return the type according to the cache file extension. Of course I
can
use text/html as default type but in this case I cannot handle
multiple types fils within the same location.

with the old fashion way:

if (-f $request_filename/$host.index.html) { rewrite (.*) $uri/
$host.index.html break; }
if (-f $request_filename/$host.index.pdf) { rewrite (.*) $uri/
$host.index.pdf break; }

types where correct.

Below three tests, first one a direct call returning content type is
the one expected.
Then my expected result would have been text/html instead of
application/octet-stream

xav - is sorry for poor english xD


------------------------------------------------------------------------------------------

http {
     include /etc/nginx/mime.types;
     include /etc/nginx/fastcgi_params;
     index index.html;
     default_type application/octet-stream;

     server {
         listen 10.0.0.181;
         server_name devel.1cafe.fr;
         root /home/cache/hard;
         error_page 599 = @django;

         location @django {
             fastcgi_pass localhost:3000;
         }

         location / {
             # default_type text/html;
             # try_files $uri$host.index.html $uri$host.index.pdf
@django;
             # test purpose
             try_files $uri $uri/index.html $uri$host.index.html $uri
$host.index.pdf @django;
         }
     }
}

GET devel.1cafe.fr/sitemap.xml

2009/02/14 10:09:40 [debug] 22727#0: *1 http script var: "/sitemap.xml"
2009/02/14 10:09:40 [debug] 22727#0: *1 try to use file: "/sitemap.xml"
2009/02/14 10:09:40 [debug] 22727#0: *1 try file uri: "/sitemap.xml"
2009/02/14 10:09:40 [debug] 22727#0: *1 http filename: "/home/cache/
hard/sitemap.xml"
2009/02/14 10:09:40 [debug] 22727#0: *1 http set discard body
2009/02/14 10:09:40 [debug] 22727#0: *1 HTTP/1.1 200 OK
Server: nginx
Date: Sat, 14 Feb 2009 09:09:40 GMT
Content-Type: text/xml
Content-Length: 870
Last-Modified: Sat, 14 Feb 2009 08:51:51 GMT
Connection: close
Served-By: intra2
Accept-Ranges: bytes


GET devel.1cafe.fr/

2009/02/14 10:11:01 [debug] 22727#0: *2 http script copy: "/index.html"
2009/02/14 10:11:01 [debug] 22727#0: *2 try to use file: "//index.html"
2009/02/14 10:11:01 [debug] 22727#0: *2 try file uri: "//index.html"
2009/02/14 10:11:01 [debug] 22727#0: *2 http filename: "/home/cache/
hard//index.html"
2009/02/14 10:11:01 [debug] 22727#0: *2 http set discard body
2009/02/14 10:11:01 [debug] 22727#0: *2 HTTP/1.1 200 OK
Server: nginx
Date: Sat, 14 Feb 2009 09:11:01 GMT
Content-Type: application/octet-stream
Content-Length: 57050
Last-Modified: Sat, 14 Feb 2009 08:37:55 GMT
Connection: close
Served-By: intra2
Accept-Ranges: bytes

GET http://devel.1cafe.fr/faq/

2009/02/14 10:12:46 [debug] 22727#0: *25 http script copy: ".index.html"
2009/02/14 10:12:46 [debug] 22727#0: *25 try to use file: "/faq/devel.
1cafe.fr.index.html"
2009/02/14 10:12:46 [debug] 22727#0: *25 try file uri: "/faq/devel.
1cafe.fr.index.html"
2009/02/14 10:12:46 [debug] 22727#0: *25 http filename: "/home/cache/
hard/faq/devel.1cafe.fr.index.html"
2009/02/14 10:12:46 [debug] 22727#0: *25 http set discard body
2009/02/14 10:12:46 [debug] 22727#0: *25 HTTP/1.1 200 OK
Server: nginx
Date: Sat, 14 Feb 2009 09:12:46 GMT
Content-Type: application/octet-stream
Content-Length: 44232
Last-Modified: Sat, 14 Feb 2009 09:12:40 GMT
Connection: close
Served-By: intra2
Accept-Ranges: bytes
Igor S. (Guest)
on 2009-02-14 14:10
(Received via mailing list)
Attachment: patch.try_files.exten (0 Bytes)
On Sat, Feb 14, 2009 at 10:50:28AM +0100, Xavier G. wrote:

> call to files ( host/robots.txt host/sitemap.xml )
> the problem is that once cached try_file would return the default_type
> if (-f $request_filename/$host.index.html) { rewrite (.*) $uri/
>
>
>     server {
>         listen 10.0.0.181;
>         server_name devel.1cafe.fr;
>         root /home/cache/hard;
>         error_page 599 = @django;

What is 599 code ?

> $host.index.pdf @django;
>         }
>     }
> }

The attached pacth should fix the bug.
Xavier G. (Guest)
on 2009-02-14 14:48
(Received via mailing list)
>>        error_page 599 = @django;
>
> What is 599 code ?
>

I picked an un used code to do stuff like this :

location @django {
  fastcgi_pass localhost:3006;
}

if ($request_method ~* POST ) {
  return 599;
}

I couldn't figure out how to it a more regular way. Do you think it's
a bad idea ?

> The attached pacth should fix the bug.

works like a charm. Thanks a lot.

xav
Arvind Jayaprakash (Guest)
on 2009-02-15 13:02
(Received via mailing list)
On Feb 14, Xavier G. wrote:
> if ($request_method ~* POST ) {
>   return 599;
> }

HTTP 405 seems to be a better status code for this.
This topic is locked and can not be replied to.