如果找不到上游主机,则安装程序nginx不会崩溃 [英] Setup nginx not to crash if host in upstream is not found

查看:67
本文介绍了如果找不到上游主机,则安装程序nginx不会崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在Docker的通用域下有多个Rails应用程序,我们使用nginx将请求定向到特定应用程序.

We have several rails apps under common domain in Docker, and we use nginx to direct requests to specific apps.

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失败并停止:

If one of these apps is not started then nginx fails and stops:

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

我们不需要全部启动,否则nginx失败. 如何使Nginx忽略失败的上游?

We don't need them all to be up but nginx fails otherwise. How to make nginx ignore failed upstreams?

推荐答案

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

使用resolver指令指向可以解析主机的对象,无论该主机当前是否正在运行.

Use the resolver directive to point to something that can resolve the host, regardless if it's currently up or not.

如果您不能执行上述(这将允许Nginx启动/运行),请在location级别解决该问题:

Resolve it at the location level, if you can't do the above (this will allow Nginx to start/run):

location /foo {
  resolver 127.0.0.1 valid=30s;
  # or some other DNS (you company/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 (you company/internal DNS server)
  #resolver 8.8.8.8 valid=30s;
  set $upstream_bar foo;
  proxy_pass http://$upstream_bar:80;
}

这篇关于如果找不到上游主机,则安装程序nginx不会崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆