Connection refused for when using node.js app (port 3000) with nginx reverse-proxy

July 13, 2018 1.2k views
Nginx Node.js Ubuntu 16.04

Hello everyone,

I tried to setup docker-compose with two containers: a site 1 (Nuxt.js app) and the Nginx reverse-proxy to reach the site at site1.mydomain.com.

When I access site1.mydomain.com, I get the following error:

proxy_1  | 2018/07/13 09:50:12 [error] 5#5: *9 connect() failed (111: Connection refused) while connecting to upstream, client: 176.148.214.6, server: site1.mydomain.com, request: "GET / HTTP/1.1", upstream: "http://172.18.0.2:3000/", host: "site1.mydomain.com"

Here is the docker-compose.yml configuration:

version: "3"
services:
  web:
    image: registry.gitlab.com/XXX/YYY:latest  # (Nuxt.js app)
    networks:
      - webnet
    expose:
      - "3000"
    ports:
      - "3000:3000"
    command: "npm run start"
  nginx:
    image: nginx:latest
    volumes:
      - ./nginx.template:/etc/nginx/conf.d/nginx.template
    ports:
      - "80:80"
    networks:
      - webnet
    depends_on:
      - web
    command: /bin/bash -c "envsubst '$$NGINX_HOST $$NGINX_PORT' < /etc/nginx/conf.d/nginx.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"
networks:
  webnet:

Here is the nginx.template configuration :

server {
    listen 80;
    server_name site1.mydomain.com;
    location / {
        proxy_pass http://web:3000;
    proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

Here is the output of docker-compose up:

Creating preprod-site_web_1 ... done
Creating preprod-site_nginx_1 ... done
Attaching to preprod-site_web_1, preprod-site_nginx_1
web_1    |
web_1    | > app_test@1.0.0 start /
web_1    | > nuxt start
web_1    |
web_1    | [10:03:21] [NUXT:AXIOS] baseURL: http://localhost:3000/
web_1    | [10:03:21] [NUXT:AXIOS] browserBaseURL: http://localhost:3000/
web_1    |
web_1    |  OPEN  http://localhost:3000
web_1    |

Here is the /etc/hosts

127.0.1.1 XXX XXX  # I have hidden the hostname
127.0.0.1 localhost
127.0.0.1 site1.mydomain.com

# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

I tried a lot of other configurations but I always ended up with this error.

What is wrong ? Is there a restriction on the port 3000 somehow ?

I checked that my app was running in the container called web by checking the output of curl localhost:3000 inside the container.

Thank you in advance,
Samuel

Be the first one to answer this question.