V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
find456789
V2EX  ›  问与答

nginx 反向代理,当 proxy_pass 指向的主机不存在的时候,能不能别中断整个 nginx,让他继续运行?

  •  
  •   find456789 · 2018-01-12 10:26:04 +08:00 · 3049 次点击
    这是一个创建于 2267 天前的主题,其中的信息可能已经有所发展或是发生改变。

    根据访问域名,由 nginx 代理到不同的主机上

    下面的配置,

    当用户访问 aaa.com 就代理到 http://host1aaa:9001

    当用户访问 bbb.com 就代理到 http://host2bbb:9001

    这里的 host1aaa,host2bbb 是内网的主机名,

    偶尔,如果 host2bbb 整个机器没有开起来,

    nginx 就会找不到主机头,

    然后就报错了,结果导致 用户也无法访问 aaa.com

    大家有办法,让他在忽略错误,继续运行吗?

    server { 
        listen 80; 
        server_name aaa.com; 
    
        location / { 
            proxy_pass http://host1aaa:9001; 
        } 
    } 
    
    
    
    server { 
        listen 80; 
        server_name bbb.com; 
    
        location / { 
            proxy_pass http://host2bbb:9001; 
        } 
    }
    
    第 1 条附言  ·  2018-01-12 14:05:22 +08:00
    似乎无解, 看来只能麻烦一点, 修改配置文件了
    22 条回复    2018-01-12 14:04:39 +08:00
    BOYPT
        1
    BOYPT  
       2018-01-12 10:29:42 +08:00   ❤️ 1
    不是有 health check 模块么。
    rrfeng
        2
    rrfeng  
       2018-01-12 10:52:01 +08:00   ❤️ 1
    并不会像你说的这样。
    iyaozhen
        3
    iyaozhen  
       2018-01-12 11:36:21 +08:00 via Android   ❤️ 1
    「然后就报错了」截个图?贴个日志?
    你后面只有一个的话肯定就挂了,还想啥呢?
    Ellison
        4
    Ellison  
       2018-01-12 11:42:08 +08:00   ❤️ 1
    为什么会中断整个 nginx?只会 502 啊
    Nioty
        5
    Nioty  
       2018-01-12 11:52:32 +08:00 via Android   ❤️ 1
    Lz 的问题不是很明白…
    gstqc
        6
    gstqc  
       2018-01-12 11:54:57 +08:00   ❤️ 1
    你可能用了个假的 nginx
    akira
        7
    akira  
       2018-01-12 12:18:12 +08:00
    普通的 nginx 并没有你这个问题。你的问题可能是别的原因导致的
    MonoLogueChi
        8
    MonoLogueChi  
       2018-01-12 12:25:52 +08:00 via Android
    这种问题 nginx 应该不会报错啊
    kn007
        9
    kn007  
       2018-01-12 12:26:02 +08:00   ❤️ 3
    楼主说的问题,确实是有的,反代域名,如果域名不可解析(dns 返回无 A 记录),那么 nginx 会启动失败。
    你要么反代 ip,要么在 hosts 定义主机名 ip。楼上那些没遇到过,只能说实际应用没有这种场景。
    kn007
        10
    kn007  
       2018-01-12 12:27:42 +08:00   ❤️ 1
    你们可以反代下 test.test.test,能启动 nginx,算我输。
    当然啦,你或是网络要是定义了 test.test.test,那不算。
    oott123
        11
    oott123  
       2018-01-12 12:31:06 +08:00   ❤️ 1
    set shabi_nginx example.com:12345;
    proxy_pass http://$shabi_nginx;
    find456789
        12
    find456789  
    OP
       2018-01-12 12:31:43 +08:00
    @kn007 确实是你说的这种情况, 因为我是用 docker 启动的,有时候某个 docker 容器我不想运行,就临时注释(只想注释,懒得去改 nginx 配置文件),结果 nginx 就找不到了,然后就启动失败, 这时候,其他服务,也无法工作了, 这个比较尴尬
    kn007
        13
    kn007  
       2018-01-12 12:35:17 +08:00   ❤️ 1
    @find456789 因为 docker 是你管理的,他的 ip 是可控的,我的建议呢,是你在 nginx 反代 ip,而不是 host,如果你实在为了好看,好管理,想用 host,那么在 /etc/hosts,定义 host 的 ip。就解决了。
    find456789
        14
    find456789  
    OP
       2018-01-12 12:42:22 +08:00
    @oott123 大神,你给的这段代码,写进去,提示错误
    -----------------------------------------

    set shabi_nginx example.com:12345;
    location / {
    proxy_pass https://$shabi_nginx/;
    }

    -----------------------------------------

    》~ $ /usr/local/bin/nginx -t
    -----------------------------------------

    nginx: [emerg] invalid variable name "shabi_nginx" in /usr/local/etc/nginx/nginx.conf:59
    nginx: configuration file /usr/local/etc/nginx/nginx.conf test failed
    oott123
        15
    oott123  
       2018-01-12 12:45:48 +08:00
    少了个 $ 你补上啊

    set $shabi_nginx example.com:12345;
    find456789
        16
    find456789  
    OP
       2018-01-12 12:51:20 +08:00
    @oott123 大神

    似乎还是不行,给$shabi_nginx 的参数加双引号,或者不加,都会出错

    /usr/local/bin/nginx -t 返回测试成功

    ----------------------------------

    set $shabi_nginx "https://www.baidu.com" ;
    location / {
    proxy_pass $shabi_nginx;
    }



    -----------------------------------------
    报错如下

    An error occurred.
    Sorry, the page you are looking for is currently unavailable.
    Please try again later.

    If you are the system administrator of this resource then you should check the error log for details.

    Faithfully yours, nginx.
    oott123
        17
    oott123  
       2018-01-12 12:56:06 +08:00   ❤️ 1
    @find456789 #16 出错是 5xx 啊,十分正常,你自己看看 error_log 呗
    find456789
        18
    find456789  
    OP
       2018-01-12 13:10:59 +08:00
    @oott123 可以了, 错误解决办法是

    在 nginx 的配置文件中的 http{}部分添加一行 resolver 8.8.8.8;即可
    likuku
        19
    likuku  
       2018-01-12 13:14:42 +08:00
    要高可用么?那么每个域名对应后端的真实主机就别是单个的,至少每个域名得有 2 个独立主机保持最低冗余。
    cross874
        20
    cross874  
       2018-01-12 13:44:22 +08:00
    next upstream
    还有你可以把返回码当一个 location 来定义就行了
    wtbhk
        21
    wtbhk  
       2018-01-12 13:48:32 +08:00 via Android
    楼上这帮人没测试过别瞎说。upstream 配了域名,Nginx 在启动的时候就会解析域名,之后不再解析,解析失败直接报错。域名能解析,IP 连不上,不会报错。
    find456789
        22
    find456789  
    OP
       2018-01-12 14:04:39 +08:00
    @cross874 把主机列表放到 upstream 中, 也会直接说 找不到主机
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   950 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 859ms · UTC 21:21 · PVG 05:21 · LAX 14:21 · JFK 17:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.