memcache_pass not allowed in if block?

Marcus Clyne eugaia at gmail.com
Sun Mar 22 04:13:44 MSK 2009


John,

I'm guessing that you have the if... configuration inside a 'server if' 
rather than a 'location if' block.  In the latest version (0.7.43), 
memcached pass can only be set in 'location' and 'location if', so you 
can't set it in 'server if' blocks.

You have (at least) two options;

1) Put your if statement inside a location block

e.g.

location / { 

   if ($http_cookie != "session_id=") {
       set $memcached_key $uri;
       memcached_pass 127.0.0.1 11211;
       default_type text/html;
   }

}

Note : this could get tedious if you have multiple locations, so you 
might want to put the check in a separate file, and just include it - i.e.

<session_check.conf>

   if ($http_cookie != "session_id=") {
       set $memcached_key $uri;
       memcached_pass 127.0.0.1 11211;
       default_type text/html;
   }

<nginx.conf>
...
location / { 

include  session_check.conf;

}


2)  Modify the source code of the memcache module (this is easy)

change line 60
- NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
+ 
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,

This will allow you to set memcache_pass in any block (except other 
upstream ones).  All the macros apart from the last one refer to where 
the directive is allowed, the meanings of which should be pretty clear 
if you are familiar with config files.  The key one for you to add is 
the NGX_HTTP_SIF_CONF one, which will allow the directive to be included 
in if statements at the server level.

If you want to use other memcache directives in other blocks than 
location/location-if ones, then you'll need to bit-or any other blocks 
you want to set them in. You could copy all the entries on the above 
line except for NGX_CONF_TAKE1 (which refers to the number of arguments 
allowed in the config, and you shouldn't change that for the other 
directives).

Note : I've not tried this, and there may be a reason why you can't use 
the memcache_pass directive in other blocks (but if there is, I don't 
know it).  If there's no reason for not including it at the server/main 
level as well (it's sometimes convenient to put things there), perhaps 
Igor will change the modules to allow that.


One note : the fastcgi_pass directive has the same restrictions as 
memcache_pass.  It probably works because you've put it in a location or 
location-if, and it's not at the server / main level.

Cheers,

Marcus.


John Luxford wrote:
> Hi,
>
> I would like to check for a session cookie before using
> memcached_pass, since authenticated users in our site would be seeing
> more dynamic content on the same pages that anonymous users would see
> cached copies of. But when I do something like this:
>
> if ($http_cookie != "session_id=") {
>     set $memcached_key $uri;
>     memcached_pass 127.0.0.1 11211;
>     default_type text/html;
> }
>
> I get the following error:
>
> 2009/03/21 18:20:27 [emerg] 78240#0: "memcached_pass" directive is not
> allowed here in /opt/local/etc/nginx/sites.conf:29
>
> I see that fastcgi_pass can be used in if blocks, so why can't
> memcached_pass too? And is there any workaround to this? I'm just
> getting up to speed on configuring nginx, but can't seem to find
> another way of going about this.
>
> Thanks!
>
> Lux
>
>
>   






More information about the nginx mailing list