0
0
Nginxdevops~20 mins

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

Choose your learning style9 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?