dynamic default_type

Maxim Dounin mdounin at mdounin.ru
Wed Feb 6 05:16:35 MSK 2008


Hello!

On Mon, Feb 04, 2008 at 09:38:18PM +0000, Ilya Grigorik wrote:

>I'm trying to integrate Nginx memcached module as a front-end server for serving
>cached requests, but the problem is, the application type is defined dynamically
>as part of the query string.. For example:
>
>GET /test?format=xml 
>GET /test?format=json
>
>The requests get cached by Nginx and memcached, but on subsequent queries, they
>are returned as default 'octetstream', which is not very useful.. So I've got as
>far as creating a dynamic variable in my config:
>
>
> 90         location /test {
> 91            if ($args ~* format=json) { set $type "text/javascript"; }
> 92            if ($args ~* format=xml)  { set $type "application/xml"; }
> 93 
> 94            default_type $type;
> 95            ...
> 95         }
>
>The problem is, default_type does not seem to evaluate the parameter and returns
>the literal string "$type". Any ideas on how to get it to return the actual
>string it points to?

This is expected, as default_type doesn't support variables.

>I also tried breaking up the queries by location regex:
> 82         location ~* test.*format=xml {
> 83             default_type application/xml;
> 84             ...
> 88         }
>
>But the regex does not seem to pass or is invalid. 

This is expected too, as location match path only, not query 
string.

>
>Any tips would be appreciated..

Try something like this:

     location /test {
         if ($args ~* "format=json") {
             rewrite ^ /test-json last;
         }
         if ($args ~* "format=xml") {
             rewrite ^ /test-xml last;
         }
     }
     location /test-json {
         default_type "text/javascript";
         ...
     }
     location /test-xml {
         default_type "application/xml";
         ...
     }

Maxim Dounin





More information about the nginx mailing list