V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
goforwardv2
V2EX  ›  程序员

nginx 解析 consul 域名问题,求大佬?

  •  
  •   goforwardv2 · 13 天前 · 760 次点击

    consul 用如下命令启动:
    sudo docker run -d --name=consul -p 8500:8500 -p 8600:8600/udp -v /home/alex/code/consul-data:/consul/data consul:1.15.4

    nginx.conf 配置如下:
    stream {
    resolver 192.168.1.125:8600 ipv6=off valid=30s;

    upstream tcp_servers {  
        server my-service.service.consul:6789;  
    }  
    
    server {  
        listen 12345;  
        proxy_pass tcp_servers;  
        proxy_connect_timeout 1s;  
    }  
    

    }

    后端的 TCP 服务器已经成功注册到 consul ,执行 dig @192.168.1.125 -p 8600 my-service.service.consul 是可以正确解析域名的,但是配置在 nginx 里面,就提示: nginx: [emerg] host not found in upstream "my-service.service.consul:6789" in /etc/nginx/nginx.conf:73
    nginx: configuration file /etc/nginx/nginx.conf test failed

    搞了一上午,怎么改配置都不行

    第 1 条附言  ·  13 天前
    nginx 没有运行在 docker 里面,是系统命令运行。
    nginx.conf 配置如下:

    load_module /usr/lib/nginx/modules/ngx_stream_module.so;
    worker_processes auto;
    worker_rlimit_nofile 30000;

    events {
    worker_connections 10240;
    }

    http {
    client_max_body_size 100m;
    resolver 8.8.8.8;

    upstream specification_server {
    server 127.0.0.1:6088;
    server 127.0.0.1:6089;
    }

    upstream http_server {
    server 172.16.15.9:8090;
    server 172.16.15.9:8091;
    }

    server {
    listen 80;

    location /get {
    proxy_pass http://http_server/get;
    }

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    location / {
    root /usr/share/nginx/html;
    }
    location = /maintenance.html {
    root /usr/share/nginx/html;
    internal;
    }

    # 其他配置项...
    }
    }

    stream {
    resolver 192.168.1.125:8600 valid=30s;

    upstream tcp_servers {
    server my-service.service.consul:6789;
    #server 192.168.1.126:6789;
    }

    server {
    listen 12345;
    proxy_pass tcp_servers;
    proxy_connect_timeout 1s;
    }
    }

    如果直接用 nginx.conf 只用 server 192.168.1.126:6789;是可以的,但是就失去意义了,向让 nginx 动态发现后端的服务器。
    14 条回复    2024-06-05 14:53:56 +08:00
    guisheng
        1
    guisheng  
       13 天前 via iPhone
    容器异常或者不在同一个网络中
    guisheng
        2
    guisheng  
       13 天前 via iPhone
    @guisheng 重启即可;前段时间遇到过 在同一个网络中 nginx 配置了未启动的容器就会这样报错
    goforwardv2
        3
    goforwardv2  
    OP
       13 天前
    @guisheng nginx 和 consul docker 开在一台机器上也不行,同时 consul 已经用了--network host 也不行
    guisheng
        4
    guisheng  
       13 天前 via iPhone
    @goforwardv2 consul 先启动,nginx 后启动。 指定了宿主机网络的话 你给 nginx -add host
    F7TsdQL45E0jmoiG
        5
    F7TsdQL45E0jmoiG  
       13 天前
    nginx 中指定 resolver
    hejiangyuan
        6
    hejiangyuan  
       13 天前
    6789 这个端口号好像不对吧。nginx 是在 docker 里面吗? 在 nginx 环境下 ping my-service.service.consul 的结果是什么?
    goforwardv2
        7
    goforwardv2  
    OP
       13 天前
    @morenacl @hejiangyuan resolver 指定了,并且 nginx 是系统命令运行的
    xoic
        8
    xoic  
       13 天前
    nginx 版本?
    goforwardv2
        9
    goforwardv2  
    OP
       13 天前
    @xoic nginx version: nginx/1.14.0 (Ubuntu)
    chenuu
        10
    chenuu  
       13 天前
    nginx-plus 才支持 consul 吧
    goforwardv2
        11
    goforwardv2  
    OP
       13 天前
    @chenuu 有可能啊,尝试了很多方法都无效,我试试 nginx-plus
    chenuu
        12
    chenuu  
       13 天前
    开源的 nginx,对 consul 的话,你可以试试 consul-template 的方案,或者换掉 ng,使用 caddy 或者 traefik
    guanzhangzhang
        13
    guanzhangzhang  
       13 天前
    看着是你 resolve 的层级不对,你自己抓包 53 看看是不是发往 8.8.8.8 了
    xoic
        14
    xoic  
       13 天前
    @goforwardv2 感觉版本不支持 resolver 带端口的写法

    该指令出现在 1.17.5 版中。
    配置用于将上游服务器的名称解析为地址的名称服务器,例如:
    resolver 127.0.0.1 [::1]:5353 valid=30s;

    文档是这么写的。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3027 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 12:17 · PVG 20:17 · LAX 05:17 · JFK 08:17
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.