# Simple arithmetics in nginx config

**URL:** <https://www.ruby-forum.com/t/simple-arithmetics-in-nginx-config/214308>\
**Category:** NGINX\
**Created:** [November 28, 2011, 9:12am UTC](https://www.ruby-forum.com/t/simple-arithmetics-in-nginx-config/214308 "2011-11-28T09:12:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [November 28, 2011, 9:12am UTC](https://www.ruby-forum.com/t/simple-arithmetics-in-nginx-config/214308/1 "2011-11-28T09:12:29Z")

</div>

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:

> **[Simple arithmetics in nginx config](https://forum.nginx.org/read.php?2,219172,219172#msg-219172)**
>
> 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...

---

<div class="post-metadata">

**Author:** ![mennanov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/45deac/32.png) [@mennanov](https://www.ruby-forum.com/u/mennanov)\
**Post date:** [November 28, 2011, 9:33am UTC](https://www.ruby-forum.com/t/simple-arithmetics-in-nginx-config/214308/2 "2011-11-28T09:33:07Z")

</div>

On Mon, Nov 28, 2011 at 4:11 PM, mennanov [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/HttpLuaModule) for details.

Regards,  
-agentzh

---

<div class="post-metadata">

**Author:** ![mennanov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/45deac/32.png) [@mennanov](https://www.ruby-forum.com/u/mennanov)\
**Post date:** [November 29, 2011, 9:36am UTC](https://www.ruby-forum.com/t/simple-arithmetics-in-nginx-config/214308/3 "2011-11-29T09:36:29Z")

</div>

## agentzh Wrote:

> error.  
> try\_files /i/$folder/$1.jpg =400;  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://mailman.nginx.org/mailman/listinfo/nginx)

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

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,219172,219260#msg-219260](http://forum.nginx.org/read.php?2,219172,219260#msg-219260)

---

<div class="post-metadata">

**Author:** ![mennanov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/45deac/32.png) [@mennanov](https://www.ruby-forum.com/u/mennanov)\
**Post date:** [December 5, 2011, 11:28am UTC](https://www.ruby-forum.com/t/simple-arithmetics-in-nginx-config/214308/4 "2011-12-05T11:28:07Z")

</div>

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

> **[GitHub - arut/nginx-let-module: Provides support for arithmetic and string...](https://github.com/arut/nginx-let-module)**
>
> Provides support for arithmetic and string operations in NGINX config - GitHub - arut/nginx-let-module: Provides support for arithmetic and string operations in NGINX config

Posted at Nginx Forum:

> **[Re: Simple arithmetics in nginx config](https://forum.nginx.org/read.php?2,219172,219621#msg-219621)**
>
> 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...
