Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Can you do simple math operations (add / subtract / division / multiplication) from within the nginx conf?

share|improve this question
Write a perl/ruby/shell script to generate your nginx.conf file..? – Steve Kemp Feb 16 '12 at 11:15
If you really want to do math, the 'Embedded Perl' module will let you include Perl code in your Nginx config. It does decrease performance a bit, but may be better than sending your files to PHP. – cyberx86 Feb 16 '12 at 12:23
up vote 3 down vote accepted

Nope - but why would you need to?

There's probably a better way to do what you're looking to do without going through the hassle of turning your web server config into an Frankenstein combination of configuration and application logic - can you expand on your goal?

share|improve this answer
I have some obfuscated legacy ids that I'd like to just forward to the correct place. As they are images it seems wasteful to be sending all of them to PHP just to do some math. Basically: --> /home/images/23.jpg . So it would be nice to just use an nginx alias to grab the right location rather than use the PHP. – Nik Feb 15 '12 at 6:13
Is it just a "grab the first two characters (or all but the last two) of the imageid", or is it more complex? If it's string manipulation that can be accomplished by regex, then it's definitely doable. – Shane Madden Feb 15 '12 at 6:19
some addition and division. btw, thanks for being so helpful :) – Nik Feb 15 '12 at 6:25
Unfortunately, no such luck then - doing the actual math is beyond the config file's capability. Sounds like a job for PHP ;) – Shane Madden Feb 15 '12 at 6:29

nginx has two special modules, embedded Perl and Lua. Theres already two variation for Lua, first and second one.

If compiled with these, Perl scripts or Lua scripts can be inserted into the configuration files of nginx, so not only "simple" math but nearly anything Perl or Lua is able to do will be available inside nginx conf ;)

share|improve this answer

Yes, if you build it with this module:

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.