Nginx connection refused, when trying to connect to node app as reverse proxy

I trying to build a webapp with docker containers and I am getting connection refused when trying to run Nginx as a reverse proxy to my node app. I am not sure if it is a nginx server config issue or a docker-compose config issue.

[error] 5#5: *1 connect() failed (111: Connection refused) while connecting to upstream, client:, server:, request: "GET / HTTP/1.1", upstream: "", host: ""

I get this error when hitting, the weird thing is that my app works when the port number is referenced, so runs the app.

  • How to set up Docker file to install Calabash-android?
  • What is the concept of Ambassador in distributed systems?
  • Docker: how to open ports to the host machine?
  • Docker Compose Image Failing
  • Running GUI in docker (no ssh, no VNC)
  • Mysql docker container block while it's importing data (OSX)
  • My nginx server config:

    server {
        listen       80;
        port_in_redirect off;
        autoindex on;
        location / {
            proxy_redirect off;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;

    My Docker Compose File: (There might be some redundant things in here)

    version: "2"
        build: ./nginx
          - "80:80"
          - app
          - app
          context: .
          dockerfile: DockerFile
          - "7770:7770"
          - mongo
          - mongo
        image: mongo
          - "27017:27017"
          - mongodata
          - mongodata
        image: tianon/true
          - /data/db

    My Node Dockerfile:

    FROM node:latest
    ADD package.json /tmp/package.json
    RUN cd /tmp && npm install
    RUN mkdir -p /opt/app && cp -a /tmp/node_modules /opt/app/
    WORKDIR /opt/app
    ADD . /opt/app
    EXPOSE 7770
    CMD ["npm", "start"]

    My ngnix Dockerfile

    FROM nginx:1.10
    COPY default.conf /etc/nginx/conf.d/default.conf

    On npm start this will run:

    app.get('*', function(req, res) {
      res.sendFile(path.join(__dirname, 'index.html'));
    app.listen(7770, function(err) {
      if (err) {
      console.log('Listening at http://localhost:7770');

    This is my first run at docker, so I may have muddled up a few things. Also I am pointing to in /private/etc/hosts.

  • Docker run returning “snmpd is neither service nor target!? ” while trying to restart it
  • Adding services in different consul clients running on same host
  • Docker issue commands to an app inside container?
  • Are there characters that I need to escape in the ProcessStartInfo.Arguments?
  • How do debug multiple core services in containers from Visual Studio Mac
  • Docker: how to set up file ownership in a data-only container?
  • 2 Solutions collect form web for “Nginx connection refused, when trying to connect to node app as reverse proxy”


    First of all, you need to remember each container has its own network stack, so you cannot use localhost inside the container to reach a service running in the docker host.

    For this specific project, you will need to point the proxy_pass directive in your Nginx server config to a value what reach the app container. Something like:

    proxy_pass http://app:7770;

    You will need to do right that because in docker-compose context your container name will be mapped to an internal DNS entry. With that, you will not need to publish 7770 of the app container to the outside world and if your MongoBD will be accessed just by your app container, you will not need to publish the 27017 port either.

    If you want to route traffic from nginx to app you have to use ip address or dns of the app container on proxy_pass. With Docker Compose services can discover each other with service names, so change in nginx conf

    proxy_pass http://app:7770;

    You don’t need to publish port 7770 to outside world. Also for mongo you don’t need to publish 27017 port.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.