Microsoft/Azure

Application Gateway - 502 Bad Gateway

megapain 2022. 1. 20. 23:34

[증상]

Application Gateway 구성 후 브라우저에서 접근하면 아래와 같이 502 Bad Gateway 오류가 발생하는 경우가 있습니다.

 

또, Application Gateway의 Overview에 

"All the instances in one or more of your backend pools are unhealthy. This will result in a 502 error when you try to access your application hosted behind the Application Gateway. Please check the backend health and resolve the issue." 라고 표시되고,

 

Backend healthStatus가 Unhealthy로 표시되고, Details에  

"Time taken by the backend to respond to application gateway's health probe is more than the time-out threshold in the probe configuration. Either increase the time-out threshold in the probe configuration or resolve the backend issues. Note: for default probe, the http timeout is 30s. To learn more visit - https://aka.ms/ProbeTimeOut." 라고 표시됩니다.

 

 

[원인]

이 문제는 Application Gateway에서 Backend server로 Health probe 요청을 보낸 후 지정된 시간 안에 응답을 받지 못한 경우 발생합니다. (Application Gateway에서 Backend server로 Health probe 상태 체크가 안되는 경우라고 볼 수 있습니다.)

https://docs.microsoft.com/en-us/azure/application-gateway/application-gateway-backend-health-troubleshooting#backend-server-timeout

 

보통 Backend Server의 NSG Inbound 규칙에 All deny 규칙이 적용되어 있는 경우에 이와 같은 증상이 발생합니다.

 

 

[해결 방법]

NSG Inbound 규칙에 Application Gateway의 Subnet 대역에서 Backend Server의 IP 주소 (또는 Subnet 대역)로 허용하는 규칙을 설정합니다.

예를 들어 Backend Server에서 HTTP (TCP 80)을 서비스한다면, 아래와 같이 NSG Inbound 규칙을 설정합니다.

Source Destination Protocol Port
ApplicationGateway Subnet CIDR range Backend Server IP Addresses TCP 80

 

위와 같이 NSG 규칙을 설정했는데도 여전히 동일한 문제가 발생한다면 Custom Health probe를 설정해야 할 수도 있습니다.

 

Backend Server의 Web Server 설정에서 host name을 지정했는지 확인해봅니다.

예를 들어, Backend Server가 Windows IIS라면 아래와 같이 Host Name을 지정했을 수도 있습니다.

 

Backend Server에서 host name이 지정되었다면 Custom Health probe를 추가해 주어야 합니다.

Custom Health probe를 추가할 때, Host에 Backend Server에서 지정한 host name을 입력하고, Http Settings에서 대상 HTTP settings를 선택합니다. Health probe가 체크할 Path도 올바르게 지정합니다.

[Test]를 클릭하여 Health probe가 정상적으로 동작하는지 확인해 볼 수 있습니다.

 

Health probe가 정상적으로 동작한다면 Backend health에서 Status가 Healthy로 표시되고, 

 

Application Gateway를 통해 Backend Server로 정상적으로 접속됩니다.

 

-끝-