Bird
Raised Fist0
Nginxdevops~20 mins

IP hash for session persistence in Nginx - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
IP Hash Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
How does IP hash maintain session persistence in NGINX?

Which statement best describes how the ip_hash directive in NGINX helps maintain session persistence?

AIt uses the client's IP address to consistently route requests to the same backend server.
BIt stores session data on the NGINX server to track user sessions.
CIt randomly distributes requests to backend servers to balance load evenly.
DIt uses cookies to identify and route users to backend servers.
Attempts:
2 left
💡 Hint

Think about what information NGINX can use without storing session data.

💻 Command Output
intermediate
1:30remaining
NGINX configuration with ip_hash output

Given this NGINX upstream configuration, what will be the output of nginx -t?

Nginx
upstream backend {
    ip_hash;
    server backend1.example.com;
    server backend2.example.com;
}

server {
    listen 80;
    location / {
        proxy_pass http://backend;
    }
}
Anginx: [emerg] unknown directive "ip_hash" in /etc/nginx/nginx.conf:2
Bnginx: [warn] server backend1.example.com is down
C
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Dnginx: [error] duplicate upstream name "backend" in /etc/nginx/nginx.conf:1
Attempts:
2 left
💡 Hint

Check if the ip_hash directive is valid in upstream context.

❓ Troubleshoot
advanced
2:00remaining
Why does session persistence fail with ip_hash in this setup?

After enabling ip_hash in NGINX, users report sessions are not sticky and requests go to different backend servers. What is the most likely cause?

AThe client IP is hidden behind a proxy or load balancer, so NGINX sees the same IP for all clients.
BThe <code>proxy_pass</code> directive is missing in the server block.
CThe <code>ip_hash</code> directive requires a cookie to work properly.
DThe backend servers have different IP addresses, causing inconsistent hashing.
Attempts:
2 left
💡 Hint

Consider how NGINX sees client IPs when behind another proxy.

🔀 Workflow
advanced
2:00remaining
Order the steps to enable IP hash session persistence in NGINX

Arrange the steps in the correct order to enable IP hash session persistence for a backend in NGINX.

A3,1,2,4
B2,1,3,4
C1,3,2,4
D1,2,3,4
Attempts:
2 left
💡 Hint

Think about defining backend servers before applying ip_hash and proxying.

✅ Best Practice
expert
2:30remaining
Best practice to handle client IPs behind proxies for ip_hash

When using ip_hash in NGINX behind a reverse proxy or load balancer, what is the best practice to ensure session persistence works correctly?

ASet the <code>proxy_pass</code> directive to use the proxy's IP address.
BConfigure NGINX to use the <code>X-Forwarded-For</code> header with the <code>real_ip_module</code> to get the actual client IP.
CDisable <code>ip_hash</code> and use random load balancing instead.
DUse sticky cookies instead of <code>ip_hash</code> without any additional configuration.
Attempts:
2 left
💡 Hint

How can NGINX know the real client IP when behind another proxy?

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