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

查看:1082
本文介绍了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.

不同容器之间的这种通信如何工作? 网络"变量在哪里

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获取页面.然后我从此更改了nginx.conf中的上游

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天全站免登陆