Nginx 의 resolver 를 이용 하여 동적 upstream 을 실현 합 니 다.
6162 단어 프로 그래 밍
최근 작업 중 에 이러한 수요 가 있 습 니 다. 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
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Linux Shell 프로 그래 밍 - 텍스트 처리 grep, sed사용자 가 지정 한 '모드' 에 따라 대상 텍스트 를 일치 하 게 검사 하고 일치 하 는 줄 을 인쇄 합 니 다. ##포함 되 지 않 음, 역방향 일치 \ ##키워드 앞 뒤 가 맞지 않 고 키워드 만 일치 합 니 다...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.