Docker:proxy_pass 到另一个容器 - nginx:在上游找不到主机 [英] Docker: proxy_pass to another container - nginx: host not found in upstream

查看:29
本文介绍了Docker:proxy_pass 到另一个容器 - nginx:在上游找不到主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(我知道其他人之前也问过这个问题,但我无法使用其他帖子中提出的解决方案解决问题,所以我想我会尝试发布我的配置文件,看看是否有人可以帮助.)

我想为 nginx 创建一个容器,并使用 proxy_pass 将请求传递给正在运行的 Web 应用程序的容器.我不知道如何在两个容器之间进行通信.当我尝试运行 docker stack deploy -c docker-compose.yml somename 时,只有 Web 容器启动.nginx容器启动失败,卡在尝试重启的循环中.这是我收到的日志消息:

I want to create a container for nginx, and use proxy_pass to pass requests to the container with the running web application. I can't figure out how to communicate between the two containers. When i try to run docker stack deploy -c docker-compose.yml somename, only the web container starts. The nginx container fails to start, and is stuck in a loop of trying to restart. This is the log messages I get:

2017/08/16 14:56:10 [emerg] 1#1:在上游web:8000"中找不到主机在/etc/nginx/conf.d/nginx.conf:2 nginx: [emerg] 中找不到主机/etc/nginx/conf.d/nginx.conf:2 中的上游web:8000"

2017/08/16 14:56:10 [emerg] 1#1: host not found in upstream "web:8000" in /etc/nginx/conf.d/nginx.conf:2 nginx: [emerg] host not found in upstream "web:8000" in /etc/nginx/conf.d/nginx.conf:2

我找到了一个答案,只要您在 docker-compose.yml 文件中使用与服务下相同的名称,nginx 就会找到该变量.但是,这对我来说似乎没有帮助.

I found an answer that as long as you use the same name as under services in the docker-compose.yml file, nginx would find the variable. However that doesn't seem to help in my case.

不同容器之间的这种通信是如何工作的?'web'变量在哪里

How does communication like this between different containers work? Where is the 'web' variable

我见过的大多数示例都在 docker-compose.yml 文件中使用 version: "2",这会有所不同吗?

Most examples I've seen use version: "2" in the docker-compose.yml file, should this make a difference?

我的 docker-compose.yml:

version: "3"
services:
  web:
    image: user/repo:web
    deploy:
      resources:
        limits:
          cpus: "0.1"
          memory: 50M
      restart_policy:
        condition: on-failure
    ports:
      - "8000:80"
    networks:
      - webnet

  nginx:
    image: user/repo:nginx
    ports:
      - 80:80
    links:
      - web:web
    depends_on:
      - web

networks:
  webnet:

Nginx 配置:

upstream docker-web {
    server web:8000;
}


server {
    listen 80;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    location / {
         proxy_pass http://docker-web;

         proxy_set_header   Host $host;
         proxy_set_header   X-Real-IP $remote_addr;
         proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_set_header   X-Forwarded-Host $server_name;
    }
}

推荐答案

我找到了解决问题的方法.得到了一些帮助来修复 docker-compose.yml,所以它看起来像这样:

I figured out how to fix the problem. Got some help to fix the docker-compose.yml, so it looks like this:

docker-compose-yml:

version: "3"
services:
  web:
    image: user/repo:web
    deploy:
      resources:
        limits:
          cpus: "0.1"
          memory: 50M
      restart_policy:
        condition: on-failure
    ports:
      - "8000:80"
    networks:
        main:
            aliases:
                - web


  nginx:
    image: user/repo:nginx
    ports:
      - 80:80
    links:
      - web:web
    depends_on:
      - web
    networks:
        main:
            aliases:
                - nginx
networks:
  main:

此后,nginx 容器实际运行,但仍无法连接到 Web 容器.发现我能够同时使用 curl webcurl web -p 8000 从 nginx 容器内部的 web 获取页面.然后我从这个

After this the nginx container actually ran, but it was still not capable of connecting to the web-container. Found out I was able to use both curl web and curl web -p 8000 to get the page from web, from inside the nginx container. Then I changed the upstream in my nginx.conf from this

upstream docker-web {
    server web:8000;
}

为此:

upstream docker-web {
    server web;
}

这篇关于Docker:proxy_pass 到另一个容器 - nginx:在上游找不到主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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