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

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

问题描述

我有两个带有Nginx的Docker容器。 container1链接到container2。 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;
    }
}

我可以ping container1 来自 container2 ,但是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天全站免登陆