try_files and content type

Igor Sysoev is at rambler-co.ru
Sat Feb 14 14:57:12 MSK 2009


On Sat, Feb 14, 2009 at 10:50:28AM +0100, Xavier Grangier wrote:

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

What is 599 code ?

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

The attached pacth should fix the bug.


-- 
Igor Sysoev
http://sysoev.ru/en/
-------------- next part --------------
Index: src/http/ngx_http_core_module.c
===================================================================
--- src/http/ngx_http_core_module.c	(revision 1822)
+++ src/http/ngx_http_core_module.c	(working copy)
@@ -1197,6 +1197,11 @@
             ngx_memcpy(p, name, path.len);
         }
 
+        if (ngx_http_set_exten(r) != NGX_OK) {
+            ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
+            return NGX_OK;
+        }
+
         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                        "try file uri: \"%V\"", &r->uri);
 


More information about the nginx mailing list