Nginx 의 resolver 를 이용 하여 동적 upstream 을 실현 합 니 다.

6162 단어 프로 그래 밍
이전에 글 을 써 서 openresty 로 동적 인 길 을 만 들 었 습 니 다. 동적 이 라 고 하지만 사실은 upstream 을 설정 파일 에 잘 써 야 합 니 다. 정적 인 것 과 같 습 니 다.
최근 작업 중 에 이러한 수요 가 있 습 니 다. upstream 은 완전 동태 이 고 클 라 이언 트 가 지정 합 니 다. 시작 할 때 오류 가 있 었 습 니 다. 마지막 으로 resolver 지정 dns 서 비 스 를 통 해 이 루어 졌 습 니 다. 구체 적 인 절 차 는 다음 과 같 습 니 다.
1. 구덩이 밟 기 시작
nginx 의 설정 은 다음 과 같 습 니 다:
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    server {
        listen       8001;
        server_name  localhost;

        location / {
                set $upstream_host $http_upstream_host;
                echo $http_upstream_host;
                proxy_pass http://$upstream_host;
        }

    }

}

내 가 이런 요청 을 보 낼 때:
curl "127.0.0.1:8001/" -H "upstream-host:www.baidu.com" -v

다음 과 같은 잘못 을 보고 했다.
access.log:
127.0.0.1 - - [14/Sep/2017:23:37:10 +0800] "GET / HTTP/1.1" 502 179 "-" "curl/7.29.0"

error.log:
2017/09/14 23:38:31 [error] 25307#25307: *48 no resolver defined to resolve www.baidu.com, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", host: "127.0.0.1:8001"

이 문 제 는 지정 되 지 않 은 resolver 사용자 정의 upstream www.baidu.com 임 이 분명 하 다.
2. resolver 정의 도 메 인 이름 분석 사용
수 정 된 nginx 설정 은 다음 과 같 습 니 다.
worker_processes  1;


events {
    worker_connections  1024;
}

http {
    resolver 114.114.114.114;
    server {
        listen       8001;
        server_name  localhost;

        location / {
                set $upstream_host $http_upstream_host;
                echo $http_upstream_host;
                proxy_pass http://$upstream_host;
        }

    }

}

이제 요청 은 모두 정상 200 입 니 다.
req:
curl "127.0.0.1:8001/" -H "upstream-host:www.baidu.com" -voa
curl "127.0.0.1:8001/" -H "upstream-host:www.qq.com" -voa

access.log:
127.0.0.1 - - [14/Sep/2017:23:44:01 +0800] "GET / HTTP/1.1" 200 2381 "-" "curl/7.29.0"
127.0.0.1 - - [14/Sep/2017:23:44:07 +0800] "GET / HTTP/1.1" 200 244182 "-" "curl/7.29.0"

3. resolver 사용 설명
resolver 의 문법 은 다음 과 같 습 니 다.
Syntax: resolver address ... [valid=time] [ipv6=on|off];
Default:    —
Context:    http, server, location

여러 개의 dns 서 비 스 를 설정 할 수 있 습 니 다. nginx 는 dns 서 비 스 를 방문 하 는 폴 링 방식 으로 dns 서 비 스 를 방문 합 니 다. nginx 는 dns 가 도 메 인 이름 을 분석 한 결 과 를 캐 시 합 니 다. 캐 시 시간 은 valid 에서 지정 합 니 다. ipv 6 는 ipv 6 를 켜 거나 닫 는 데 사 용 됩 니 다.
Syntax: resolver_timeout time;
Default:    
resolver_timeout 30s;
Context:    http, server, location
resolver_timeout dns 해석 의 시간 초과 시간 을 지정 합 니 다.
4. ref
http://nginx.org/en/docs/http/ngx_http_core_module.html#resolver

좋은 웹페이지 즐겨찾기