Variable value is resetting

Maxim Dounin mdounin at
Wed Nov 10 20:00:06 UTC 2021


On Wed, Nov 10, 2021 at 01:12:22PM -0500, malzad wrote:

> I have a static int variable in a load balancing module and initially set it
> to 0. I want to update this variable when there will be any HTTP requests
> with certain criteria.  It gets updated but whenever the module receives a
> new HTTP request ( or after receiving a few HTTP requests), that int
> variable again resets to 0. how can I prevent it from resetting the value?

Static variables in C are initialized to 0 on process creation, 
and that's all, there is nothing that can reset the variable to 0 
except your own code.

Note though that there can be multiple worker processes in nginx, 
and if you see the variable being 0 after you've updated it in a 
previous request, likely this means that you are looking at the 
variable in the different worker processes.  To fix this, you have 
to re-think your code with multiple worker processes in mind.  
Depending on what you are trying to do, using a shared memory or 
writing logs might be a way to go.

Maxim Dounin

More information about the nginx mailing list