gpt4 book ai didi

nginx - 设置 nginx 在找不到上游主机时不会崩溃

转载 作者:行者123 更新时间:2023-12-02 00:12:23 25 4
gpt4 key购买 nike

我们在 Docker 的公共(public)域下有几个 Rails 应用程序,我们使用 nginx 将请求定向到特定应用程序。

our_dev_server.com/foo # proxies to foo app
our_dev_server.com/bar # proxies to bar

配置如下:

upstream foo {
server foo:3000;
}

upstream bar {
server bar:3000;
}

# and about 10 more...

server {
listen *:80 default_server;

server_name our_dev_server.com;

location /foo {
# this is specific to asset management in rails dev
rewrite ^/foo/assets(/.*)$ /assets/$1 break;
rewrite ^/foo(/.*)$ /foo/$1 break;
proxy_pass http://foo;
}

location /bar {
rewrite ^/bar/assets(/.*)$ /assets/$1 break;
rewrite ^/bar(/.*)$ /bar/$1 break;
proxy_pass http://bar;
}

# and about 10 more...
}

如果这些应用程序之一未启动,则 nginx 会失败并停止:

host not found in upstream "bar:3000" in /etc/nginx/conf.d/nginx.conf:6

我们不需要它们全部启动,但否则 nginx 会失败。如何让 nginx 忽略失败的上游?

最佳答案

  1. 如果您可以使用静态 IP,那么只需使用它,它就会启动,如果没有响应,则返回 503

  2. 使用resolver指令来指向可以解析主机的东西,无论它当前是否启动。

  3. 如果您无法执行上述操作,请在位置级别解决该问题(这将允许 Nginx 启动/运行):

     location /foo {
    resolver 127.0.0.1 valid=30s;
    # or some other DNS (your company's internal DNS server)
    #resolver 8.8.8.8 valid=30s;
    set $upstream_foo foo;
    proxy_pass http://$upstream_foo:80;
    }

    location /bar {
    resolver 127.0.0.1 valid=30s;
    # or some other DNS (your company's internal DNS server)
    #resolver 8.8.8.8 valid=30s;
    set $upstream_bar foo;
    proxy_pass http://$upstream_bar:80;
    }

关于nginx - 设置 nginx 在找不到上游主机时不会崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32845674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com