Ruby Forum NGINX > a newbie question regarding regex in nginx conf

Posted by Zhang Yining (Guest)
on 03.09.2008 17:43
(Received via mailing list)
Hi list,

I have a question regarding regex in nginx conf, and believe it must
have been brought up before, yet after googling for it, I could not find
an answer.

Specifically, is it possible to use curly braces in nginx conf?

For example, for some url like: /photos/123456

I would want to rewrite it to: /path/to/photos/12/1234/123456.png

in apache's conf rewrite rule, I could use something like:

[0-9]{2}[0-9]{2}[0-9]{2}  /path/to/photos/$1/$1$2/$1$2$3.png

But it seems curly braces are reserved for nginx conf file's own use,
and not recognized by the regex processing.

So is there any way to use curly braces in regex? If so, could some one
kind enough to provide a link or short explanation of how to do it?

Thanks in advance,

Yining
Posted by Maxim Dounin (Guest)
on 03.09.2008 18:04
(Received via mailing list)
Hello!

On Wed, Sep 03, 2008 at 11:33:15PM +0800, Zhang Yining wrote:

>I would want to rewrite it to: /path/to/photos/12/1234/123456.png
>
>in apache's conf rewrite rule, I could use something like:
>
>[0-9]{2}[0-9]{2}[0-9]{2}  /path/to/photos/$1/$1$2/$1$2$3.png
>
>But it seems curly braces are reserved for nginx conf file's own use,
>and not recognized by the regex processing.
>
>So is there any way to use curly braces in regex? If so, could some one
>kind enough to provide a link or short explanation of how to do it?

Just quote the regex, i.e:

rewrite  "([0-9]{2})([0-9]{2})([0-9]{2})" 
/path/to/photos/$1/$1$2/$1$2$3.png;

(single quotes will do the trick too).

It's documented in
http://sysoev.ru/nginx/docs/http/ngx_http_rewrite_module.html#rewrite
(in russian), but not (yet) in english wiki.  Feel free to update
http://wiki.codemongers.com/NginxHttpRewriteModule#rewrite.

Maxim Dounin
Posted by Chris Savery (Guest)
on 03.09.2008 18:19
(Received via mailing list)
I'm curious about your breaking up the filename into a path like that.
If using ext3 filesystem is there any advantage speed wise to doing
that? I thought it could handle large file counts without a problem
using indexing but I may be wrong and I am quite interested as I expect
to have thousands of image files as well and if it's going to be slow on
ext3 then I would also want to split up directories like this.
Chris :)
Posted by Zhang Yining (Guest)
on 03.09.2008 18:51
(Received via mailing list)
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi Maxim,

Thanks for the prompt reply!

Maxim Dounin wrote:
>>
>> So is there any way to use curly braces in regex? If so, could some one
>> kind enough to provide a link or short explanation of how to do it?
> 
> Just quote the regex, i.e:
> 
> rewrite  "([0-9]{2})([0-9]{2})([0-9]{2})" 
> /path/to/photos/$1/$1$2/$1$2$3.png;
> 
> (single quotes will do the trick too).

Ah I see! Why didn't I think of trying that before posting the question?

> 
> It's documented in
> http://sysoev.ru/nginx/docs/http/ngx_http_rewrite_module.html#rewrite
> (in russian), but not (yet) in english wiki.  Feel free to update
> http://wiki.codemongers.com/NginxHttpRewriteModule#rewrite.
> 

Done.

> Maxim Dounin
> 
> 

- --
Zhang Yining
URL:         http://www.zhangyining.net | http://www.yining.org
mailto:      yining@zhangyining.net | zhang.yining@gmail.com
Fingerprint: 25C8 47AE 30D5 4C0D A4BB  8CF2 3C2D 585F A905 F033
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFIvr5XPC1YX6kF8DMRAupRAJwJyK4VHjux1Pc9tjA38vx58AfOSACeJBxr
sxQG4vj7bPv+Q1mQbOWmot4=
=jy7G
-----END PGP SIGNATURE-----
Posted by mike (Guest)
on 03.09.2008 18:51
(Received via mailing list)
i try to keep directories to less than 10,000 files apiece,
personally. i think nearly every filesystem it's a good idea to put a
limit on it, especially if you ever use "rm" and such - eventually the
argument list gets too long too :)
Posted by Zhang Yining (Guest)
on 03.09.2008 19:18
(Received via mailing list)
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi Chris,

Chris Savery wrote:
> I'm curious about your breaking up the filename into a path like that.
> If using ext3 filesystem is there any advantage speed wise to doing
> that? I thought it could handle large file counts without a problem
> using indexing but I may be wrong and I am quite interested as I expect
> to have thousands of image files as well and if it's going to be slow on
> ext3 then I would also want to split up directories like this.
> Chris :)

First of all, I simplified the question a little, all url's are actually
ended with "-(L|M|S|Q|T)" denoting different sizes (large, medium,
small, square, thumbnail), so that's multiplication by five :-)

Secondly, yes, it's ext3 (on my dev pc), but I split files this way is
more for ease of management and some flexibility instead of from purly
performance stand point.

I don't have hard data and facts right now, but in my understanding,
splitting up does help in terms of performance. I will share more info
when I have some more.


- --
Zhang Yining
URL:         http://www.zhangyining.net | http://www.yining.org
mailto:      yining@zhangyining.net | zhang.yining@gmail.com
Fingerprint: 25C8 47AE 30D5 4C0D A4BB  8CF2 3C2D 585F A905 F033
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFIvsU8PC1YX6kF8DMRAk5NAKCJh/2C3xGHwgIsZDkJpeV9UmDQyQCfVAU1
nos9cAElZSsXHS/0zt1q0Uo=
=4LIA
-----END PGP SIGNATURE-----
Posted by Igor Sysoev (Guest)
on 03.09.2008 20:13
(Received via mailing list)
On Wed, Sep 03, 2008 at 07:58:35PM +0400, Maxim Dounin wrote:

> Just quote the regex, i.e:
> 
> rewrite  "([0-9]{2})([0-9]{2})([0-9]{2})"  
> /path/to/photos/$1/$1$2/$1$2$3.png;
> 
> (single quotes will do the trick too).

Some addition on quotes: nginx treats single and double quotes similary,
the both quote types do not affect on variable evaluation, etc.

The different quotes are simply the way to use one characters inside
others:

 "some 'text'"
 'some "text"'
Posted by Chris Savery (Guest)
on 03.09.2008 20:27
(Received via mailing list)
Thanks Zhang.
I was just wondering as I'm building a photo site as well and expect the
number of images to grow over time to be large. I also have directories
for s/ m/ p/ l/ f/ - and was thinking I may split into subdirectories
but just wasn't sure it made any difference. I'd be interested to know
if you ever find info about that. I do know that some command line tools
are hard to use with large numbers of files but I didn't really expect
to have to go in manually to browse them. Also my files are replicated
up onto Amazon S3.
Chris :)
Posted by mike (Guest)
on 03.09.2008 22:37
(Received via mailing list)
On Wed, Sep 3, 2008 at 10:11 AM, Zhang Yining <zhang.yining@gmail.com> 
wrote:

> First of all, I simplified the question a little, all url's are actually
> ended with "-(L|M|S|Q|T)" denoting different sizes (large, medium,
> small, square, thumbnail), so that's multiplication by five :-)

ha! i am doing the same thing, but _l _m _s _sq and a copy of the 
original :)