This article is a mirror article of machine translation, please click here to jump to the original article.

View: 9418|Reply: 0

[Web] Implement dynamic upstreams using Nginx's resolver

[Copy link]
Posted on 7/27/2019 10:23:00 PM | | |
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.





Previous:Share a personal FTP building software
Next:Alibaba Cloud OSS Nginx reverse proxy configuration
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com