본문 바로가기

Server Infra/Nginx

Nginx Proxy ELB 연동 관련 팁

728x90

간혹 Nginx 와 ELB 사이의 Connection이 간헐적으로 끊기는 현상이 있다.


해당 이슈의 에러 코드를 보면

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

위와 같은 에러가 발생하는데 이 현상은 ELB 의 IP가 주기적으로 변경되며 발생하는 Nginx의 Connect 에러이다.


따라서 해당 이슈 발생시 Nginx Restart하는 것 만으로 이슈가 해결되지만 그것은 임시적인 방법이고 그렇게 해결했다 하더라도 또 다시 에러가 발생하는 것을 볼 수 있다.


AWS 의 VPC는 항상 0.2로 끝나는 DNS 서버를 가지고 있다. VPC 생성시 정하는 CIDR이 172.32.0.0/16인 경우 DNS 는 172.32.0.2가 되는데


이 설정을 Nginx에 추가한다.

resolver 172.32.0.2 valid=10s;


여기서 주의할점은 proxy_pass를 변수로 정의해야 한다는 점 이다. 변수로 정의 하지 않으면 단 한번만 해결하며 그 이후에 같은 증상이 발생한다


최종 설정은 아래와 같다

server {
      keepalive_timeout 10;
      listen       80;
      location / {
                resolver 172.32.0.2 valid=10s;
                set $backend "http://[ELB URL]";
                proxy_pass $backend;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-Host $server_name;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
                proxy_set_header Host $http_host;
                proxy_set_header X-NginX-Proxy true;
                add_header 'Cache-Control' 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
                real_ip_header X-Forwarded-For;
                real_ip_recursive on;
                proxy_redirect off;
                expires off;
                access_log off;
                charset utf-8;
        }

뭔가..잡다한 설정은 많지만 resolver와 set, proxy_pass부분확인하시길


해당 설정을 하면 DNS를 통하여 커넥션을 하기에 발생되는 이슈 해결이 가능하다.


아래는 Suricata를 통한 패킷 모니터링이다. 위와 같은 설정시 아래와 같은 로그가 주기적으로 남는데 이는 DNS로부터 ELB의 아이피를 확인하기 때문. 별 문제는 없다.

Jul 30 22:49:11 ip-172-32-18-213 suricata[2991]: [1:2240008:2] SURICATA DNS flow memcap reached [Classification: (null)] [Priority: 3] {UDP} 172.32.18.213:33230 -> 172.32.0.2:53

Jul 30 22:49:11 ip-172-32-18-213 suricata[2991]: [1:2240008:2] SURICATA DNS flow memcap reached [Classification: (null)] [Priority: 3] {UDP} 172.32.18.213:33230 -> 172.32.0.2:53

Jul 30 22:49:11 ip-172-32-18-213 suricata[2991]: [1:2240008:2] SURICATA DNS flow memcap reached [Classification: (null)] [Priority: 3] {UDP} 172.32.18.213:33230 -> 172.32.0.2:53

Jul 30 22:49:11 ip-172-32-18-213 suricata[2991]: [1:2240008:2] SURICATA DNS flow memcap reached [Classification: (null)] [Priority: 3] {UDP} 172.32.18.213:33230 -> 172.32.0.2:53

Jul 30 22:49:11 ip-172-32-18-213 suricata[2991]: [1:2240001:1] SURICATA DNS Unsollicited response [Classification: (null)] [Priority: 3] {UDP} 172.32.0.2:53 -> 172.32.18.213:33230

Jul 30 22:49:11 ip-172-32-18-213 suricata[2991]: [1:2240001:1] SURICATA DNS Unsollicited response [Classification: (null)] [Priority: 3] {UDP} 172.32.0.2:53 -> 172.32.18.213:33230


728x90