Report this

What is the reason for this report?

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

Posted on July 13, 2018

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



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

For me I had a network error issue. My server didnt have the proper host of

'localhost' or better yet '127.0.0.1' 

and so even though I could plug the domain name and the port into my browser and even curl and get some data back, internally my client side node app didnt seem to be able to read the correct web address when I said the host to

localhost:8080

Try

netstat -plnt

to see what ports are actually being listened to and try using ‘127.0.0.1’ for all hosts

Hello,

What I could suggest is checking if your containers are part of the new network:

docker network inspect webnet

Then test the connection:

docker exec -ti nginx ping web

Regards, Bobby

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.