String replace on rewrite module

Alex Sergeyev asergeyev at dyn.com
Wed Jul 21 17:34:08 MSD 2010


On Wed, 2010-07-21 at 10:32 +0600, Mikhail Mazursky wrote:
> I think there's embedded perl for that kind of stuff, but i can't help
> because i don't know perl :)

Yeah... something like this would also work

perl_set $underscored_uri 'sub { ($_ = $_[0]->uri) =~
s/(?<=.)\/(?=.*/)/_/g; $_ }'

location /something {
   rewrite . $underscored_uri last;  
}


1. you're ending up with perl module which is big
2. could be trickier if you have querystring to process
3. likely it'll be slower than recursive rewrites.


Alex.

p.s.
if you want to debug this in command line:

perl -Mstrict -we 'sub uri{$ARGV[0]} sub test{($_=uri()) =~
s/(?<=.)\/(?=.*\/)/_/g;$_} print
test()."\n"' /lkasld/asda/sda/sd/image.gif

will print you
/lkasld_asda_sda_sd/image.gif





More information about the nginx mailing list