0
votes

I'm trying to use express.js and nginx as a reverse proxy with Docker. The express app works fine with docker on its own, but when I add nginx I get 502 response. It prints this:

nginx_1 | 2019/03/30 21:14:04 [error] 6#6: *3 connect() failed (111: Connection refused) while connecting to upstream, client: 172.20.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:20000/", host: "localhost:21000"

docker-compose.yml

version: '3.3'

services:
  app:
    image: node:latest
    volumes:
      - ./app:/usr/src/service/app
    working_dir: /usr/src/service/app
    command: ["node", "app"]
    ports:
      - 20000:18000

  nginx: 
    image: nginx:latest
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./nginx/error.log:/etc/nginx/error_log.log
      - ./nginx/cache/:/etc/nginx/cache
    ports:
      - 21000:80

nginx.conf

events {

}

http {
  upstream upstream_server {
    server localhost:20000;
  }

  server {
    listen 127.0.0.1;
    listen 80;
    server_name localhost;

    location / {
      proxy_set_header Host $http_host;
      proxy_redirect off;
      proxy_pass http://upstream_server;
      break;
    }
  }
}
1

1 Answers

0
votes

Your nginx container & app container are on the same network but not on the same host - hence localhost is not where you'll find your app running.

Try app as the server parameter in upstream - this should resolve to the IP of the app container in the compose network.