관리 메뉴

공부 기록장 💻

[AWS] Elastic Beanstalk 502 Bad Gateway 해결하기 본문

# Tech Studies/AWS

[AWS] Elastic Beanstalk 502 Bad Gateway 해결하기

dream_for 2023. 5. 8. 21:44

문제 상황

 

 

AWS에서 Elastic Beanstalk 환경을 구성하여 Spring Boot 프로젝트를 배포한 후, 

배포된 URL로 접속했을 때 아래와 같이 502 Bad Gateway 오류가 나타나는 문제가 발생했다.

 

 

 

HTTP 502 상태 코드 (Bad Gateway)는 CloudFront가 요청한 리소스들을 서빙할 수 없음을 나타내는 에러 코드인데 왜 발생하는 것일까?

 

원인 파악

아래 스택 오버플로에 명쾌한 답이 나와있었는데,

ELB의 로드 밸런서의 nginx 가 기본적으로 포트 5000을 가리키고 있는 것이 원인이었다.

 

https://stackoverflow.com/questions/54612962/502-bad-gateway-elastic-beanstalk-spring-boot

 

502 bad gateway Elastic Beanstalk Spring Boot

I deployed a Spring Boot app on AWS Elastic Beanstalk. I am facing a 502 Bad Gateway error. I cannot find anything useful from the logs /var/log/nginx/error.log 2019/02/10 02:12:54 [error] 3257...

stackoverflow.com

 

애플리케이션 로드 밸런서가 기본적으로 EC2 인스턴스에서 nginx 서버의 80번 포트를 가리키고,

nginx가 기본적으로 포트 5000의 요청을 전달하도록 구성되어 있기 때문에 발생하는 문제이다.

 

 

아래 AWS 공식 문서를 통해 Elastic Beanstalk의 구성을  면밀히 살펴보면 그 원인을 보다 더 이해할 수 있게 된다.

 

https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/java-se-nginx.html

 

Elastic Beanstalk은 기본적으로 애플리케이션을 Elastic Load Banacling 로드 밸런서와 매핑을 하는데 nginx 웹 서버를 리벅스 프록시로 사용하며, 이때 포트 80으로 매핑을 하게 된다.

(ELB는 nginx의 기본 구성을 제공을 하지만, 물론 사용자는 AWS 내에서 이 구성을 오버라이딩하거나 확장할 수 있다.)

 

그리고 기본적으로 Elastic Beanstalk은 요청을 포트 5000에서 애플리케이션으로 전달하도록 nginx 프록시를 구성하며, AWS 환경 PORT 속성을 기본 애플리케이션이 수신 대기하는 포트로 설정하여, 기본 포트를 재정의할 수 있다고 한다.

 

 

아래 이미지는 위의 스택오버플로우에 나와 있는 예시로, ELB 의 Elastic Load Balancer와 nginx가 향하는 포트 구성을 잘 나타내고 있는 것 같아 가져와보았다.

 

 

 

 

이는 Spring Boot 공식 문서에서도 AWS의 Elastic Beanstalk을 통한 배포 과정에서,

Java SE Platform을 사용하는 경우 서버 포트 번호를 5000으로 설정해주어야 한다고 나타나 있다.

 

https://docs.spring.io/spring-boot/docs/current/reference/html/deployment.html

 

 

그러나 Spring 프로젝트 내에서 서버 포트 번호를 설정하는 것은 이전에 시도했던 방법이다.

그럼에도 502 Bad Gateway 에러가 발생였고, 결국 Elastic Beanstalk 환경 속성에 사용하는 SERVER_PORT를 추가하여 해결하였다.

 

 

문제 해결

 

 

 

 

아래와 같이 환경 속성에 SERVER_PORT 에 값 5000을 추가해주었다.

 

 

 

 

 

 

https://stackoverflow.com/questions/54612962/502-bad-gateway-elastic-beanstalk-spring-boot

 

502 bad gateway Elastic Beanstalk Spring Boot

I deployed a Spring Boot app on AWS Elastic Beanstalk. I am facing a 502 Bad Gateway error. I cannot find anything useful from the logs /var/log/nginx/error.log 2019/02/10 02:12:54 [error] 3257...

stackoverflow.com

https://bobcares.com/blog/aws-beanstalk-bad-gateway/#:~:text=Bad%20Gateway%20In%20AWS%20Beanstalk,with%20which%20it%20is%20communicating.

 

AWS Beanstalk Bad Gateway | How To Fix It?

Today, we will see how to troubleshoot a bad gateway error in AWS Elastic Beanstalk. We will also see a simple solution. More...

bobcares.com

 

 

728x90
반응형
Comments