nginx proxy_pass 到链接的 docker 容器 [英] nginx proxy_pass to a linked docker container

查看:109
本文介绍了nginx proxy_pass 到链接的 docker 容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个带有 nginx 的 docker 容器.容器 1 链接到容器 2.然后 Docker 向 /etc/hosts 添加一个条目,我将其输入到 nginx 配置中,如下所示:

I have two docker containers with nginx. container1 is linked to container2. Docker then adds an entry to /etc/hosts which I entered into the nginx configuration like so:

server {
    location ~ ^/some_url/(.*)$ {
        proxy_pass http://container1/$1;
    }
}

我可以从 container2 ping container1,但 nginx 无法解析:

I can ping container1 from container2, but nginx cannot resolve it:

*1 没有定义解析器来解析 container1

*1 no resolver defined to resolve container1

如何将请求代理传递给另一个 docker 容器?

How can I proxy_pass a request to another docker container?

推荐答案

直接使用上游块而不是容器名称

Use an upstream block instead of the container name directly

upstream backend {
    server container1;
}
server {
    location ~ ^/some_url/(.*)$ {
        proxy_pass http://backend/$1;
    }
}

这应该允许进行正常的名称解析,从而提供一种轻松地将 docker 链接与 nginx 一起使用的方法.

This should allow normal name resolution to occur providing a way to easily use docker links with nginx.

这篇关于nginx proxy_pass 到链接的 docker 容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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