Bird
Raised Fist0
Nginxdevops~10 mins

IP hash for session persistence in Nginx - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - IP hash for session persistence
Client sends request
↓
Extract client IP
↓
Calculate hash from IP
↓
Map hash to backend server
↓
Forward request to selected server
↓
Server responds
↓
Client gets consistent server
The client IP is used to create a hash that decides which backend server handles the request, ensuring the same client goes to the same server.
Execution Sample
Nginx
upstream backend {
    ip_hash;
    server backend1.example.com;
    server backend2.example.com;
}

server {
    location / {
        proxy_pass http://backend;
    }
}
This config uses ip_hash to send requests from the same client IP to the same backend server for session persistence.
Process Table
StepClient IPHash CalculationSelected ServerAction
1192.168.1.10hash(192.168.1.10) = 1backend1.example.comRequest forwarded to backend1
2192.168.1.11hash(192.168.1.11) = 2backend2.example.comRequest forwarded to backend2
3192.168.1.10hash(192.168.1.10) = 1backend1.example.comRequest forwarded to backend1 (same as step 1)
4192.168.1.12hash(192.168.1.12) = 1backend1.example.comRequest forwarded to backend1
5192.168.1.11hash(192.168.1.11) = 2backend2.example.comRequest forwarded to backend2 (same as step 2)
6192.168.1.13hash(192.168.1.13) = 2backend2.example.comRequest forwarded to backend2
7192.168.1.14hash(192.168.1.14) = 1backend1.example.comRequest forwarded to backend1
8192.168.1.15hash(192.168.1.15) = 2backend2.example.comRequest forwarded to backend2
9192.168.1.10hash(192.168.1.10) = 1backend1.example.comRequest forwarded to backend1 (session persistence)
10192.168.1.16hash(192.168.1.16) = 1backend1.example.comRequest forwarded to backend1
💡 Requests continue to be routed based on client IP hash, ensuring session persistence.
Status Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7After 8After 9After 10
Client IP-192.168.1.10192.168.1.11192.168.1.10192.168.1.12192.168.1.11192.168.1.13192.168.1.14192.168.1.15192.168.1.10192.168.1.16
Hash Result-1211221211
Selected Server-backend1.example.combackend2.example.combackend1.example.combackend1.example.combackend2.example.combackend2.example.combackend1.example.combackend2.example.combackend1.example.combackend1.example.com
Key Moments - 3 Insights
Why does the same client IP always go to the same backend server?
Because the IP hash function maps the client IP to a specific backend server consistently, as shown in execution_table rows 1, 3, and 9 where the same IP 192.168.1.10 always selects backend1.
What happens if a new client IP appears?
The new IP is hashed and mapped to one of the backend servers based on the hash result, as in row 10 where 192.168.1.16 maps to backend1.
Can the distribution be uneven between servers?
Yes, because the hash depends on IP values, some servers may get more clients if IPs hash to them more often, as seen in variable_tracker where backend1 gets more selections.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5. Which server handles the request from IP 192.168.1.11?
Abackend3.example.com
Bbackend1.example.com
Cbackend2.example.com
DNo server selected
💡 Hint
Check the 'Selected Server' column at step 5 in the execution_table.
At which step does the client IP 192.168.1.10 appear for the third time?
AStep 9
BStep 3
CStep 7
DStep 10
💡 Hint
Look at the 'Client IP' column in the execution_table and count occurrences of 192.168.1.10.
If a new backend server is added, how would the hash mapping change?
AAll client IPs would map to the new server only
BThe hash function would distribute IPs among all servers including the new one
CThe existing mapping would not change
DThe IP hash would stop working
💡 Hint
Consider how hashing distributes keys among available servers as shown in the concept_flow.
Concept Snapshot
ip_hash directive in nginx upstream block
- Uses client IP to calculate hash
- Routes requests from same IP to same backend
- Helps keep session persistence
- Simple config: ip_hash; inside upstream
- Works best with stable client IPs
- May cause uneven load distribution
Full Transcript
This visual execution shows how nginx uses the ip_hash method to keep session persistence. When a client sends a request, nginx extracts the client IP and calculates a hash value from it. This hash determines which backend server will handle the request. The same client IP always maps to the same backend server, ensuring the session stays on one server. The execution table traces multiple requests from different IPs, showing consistent server selection for repeated IPs. The variable tracker shows how client IPs, hash results, and selected servers change step-by-step. Key moments clarify why the same IP maps to the same server and what happens when new IPs or servers appear. The quiz tests understanding of server selection and hash behavior. The snapshot summarizes the ip_hash directive usage and benefits.

Practice

(1/5)
1. What is the main purpose of using ip_hash in an Nginx upstream block?
easy
A. To ensure requests from the same client IP go to the same backend server
B. To encrypt traffic between Nginx and backend servers
C. To balance load evenly regardless of client IP
D. To cache static files on the client side

Solution

  1. Step 1: Understand the role of ip_hash

    The ip_hash directive in Nginx is used to route requests from the same client IP address to the same backend server.
  2. Step 2: Identify the purpose in session persistence

    This helps keep user sessions sticky, meaning the user stays connected to the same server, which is important for apps storing session data locally.
  3. Final Answer:

    To ensure requests from the same client IP go to the same backend server -> Option A
  4. Quick Check:

    IP hash = session stickiness [OK]
Hint: ip_hash keeps same IP on same server for session stickiness [OK]
Common Mistakes:
  • Confusing ip_hash with encryption
  • Thinking ip_hash balances load evenly
  • Assuming ip_hash caches files
2. Which of the following is the correct way to enable IP hash in an Nginx configuration for upstream servers?
easy
A. upstream backend { hash_ip; server 192.168.1.10; server 192.168.1.11; }
B. upstream backend { iphash; server 192.168.1.10; server 192.168.1.11; }
C. upstream backend { ip_hash; server 192.168.1.10; server 192.168.1.11; }
D. upstream backend { ip_hash = on; server 192.168.1.10; server 192.168.1.11; }

Solution

  1. Step 1: Recall the exact directive syntax

    The correct directive to enable IP hash in Nginx is ip_hash; placed inside the upstream block without any assignment or spelling changes.
  2. Step 2: Check each option for syntax correctness

    upstream backend { ip_hash; server 192.168.1.10; server 192.168.1.11; } uses ip_hash; correctly. Options A and C have wrong directive names. upstream backend { ip_hash = on; server 192.168.1.10; server 192.168.1.11; } uses an invalid assignment syntax.
  3. Final Answer:

    upstream backend { ip_hash; server 192.168.1.10; server 192.168.1.11; } -> Option C
  4. Quick Check:

    Correct directive is ip_hash; [OK]
Hint: Use exact directive 'ip_hash;' inside upstream block [OK]
Common Mistakes:
  • Misspelling ip_hash as iphash or hash_ip
  • Adding = on which is invalid
  • Placing ip_hash outside upstream block
3. Given this Nginx upstream configuration with ip_hash; enabled, what will happen if two requests come from the same client IP?
medium
A. Both requests will be sent to different backend servers randomly
B. Both requests will be sent to the same backend server
C. Requests will be load balanced based on server load
D. Requests will be rejected due to IP conflict

Solution

  1. Step 1: Understand ip_hash behavior on requests

    With ip_hash; enabled, Nginx uses the client's IP address to decide which backend server to send the request to, ensuring the same IP always maps to the same server.
  2. Step 2: Apply this to two requests from the same IP

    Since both requests come from the same IP, they will be routed to the same backend server for session persistence.
  3. Final Answer:

    Both requests will be sent to the same backend server -> Option B
  4. Quick Check:

    Same IP = same server [OK]
Hint: Same IP means same backend server with ip_hash [OK]
Common Mistakes:
  • Assuming random distribution despite ip_hash
  • Thinking load balancing ignores ip_hash
  • Believing requests get rejected on IP reuse
4. You added ip_hash; inside your upstream block, but session stickiness is not working. Which of these is the most likely cause?
medium
A. You placed ip_hash; inside the server block instead of upstream
B. You forgot to restart Nginx after adding ip_hash;
C. Your backend servers do not support IP hashing
D. You used ip_hash on; instead of ip_hash;

Solution

  1. Step 1: Verify correct placement of ip_hash;

    The ip_hash; directive must be inside the upstream block, not inside individual server blocks.
  2. Step 2: Analyze other options

    Restarting Nginx is necessary but usually not the main cause. Backend servers don't affect ip_hash behavior. Using ip_hash on; is invalid syntax.
  3. Final Answer:

    You placed ip_hash; inside the server block instead of upstream -> Option A
  4. Quick Check:

    ip_hash must be inside upstream block [OK]
Hint: ip_hash belongs inside upstream, not server block [OK]
Common Mistakes:
  • Putting ip_hash in server block
  • Using wrong syntax like ip_hash on;
  • Blaming backend servers for ip_hash issues
5. You have three backend servers and want to use IP hash for session persistence. However, one server is down. What will Nginx do with requests from an IP previously mapped to the down server?
hard
A. Nginx will randomly distribute requests ignoring IP hash
B. Nginx will keep sending requests to the down server causing errors
C. Nginx will reject the requests with a 503 error
D. Nginx will send requests to a different available backend server

Solution

  1. Step 1: Understand ip_hash behavior with unavailable servers

    When a backend server is down, Nginx detects it as unavailable and will not send requests to it, even if IP hash maps to it.
  2. Step 2: Determine fallback behavior

    Nginx will route requests to another available backend server to keep service running, though session stickiness is temporarily lost for that IP.
  3. Final Answer:

    Nginx will send requests to a different available backend server -> Option D
  4. Quick Check:

    Down server skipped, requests go to available servers [OK]
Hint: Down servers skipped; requests go to other servers [OK]
Common Mistakes:
  • Assuming Nginx keeps sending to down server
  • Thinking requests get rejected automatically
  • Believing ip_hash ignores server health
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard