【发布时间】:2015-12-27 00:49:05
【问题描述】:
我们在 Docker 中的公共域下有几个 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 忽略失败的上游?
【问题讨论】:
-
您是将应用容器与 Nginx 容器链接起来,还是将它们分开运行?如果
upstream块中的主机在运行时没有解析,那么 Nginx 将退出并出现上述错误... -
如果您可以使用IP,那么它会启动正常。在您的情况下使用
resolver(nginx.org/en/docs/http/ngx_http_core_module.html#resolver) 会起作用吗? -
@Justin 我们将每个应用程序都放在单独的容器中,nginx 也是如此。用 docker 链接它们
-
我有一个类似的设置(带有应用容器的 Nginx 容器)。我们创建了一个 Nginx 映像,其中包含一个
proxy.sh脚本,该脚本读取环境变量并为每个动态添加upstream条目,然后启动 Nginx。这很有效,因为当我们运行代理容器时,我们可以在运行时传入所需的上游。您可以执行类似的操作以在启动时启用/禁用某些上游 (或者像我的设置一样,只需在运行时添加所需的上游) -
我只是讨厌 nginx 崩溃。它只是一个愚蠢的设计。任何朋友怎么会因为另一台没有发现它的愚蠢设计而导致一台服务器崩溃
标签: nginx url-rewriting proxypass