Forum: NGINX memcache_pass not allowed in if block?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
4e846d456cd4574e3e84a8c78aabc6dc?d=identicon&s=25 John Luxford (Guest)
on 2009-03-22 00:40
(Received via mailing list)
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
535e2bd84829abaf90def03e89299e20?d=identicon&s=25 Marcus Clyne (Guest)
on 2009-03-22 02:22
(Received via mailing list)
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.
4e846d456cd4574e3e84a8c78aabc6dc?d=identicon&s=25 John Luxford (Guest)
on 2009-03-22 05:10
(Received via mailing list)
Hi Marcus,

Thanks for the info. Looks like the MacPorts version is 0.5.34 so I'll
have to try compiling it myself. I didn't think it changed since the
fastcgi_pass docs say it goes in location or location-if while
memcached_pass says "http, server, location" but not location-if.

Sounds like my problem is solved by an upgrade though, which is good
news!

Thanks again,

Lux
535e2bd84829abaf90def03e89299e20?d=identicon&s=25 Marcus Clyne (Guest)
on 2009-03-22 05:27
(Received via mailing list)
> Thanks for the info. Looks like the MacPorts version is 0.5.34 so I'll
> have to try compiling it myself. I didn't think it changed since the
> fastcgi_pass docs say it goes in location or location-if while
> memcached_pass says "http, server, location" but not location-if.
>
Yes, I just checked - the documentation is currently wrong.

Igor, is there a reason for not having memcached_pass in main / server /
server if?  If not, could you possibly change it so that it's possible?
Other directives like fastcgi_pass too might be useful to be set in
main/server/server-if, unless there is a reason for not allowing it.

Thanks,

Marcus.
This topic is locked and can not be replied to.