Nginx connect() failed (111 connection refused) while connecting to upstream

Hi, I have an issue with dockerized nginx + nextcloud. When the nextcloud exposes port to the localhost “127.0.0.1:8080:80”, nginx cannot access it and returns the following error: “connect() failed (111 connection refused) while connecting to upstream”. It only works fine when expose “8080:80” on the nextcloud and proxy_pass to “http://local_ip_of_docker_host:8080” but this is not the solution becouse I don’t want to have this port open to the internet.

docker_compose.yml:

app:
    image: nextcloud
    restart: always
    ports:
      - "127.0.0.1:8080:80"
    links:
      - db
    volumes:
      - /usr/docker/cloud/www:/var/www/html
      - /mnt/nfs/Cloud:/var/www/html/data
    depends_on:
      - db


  nginx:
    image: nginx
    restart: always
    ports:
     - "80:80"
     - "443:443"
    volumes:
     - /usr/docker/nginx/conf.d:/etc/nginx/conf.d
     - /usr/docker/nginx/ssl:/etc/nginx/ssl

nginx:

server {
    listen       443 ssl http2;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;
    ssl_certificate_key /etc/nginx/ssl/key.key;
    ssl_certificate     /etc/nginx/ssl/cert.crt;

    location / {
        proxy_pass http://127.0.0.1:8080;
    }

Hi

Try:

app:
    image: nextcloud
    restart: always
    links:
      - db
    volumes:
      - /usr/docker/cloud/www:/var/www/html
      - /mnt/nfs/Cloud:/var/www/html/data
    depends_on:
      - db


  nginx:
    image: nginx
    restart: always
    ports:
     - "80:80"
     - "443:443"
    volumes:
     - /usr/docker/nginx/conf.d:/etc/nginx/conf.d
     - /usr/docker/nginx/ssl:/etc/nginx/ssl
server {
    listen       443 ssl http2;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;
    ssl_certificate_key /etc/nginx/ssl/key.key;
    ssl_certificate     /etc/nginx/ssl/cert.crt;

    location / {
        proxy_pass http://app;
    }

It reveals that main problem was found inside nextcloud conf.php file. I’ve added the “172.17.0.1:8080” entry which is a docker0 network gateway address. The second thing I’ve changed was proxy_pass address. Now it works. @terpz thanks for your answer. I’ll try accessing containers by name in the future.

config.php:

  array (
    0 => '192.168.1.6:8080',
    1 => '172.17.0.1:8080',
  ),

nginx:

    location / {
        proxy_pass http://172.17.0.1:8080;
    }

Yes, because now you are going from the docker network, to the outside of docker network, only to go back into the docker network.
Also the point of containers is that you can kill it and start it again and it will just run, if you hardcode the gateway ip, it might change the next time you want to run it