Nginx无法使用自定义DNS解析器解析上游

时间:2019-05-12 17:04:26

标签: nginx dns consul

docker run --rm --net=host -v $PWD/default.conf:/etc/nginx/conf.d/default.conf nginx
2019/05/12 17:02:49 [emerg] 1#1: host not found in upstream "tickethub.service.consul" in /etc/nginx/conf.d/default.conf:10
nginx: [emerg] host not found in upstream "tickethub.service.consul" in /etc/nginx/conf.d/default.conf:10

挖掘过程中正确显示了DNS记录:

dig @127.0.0.1 -p 8600 tickethub.service.consul

; <<>> DiG 9.12.3-P1 <<>> @127.0.0.1 -p 8600 tickethub.service.consul
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 57394
;; flags: qr aa rd; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 4
;; WARNING: recursion requested but not available

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;tickethub.service.consul.  IN  A

;; ANSWER SECTION:
tickethub.service.consul. 0 IN  A   172.23.0.6
tickethub.service.consul. 0 IN  A   172.23.0.5
tickethub.service.consul. 0 IN  A   172.23.0.7

;; ADDITIONAL SECTION:
tickethub.service.consul. 0 IN  TXT "consul-network-segment="
tickethub.service.consul. 0 IN  TXT "consul-network-segment="
tickethub.service.consul. 0 IN  TXT "consul-network-segment="

;; Query time: 0 msec
;; SERVER: 127.0.0.1#8600(127.0.0.1)
;; WHEN: Sun May 12 16:58:54 GMT 2019
;; MSG SIZE  rcvd: 209

我的nginx配置:

server {
    listen       80;
    server_name  localhost;

    location / {
        resolver 127.0.0.1:8600;
        proxy_pass   http://tickethub.service.consul;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

可能是什么问题?

2 个答案:

答案 0 :(得分:0)

当我将docker容器上的DNS显式设置为<string, Command>时,此方法有效,这意味着Nginx可能试图不使用指定的解析器来解析它。

我想我还必须将DNS端口更改为53,而不是显式的8600

或其他...

可能是一堆nginx错误...

答案 1 :(得分:0)

mic drop

当我使用变量设置proxy_pass时,它起作用:

location / {
    resolver consul;
    set $endpoint tickethub.service.consul;
    proxy_pass http://$endpoint/;
}