conditional behaviour based on Accept: values

Manlio Perillo manlio_perillo at libero.it
Fri Mar 7 18:20:32 MSK 2008


gabriele renzi ha scritto:
> Hi everyone,
> 

Hi Gabriele!

> I just started to use nginx and it seems great, thanks to everyone
> involved in code or docs.
> But of course, now I hit a small problem: how can I access the values
> proposed from the client via the Accept: header and behave according
> to that?
> 
> In context: I'm emitting different values based on this value and I'd
> like to take advantage of nginx to properly cache them.
> Ideally, I'd like to do something like
> 
> if ("rdf" ~* accepts ) {
>   set $memcached_key $uri.rdf;
>   memcached_pass;
> }
> if ("json" ~* accepts ) {
>   set $memcached_key $uri.json;
>   memcached_pass;
> }
> 
> set $memcached_key $uri.html;
> memcached_pass;
> 
> 
> yeah, maybe avoiding the triple call to memcached_pass, too :)
> 
> It this possible somehow? 


The value of the accept header can be found in the $http_accept variable.

Every header can be accessed this way:
http://wiki.codemongers.com/NginxHttpCoreModule#var_args

To avoid the triple memcache_pass, you can just do:

set $memcached_key $uri.html;

if ("rdf" ~* accepts ) {
   set $memcached_key $uri.rdf;
   memcached_pass;
}
if ("json" ~* accepts ) {
   set $memcached_key $uri.json;
   memcached_pass;
}

memcached_pass;


(untested)


For better parsing, you can use the perl module (not included by default).

> I' tried to look at the HttpCore module, but
> I could not understand if there is a way to do it.
> 
> thanks in advance for any help.
> 
> 


Manlio Perillo





More information about the nginx mailing list