session persistance with IP hash

Reinis Rozitis r at
Wed Jul 27 20:44:00 UTC 2016

> My understanding is that the ip_hash directive is responsible for session 
> persistence. My questions are:
> 1) How long does it last? For example, if I connect and my ip hash tells 
> nginx to connect to backend3, will my source IP be forever tied to backend 
> 3?

If your IP doesn't change and all the servers are up, you will most likely 
always land on the same backend server.

> 2) Is there another way to acheive session persistence other than ip hash 
> and other than purchasing plus edition?

Yes, you can use different hash mechanisms/keys (for example specific 
cookies etc):

There are also some third-party sticky cookie modules.

I personally preffer to use centralised cache storage (memcache/redis etc) 
so it doesn't matter on which server the user lands and they can be used in 
roundrobin fashion.

> 3) Is there an ip hash "cache" or something I can clean out periodically 
> to force the source IP to get a new hash and therefore a chance to connect 
> to a different server?

The standard nginx hash mechanisms don't have any inbuilt "cache". In case 
of ip_hash you can change the upstream server order (though it will swap 
arround all the users not only particular remote addr).


More information about the nginx mailing list