Docker nginx反向代理返回502错误的网关“连接到上游时拒绝连接". [英] Docker nginx reverse proxy returns 502 bad gateway "connection refused while connecting to upstream"

查看:124
本文介绍了Docker nginx反向代理返回502错误的网关“连接到上游时拒绝连接".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在一个容器中将nginx反向代理设置为运行我的应用程序的另一个容器.这是我的nginx.conf:

I'm trying to set up nginx reverse proxy in a container to another container where my app is running. Here is my nginx.conf:

    daemon off;

    user  nginx;
    worker_processes  1;

    error_log  /var/log/nginx/error.log warn;
    pid        /var/run/nginx.pid;


    events {
        worker_connections  1024;
    }


    http {
        include       /etc/nginx/mime.types;
        default_type  application/octet-stream;

        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                        '$status $body_bytes_sent "$http_referer" '
                        '"$http_user_agent" "$http_x_forwarded_for"';

        access_log  /var/log/nginx/access.log  main;

        sendfile        on;

        upstream appserver {
            server app:3000;
        }

        server {
            listen 80;
            server_name localhost;

            location / {
                proxy_pass http://appserver/;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_redirect off;        
            }
        }
    }

我的docker-compose.yml看起来像这样:

My docker-compose.yml looks like this:

version: '3'
services:
  db:
    image: postgres
  redis:
    image: redis
  web:
    build: ./web
    image: web
    ports:
      - "8080:80"
  app:
    build: ./app
    image: app
    command: puma
    volumes:
      - ./app:/app
    expose:
      - "3000"
    ports:
      - "3000:3000"
    depends_on:
      - db
      - redis
      - web

反向代理的Dockerfile只是复制配置文件并启动nginx服务.这是我的问题:

The Dockerfile for the reverse proxy simply copies the config file and starts the nginx service. Here's my issue:

在主机浏览器上访问localhost:8080时,nginx返回502 Bad Gateway.日志显示"web_1 | 2017/02/26 22:55:15 [错误] 12#12:* 1连接到上游时,connect()失败(111:连接被拒绝),客户端:172.25.0.1,服务器:localhost,请求:"GET/HTTP/1.1",上游:" http://127.0.53.53:3000/",主机:"localhost:8080"web_1 |172.25.0.1--[26/Feb/2017:22:55:15 +0000]"GET/HTTP/1.1" 502576-""Mozilla/5.0(Windows NT 10.0; Win64; x64)AppleWebKit/537.36(KHTML,例如Gecko)Chrome/56.0.2924.87 Safari/537.36"-"

When accessing localhost:8080 on the host's browser, nginx returns 502 Bad Gateway. The logs reveal "web_1 | 2017/02/26 22:55:15 [error] 12#12: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.25.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "http://127.0.53.53:3000/", host: "localhost:8080" web_1 | 172.25.0.1 - - [26/Feb/2017:22:55:15 +0000] "GET / HTTP/1.1" 502 576 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36" "-""

现在,我认为nginx无法访问我的应用程序容器,但是,在网络"容器中运行"curl app:3000"会返回正确的响应.转发端口后,我也可以直接在端口3000上访问该应用程序.所以我觉得问题出在我的nginx.conf上,它是如何尝试访问该资源的.我已经将头撞在墙上的墙上了一段时间了.有什么想法吗?

Now right away I'm thinking nginx can't access my app container, however, running "curl app:3000" within the "web" container returns the proper response. I can also access the app directly on port 3000 when the port is forwarded. So I feel like the issue is with my nginx.conf of how it's trying to access that resource. I've been banging my head against a wall on this for a while. Any ideas?

推荐答案

重写docker-compose.yml可以确保在nginx反向代理之前启动应用程序,从而解决了该问题.

Rewriting the docker-compose.yml as so elimates the issue by ensuring the application is started prior to the nginx reverse proxy.

version: '3'
services:
  db:
    image: postgres
  redis:
    image: redis
  web:
    build: ./web
    image: web
    ports:
      - "8080:80"
    depends_on:
      - app
  app:
    build: ./app
    image: app
    command: puma
    volumes:
      - ./app:/app
    expose:
      - "3000"
    ports:
      - "3000:3000"
    depends_on:
      - db
      - redis

这篇关于Docker nginx反向代理返回502错误的网关“连接到上游时拒绝连接".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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