AWS Load Balancer ERR_TOO_MANY_REDIRECTS

ETFairfax picture ETFairfax · Mar 10, 2017 · Viewed 11.4k times · Source

I've been dipping into AWS for the 1st time am bit stuck with a problem trying to set up a load balancer (ELB).

So far I have used ECS to create 2 EC2 instances that are running a container each with an app listening on port 3000.

For each of the instances I am able to browse to their IPv4 Public IPs specifying port 3000 and get to the containerised app. I am able to log in and use the app as expected.

So I thought the right thing to do next is set-up an ELB which would not only balance the load(!) but also handle port forwarding.

The ELB has a port 80 Listener, and I have a Target Group in which I have registered my ECS instances on port 3000.

I have then popped the ELBs DNS name (i.e. my-load-balancer-123456789.eu-west-1.elb.amazonaws.com) into my browser and was presented with the logon page of my app.

All good until I actually log on. I am then presented with the error message:

ERR_TOO_MANY_REDIRECTS: my-load-balancer-123456789.eu-west-1.elb.amazonaws.com redirected you too many times.

I have 2 questions

1: Why is the redirect loop happening?

2: Are there any diagnostic tools that I should know about which would help me with problems like this in the future?

Update: I have tried clearing all my browser cookies btw.

Any help appreciated.

Answer

Jeremy picture Jeremy · Oct 1, 2018

This issue is pretty common when you have redirects being done by the server itself. AWS has a guide for how to prevent these issues.

The following leads to an infinite loop of redirection between the load balancer and the backend web server:

  • The rewrite rule on the web server for directing HTTP requests to HTTPS forces requests to use port 443 for HTTPS traffic on the load balancer.
  • The load balancer still sends requests to the backend web server on port 80.
  • The backend web server redirects requests to port 443 on the load balancer.

The error ERR_TOO_MANY_REDIRECTS is returned, and the requests are never served.

To resolve this, change your web server’s rewrite rule using the X-Forwarded-Proto header of the HTTP request to apply only if the client protocol is HTTP. Ignore the rewrite rule for all other protocols used by the client.

Note: If you're using Application Load Balancers, use redirect actions to redirect traffic instead.