Home  >  Q&A  >  body text

How to perform division operation on variables in nginx?

For example, the requested url is

photos/100000_0_9-.jpg

I want to calculate the number 100000 and divide it

location ~ /photos/([0-9]+)_0_([0-9]+)-.jpg$ {
    root '/home/images';
    set $id /10000;
    set $version ;
    rewrite /photos/([0-9]+)_0_([0-9]+)-.jpg$ /$id/$version/_0_-.jpg;
}

Then rewrite to the directory corresponding to the id, but if you do this, the value of id will become 100000/10000 instead of 10.

How to implement this division operation in nginx?

怪我咯怪我咯2712 days ago737

reply all(1)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-05-16 17:32:00

    The Nginx configuration file does not seem to support data science operations. You can use the HttpLuaModule written by agentzh.

    location ~ /photos/([0-9]+)_0_([0-9]+)-.jpg$ {
        root '/home/images';
        set $id ;
        set $version ;
        set_by_lua $id
                   "return math.floor(tonumber(ngx.arg[1])/10000)"
                    $id;
        rewrite /photos/([0-9]+)_0_([0-9]+)-.jpg$ /$id/$version/_0_-.jpg;
    }

    You can also write a simple mathematical operation extension in bytes https://github.com/arut/nginx-let-mod....

    reply
    0
  • Cancelreply