Conditional behaviour based on Accept: values

Hi everyone,

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 :slight_smile:

It this possible somehow? 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.

gabriele renzi ha scritto:

Hi everyone,

Hi Gabriele!

if (“rdf” ~* accepts ) {

yeah, maybe avoiding the triple call to memcached_pass, too :slight_smile:

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

On Fri, Mar 7, 2008 at 3:20 PM, Manlio P.
[email protected] wrote:

gabriele renzi ha scritto:

Hi everyone,

Hi Gabriele!

eilà :slight_smile:

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

ah thank you I overlooked that.

To avoid the triple memcache_pass, you can just do:

no I just meant to avoid writing it 3 times, but with current nginx
this seems to work as I expect it:

  if ($http_accept ~* rdf) {
    set $memcached_key DATA$uri-$args.rdf;
  }
  if ($http_accept ~* json) {
    set $memcached_key DATA$uri-$args.json;
  }
  if ($http_accept ~* plain) {
    set $memcached_key DATA$uri-$args.txt;
  }
  memcached_pass   memcached;

next I’ll have to check if I can do “set $key $key.foo” :slight_smile:

Thanks again for the help.

gabriele renzi ha scritto:

[…]

To avoid the triple memcache_pass, you can just do:

no I just meant to avoid writing it 3 times, but with current nginx
this seems to work as I expect it:

Yes, I forgot to remove the memcached_pass directives in the if :).

next I’ll have to check if I can do “set $key $key.foo” :slight_smile:

key.foo what should return?

However no, it is not possible.
But check this extension I have written:
http://hg.mperillo.ath.cx/nginx/mod_parsed_vars/file/tip/README

Thanks again for the help.

Ciao Manlio P.