Simple arithmetics in nginx config

Hello! I have an image folder called “i” in site’s root folder, it has
many subfolders like 0, 100, 200, 300 … 600000, etc…
So in folder “200” there are images from 200.jpg to 299.jpg, in folder
“300” there are 300.jpg to 399.jpg ans so on. As you can see i create a
new folder for every 100th image because i don’t want to have all the
images in one folder (there are too much).

The problem is that i can’t calculate a proper folder name, i tried like
this:

location ~ /i/(.*).jpg {
set $folder $1-($1%100);
try_files /i/$folder/$1.jpg =400;
}

but of course it does not work due to syntax error.

Help me please

Posted at Nginx Forum:

On Mon, Nov 28, 2011 at 4:11 PM, mennanov [email protected] wrote:

location ~ /i/(.*).jpg {
set $folder $1-($1%100);
try_files /i/$folder/$1.jpg =400;
}

but of course it does not work due to syntax error.

The standard “set” directive does not support arithmetic operations at
all. But it’s an ideal use case for our ngx_lua module:

location ~ /i/(.*)\.jpg {
   set_by_lua $folder 'return ngx.var[1] - (ngx.var[1] % 100)';
   try_files /i/$folder/$1.jpg =400;
}

See Lua | NGINX for details.

Regards,
-agentzh

agentzh Wrote:

error.
try_files /i/$folder/$1.jpg =400;
[email protected]
nginx Info Page

Thank you so much, i’ve just recompiled nginx with this module and it
just works!

Posted at Nginx Forum:

Hi!
I’ve recently wrote a simple module nginx-let-module for myself
providing arithmetic (and string op) support in NGINX.
it supports parentheses, hex, simple ops. here’s an example:

let $value ( $uid + 0x12 ) * $offset - 100 ;

it’s here

Posted at Nginx Forum: