Servers, nginx and Docker · Troubleshooting walkthrough
ALB targets keep switching between healthy and unhealthy
Short answer
ALB targets that flip between healthy and unhealthy are failing some health checks, not all. The reason code says why: Target.Timeout means the health path answers slower than HealthCheckTimeoutSeconds (5 seconds by default), and Target.ResponseCodeMismatch means it returned a code outside the matcher, often a redirect. Point the check at a fast /health route that always returns 200.
A target that flaps is failing some health checks and passing others. The load balancer counts consecutive results: by default two failed checks in a row mark a target unhealthy and five passes in a row bring it back. So a health page that is slow or wrong only now and then produces exactly this on-off pattern, and while a target is out, the others take its traffic and users may see 5xx errors.
The target's own reason code tells you which of two families you are in: the page is too slow (Target.Timeout) or it answers with a code the target group does not count as success (Target.ResponseCodeMismatch). Read the reason first, then change the health route or the target group, in that order.
The text you see
Target.TimeoutRequest timed outTarget.ResponseCodeMismatchHealth checks failed with these codes: [302]
Stage 1
Read the reason code while it is happening
The console summary says only "unhealthy". The API gives a reason and a description for each target, so run this several times, because a flapping target may look healthy in any single run.
State, reason and description per target
aws elbv2 describe-target-health \
--target-group-arn arn:aws:elasticloadbalancing:us-east-1:111122223333:targetgroup/my-targets/73e2d6bc24d8a067 \
--query 'TargetHealthDescriptions[].[Target.Id,TargetHealth.State,TargetHealth.Reason,TargetHealth.Description]' \
--output table| Reason shown | What it means | Go to |
|---|---|---|
| Target.Timeout | No answer inside the health check timeout | Slow health route |
| Target.ResponseCodeMismatch | An answer came back, but its status code is not in the matcher | Wrong status code |
| Target.FailedHealthChecks | The check could not complete at all, for example a refused connection | Network and listener |
Also look at how often it flips. A target that flaps every few minutes points at load or a slow dependency; one that is down for hours points at a crashed process or a blocked path.
Stage 2
A health route that is slower than the timeout
For instance and IP targets the default health check timeout is 5 seconds and the interval is 30. If the health path renders a full page, opens a database connection or waits on another service, it will usually answer in time and now and then it will not. Two misses in a row and the target leaves service; five good answers later it returns. That is the flap.
Time the path from a machine in the same network as the load balancer, using the same user agent the checker sends. Run it in a loop so you see the worst case, not an average.
- Add the light route in the application.
- Point the target group at it, with a timeout that leaves headroom.
- Run
describe-target-healthrepeatedly and watch theUnHealthyHostCountmetric for the target group.
Twenty timed requests to the health path
for i in $(seq 1 20); do
curl -s -o /dev/null -w '%{http_code} %{time_total}s\n' -A 'ELB-HealthChecker/2.0' http://10.0.0.10:8080/
done- Any line near the timeout, or a code other than 200, is your problem.
- Fix the route, not the clock: a dedicated
/healththat answers 200 without touching the database. - Raise the timeout only afterwards, and keep it below the interval. A slower check hides an overloaded instance; AWS suggests checking utilisation and adding targets when the instance is busy.
Stage 3
A status code the matcher refuses
Only codes inside the target group's matcher count as healthy, and the default for an ALB is 200. A web server that sends unknown hosts to the canonical domain, or forces HTTPS, answers the health check with 301 or 302. The check does not follow redirects, so the target fails with Target.ResponseCodeMismatch and a description such as Health checks failed with these codes: [302].
The request reaches the target with the target's private address in the Host header, not your public domain. Test with exactly that, because a virtual host that matches only your domain name will answer something else.
What the checker sees
curl -si -H 'Host: 10.0.0.10:8080' -A 'ELB-HealthChecker/2.0' http://10.0.0.10:8080/health | head -1- Redirect rule: exclude the health path from it, or give the server a default virtual host that answers 200.
- Login required: the health route must not need a session. A
401or403is a mismatch too. - App really returns another success code: add it to the matcher instead.
| Wrong fix | Why it does not help |
|---|---|
| Raising the timeout to 60 seconds | The answer is fast already; it is just the wrong code |
| Adding the load balancer address to an allow list | The checker connects to the target's private IP, and the code is still not 200 |
Stage 4
Change the target group and confirm it held
Put the health path, timeout, interval and matcher in one command so they change together. The matcher value is quoted because it contains a comma.
Health path, timing and matcher in one change
aws elbv2 modify-target-group \
--target-group-arn arn:aws:elasticloadbalancing:us-east-1:111122223333:targetgroup/my-targets/73e2d6bc24d8a067 \
--health-check-path /health \
--health-check-timeout-seconds 10 \
--health-check-interval-seconds 30 \
--matcher 'HttpCode="200,204"'Stage 5
When targets are never healthy, not flapping
If a target never enters service, the cause is usually not the route. AWS lists the security groups and network ACLs between the load balancer and the target as the first things to check: the target's security group must allow the health check port from the load balancer's security group. A flapping target, by contrast, can already be reached, which is why the page and the load deserve attention first.
Confirm the fix by watching for a longer time than one interval. Several consecutive healthy results across a few minutes, a flat UnHealthyHostCount and a drop in 5xx on the load balancer all say the same thing.
Repeat this a few times
aws elbv2 describe-target-health --target-group-arn arn:aws:elasticloadbalancing:us-east-1:111122223333:targetgroup/my-targets/73e2d6bc24d8a067 --query 'TargetHealthDescriptions[].TargetHealth.State'If you only keep one thing
- Read
Reasonfirst:Target.Timeoutmeans too slow,Target.ResponseCodeMismatchmeans the wrong code. - Give the target group a light
/healthroute with no login, redirect or database call. - Alarm on
UnHealthyHostCountabove 0 so flapping shows up before users report 5xx.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.