Recently, there has been a need for this aspect in my work, upstream is completely dynamic, specified by the client, there are some errors at the beginning, and finally the DNS service is specified through the resolver, the specific process is as follows.
1. Start stepping on the pit
The configuration of nginx is as follows:
When I send this kind of request:
The following error was reported:
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 resolvewww.baidu.com, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", host: "127.0.0.1:8001" It is clear that this issue states that not specifying a resolver causes custom upstreamwww.baidu.com.
2. Use Resolver to define domain name resolution
The modified nginx configuration is as follows:
Now the requests are normal 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 instructions
The syntax for resolver is as follows:
Syntax: resolver address ... [valid=time] [ipv6=on|off];
Default: —
Context: http, server, location You can configure multiple DNS services, nginx will use polling to access DNS services, nginx will cache the results of DNS domain name resolution, the cache time is specified by valid, and IPv6 is used to display IPv6 on or off.
resolver_timeout is used to specify the timeout for DNS resolution.
|