5

I have two separate Nginx machines. The domain uses round-robin DNS and has two A records, one for each of the two frontend Nginx server.

Both Nginx servers use the ip_hash directive and proxy requests to the same backend servers.

Will an IP address that goes to the first Nginx server always be routed to the same backend server as that same IP address that goes to the other Nginx server?

This may be of help: What IP does nginx use for ip_hash sticky sessions?

2
  • Just want to clarify. Are you using round-robin DNS to publish two A records? One goes to Nginx A and the other to Nginx B? May 29, 2013 at 19:00
  • @jeffatrackaid Yes, round robin DNS. May 29, 2013 at 19:04

2 Answers 2

2

The Nginx servers are unaware of each other with their own ip_hash tables. So a specific server will route traffic to the backend based on its own ip_hash pool. Any consistency between the frontend Nginx servers would be due to chance alone.

Now in practice, this may be less of an issue because the client's DNS is unlikely to change during their session. As a result, they will hit the same front-end server and be routed to the same backend server.

Another approach to this is to use something like HAProxy, which can balance on a number of metrics, e.g. as a URL parameter. Your application can set a parameter to assure the client gets routed to the same backend. Of course normalizing your data over your backends with a distributed tool is preferred but that can be challenging with legacy applications.

11
  • 2
    But if Nginx always uses the same method to hash ip addresses with no random variables, then they will do the same thing. Have you looked into the Nginx source code to determine this? May 29, 2013 at 19:14
  • Also what do you mean by the DNS comment. Doesn't round robin mean a request will go back and forth between A records? May 29, 2013 at 19:15
  • The hash may not be random but the assignment to the upstream backends would be different between the two systems based on traffic. May 29, 2013 at 19:24
  • Round robin will go back and forth but DNS record caching client-side often negates this behavior, even with short TTLs. If their systems cache DNS for 1 hour, then they will be using the same front-end server for that hour. May 29, 2013 at 19:25
  • Ahh ok. I didn't know about record caching. May 29, 2013 at 19:26
0

According to the docs:

The key for the hash is the class-C network address or the entire IPv6-address of the client.

So, in theory, this should work.

Another problem that could arise is when one of your backends fail. Nginx doesn't make any promises about what backend the client hits next.

But, yeah, HAProxy ftw.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .