Best way to get the $request_uri without the starting slash

Matthew Ward mward at
Mon Dec 19 09:54:33 UTC 2011

I want to pass the $request_uri to memcached as the key but without the starting slash, as our memcached key names are a prefixed integer which can be referenced from the URI, as follows: -> prefix:1000

In nginx, the only (or all) variables that refer to the URI also contain the starting slash. So if I were to use prefix:$request_uri as above I would get prefix:/1000 as the key.

At the moment I'm solving this by using a regex conditional to set a key name (this is paraphrased):

if ($request_uri ~* ^/(.*)$) {
  set $request_key $1;

set $memcached_key prefix:$request_key;

So now I have the correct key name for memcached. Are there any better ways of doing this? Would this have a serious impact on performance, performing this conditional every time? Is it possible do this using set without the conditional part?

Thanks for any help,

More information about the nginx mailing list