dev.Log

[Nginx] connect() failed (113: No route to host) while connecting to upstream, 본문

BACKEND.*/Server

[Nginx] connect() failed (113: No route to host) while connecting to upstream,

초코푸딩 2024. 4. 9. 16:46

갑자기 502 리턴하는 장애발생!

 

[요약]

✅ AWS ELB의 동적 IP 변경 문제로 인해 connect() failed (113: No route to host) 오류 발생
Nginx에서 resolver 설정을 추가하여 주기적으로 DNS 조회
✅ resolver 8.8.8.8 valid=30s; → 30초마다 IP를 갱신하여 최신 ELB IP를 반영

      (구글 DNS를 사용하여 NGINX가 도메인을 해석하고, 30초 동안 캐싱)
✅ server test.com resolve; → proxy_pass 실행 시마다 최신 DNS 조회
✅ AWS 내부 DNS를 사용하면 성능 향상 가능 


 

원인 :

AWS ELB는 트래픽 등에 의해 동적으로 ip들이 늘어나거나 줄어드는데

nginx 가 설정파일을 읽는 시점에 도메인의 ip를 resolve 하기 때문에 AWS ELB 의 동적인 ip 변경이 일어날 경우 nginx가 주소를 찾지못해서 connect() failed (113: No route to host) while connecting to upstream 이런 에러가 나게 된다. 

proxy_pass https://test.com

 

 

해결 방법 : 

proxy_pass 에 도메인 직접입력이 아니라 변수로 할당하게 되면

nginx 는 resolver dns를 참조하여 매번 ip를 찾아가기 때문에 문제를 해결할 수 있다.

 resolver를 통해 nginx가 어떤 DNS 서버를 사용할지를 정할 수 있다.

http {
    resolver 8.8.8.8 valid=30s;  # 30초마다 DNS 캐시 갱신 (Google DNS 사용->AWS DNS 확인해서 변경)
    resolver_timeout 5s;  # DNS 조회 타임아웃 (기본값: 30초)

    upstream backend {
        server test.com resolve;  # DNS를 주기적으로 조회
    }

    server {
        listen 80;

        location / {
            proxy_pass https://test.com;
            proxy_set_header Host $host;
        }
    }
}

 

https://wooono.tistory.com/685

 

[Nginx] resolver란?

resolver란? DNS 서버는, 도메인에 따른 실제 IP 주소를 반환해주는 역할을 합니다. nginx config 파일 내부에 특정 도메인을 작성했다면, DNS 서버를 통해 해당 도메인이 실제로 어떤 IP 주소를 가지고 있

wooono.tistory.com

 

https://circlee7.medium.com/nginx-proxy-pass-%EC%9D%98-aws-elb-%EC%97%B0%EA%B2%B0-%EC%84%A4%EC%A0%95-f0c4b792ef71

 

Nginx proxy_pass 의 AWS ELB 연결 문제

어느날 nginx 의 proxy_pass 에서pending이 걸렸다. (두둥)

circlee7.medium.com

 

 

'BACKEND.* > Server' 카테고리의 다른 글

CPU 사양보는법 (lscpu)  (1) 2024.05.06
SSE (Server-Sent-Events)  (0) 2024.04.09
도커 MySQL 컨테이너 생성 및 실행  (0) 2022.10.28
도커  (1) 2022.10.28
보안 공부 사이트  (1) 2022.09.01
Comments