Dynamic default_type

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?

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.

Any tips would be appreciated…

Hello!

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

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

Thanks Max.

After some trial and error I got it all working:
http://www.igvita.com/2008/02/11/nginx-and-memcached-a-400-boost/

Thanks for the tips.

Cheers,
Ilya