Allow 301 response in AWS Elastic Beanstalk

Seito Horiguchi
3 min readAug 31, 2022

--

Overview

The other day, I deployed a web app on AWS Elastic Beanstalk.
I thought I had wholly defeated it after battling several errors, but when I looked at the dashboard, the Health status was “Severe”, and something seemed wrong.

But when I look at the logs, there are no errors, I can access the domain…

I wondered what was wrong, so I contacted AWS support and got this answer.

From curl operation, we could see that response code is “301”. 
I would like to inform you that the Environment’s Load Balancer is expecting HTTP 200 as response code but the Application is returning HTTP 301.
Redirection configured on the backend can result in a 301 or 302 response code, resulting in failed health checks even if your application is working fine. Therefore, due to mismatch in the success code the health failing with”Target.ResponseCodeMismatch”[2]. As as result, health is showing as severe.

In short, the cause is that a 301 response is being returned and this is being recognized as some kind of error. If you make Elastic Beanstalk recognize that this response is not an error, you can solve the problem.
So I tried throwing a request with curl and got a response that looked good.

curl -Ivk {myAppDomain.ap-northeast-1.elasticbeanstalk.com}*   Trying xx.xxx.xxx.xx:80...
* Connected to {myAppDomain.ap-northeast-1.elasticbeanstalk.com} (xx.xxx.xxx.xx) port 80 (#0)
> HEAD / HTTP/1.1
> Host: {myAppDomain.ap-northeast-1.elasticbeanstalk.com}
> User-Agent: curl/7.79.1
> Accept: /
> Mark bundle as not supporting multiuse
< HTTP/1.1 301 Moved Permanently
HTTP/1.1 301 Moved Permanently

301 response means redirect. It seems to be affected because I have set up my own domain and configured HTTP to HTTPS redirects, etc.

Solution

You can configure the load balancer to accept 301 responses.
Specifically, you do the following

  1. Go to “Configuration”, “Load balancer”, and click “Edit”

2. Go to “Processes”, check the process of “Port: 80, Protocol: HTTP“, go to “Action” and click “Edit“

3. Add 301 to “HTTP code”; (If 200 is not present, add it as well. In that case, separate it with a comma without space, like 200,301.) Then press the Save button to close.

4. Finally, do not forget to press “Apply” and then “Contiune” at the bottom of the page. If you don’t do this, it will not be adopted. That’s all!

--

--

Seito Horiguchi

Hi, I'm a Web developer and Tech YouTuber ;) I mainly code JS, TS, and Python. https://www.linkedin.com/in/seito/