Home > Net >  Connection refused while connecting to upstream to a port other than 9000 in docker project
Connection refused while connecting to upstream to a port other than 9000 in docker project

Time:11-30

I've been struggling with running two php docker projects on the same server for a long time. For the first project, I use for the php port 9000:9000 in docker-compose.yaml. If I use the same port for another project, logically, when starting docker, it reports an error that port 9000 is already in use. Therefore, I set the port to 9002, but I get the error 502 Bad Gateway and Connection refused.

connect() failed (111: Connection refused) while connecting to upstream, client: 172.19.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://172.19.0.3:9002", host: "127.0.0.1:4443"

The first project working correctly.

Can someone advise me how to adjust the configuration or where am i doing wrong?

First docker project:

version: '3.9'

services:
  php:
    container_name: php
    build:
      context: ./docker/php
    ports:
      - '9000:9000'
    volumes:
      - .:/var/www/
      - ~/.ssh:/root/.ssh:ro
  nginx:
    container_name: nginx
    image: nginx:stable-alpine
    ports:
      - '5080:80'
      - '5443:443'
    volumes:
      - .:/var/www/
      - ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
      - /etc/letsencrypt:/etc/letsencrypt
    depends_on:
      - php
upstream php-upstream {
    server php:9000;
}

server {
    listen 80;
    server_name localhost;

    location / {
        return 301 https://$host$request_uri;
    }
}

server {
    listen 443 ssl;
    index index.php index.html index.htm;
    server_name localhost;
    root /var/www;
    error_log /var/log/nginx/project_error.log;
    access_log /var/log/nginx/project_access.log;

    ssl_certificate /etc/letsencrypt/live/domain1/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/domain1/privkey.pem;

    location / {
        try_files $uri /index.php$is_args$args;
    }

    location ~ ^/index\\.php(/|$) {
        fastcgi_pass php-upstream;
        fastcgi_split_path_info ^(. \\.php)(/.*)$;

        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;

        include fastcgi_params;

        fastcgi_buffer_size 128k;
        fastcgi_buffers 4 256k;
        fastcgi_busy_buffers_size 256k;

        internal;
    }

    location ~ \\.php$ {
        return 404;
    }
}

Second docker project:

version: '3.9'
services:
  php:
    container_name: hostmagic_php
    build:
      context: ./docker/php
    ports:
      - '9002:9000'
    volumes:
      - .:/var/www/symfony
      - ~/.ssh:/root/.ssh:ro
  nginx:
    container_name: hostmagic_nginx
    image: nginx:stable-alpine
    ports:
      - '8080:80'
      - '4443:443'
    volumes:
      - .:/var/www/symfony
      - ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
      - /etc/letsencrypt:/etc/letsencrypt
    depends_on:
      - php
  rabbitmq:
    image: rabbitmq:3-management-alpine
    container_name: hostmagic_rabbitmq
    environment:
      RABBITMQ_DEFAULT_USER: admin
      RABBITMQ_DEFAULT_PASS: password
      RABBITMQ_DEFAULT_VHOST: "/"
    ports:
      - 15672:15672
      - 5672:5672
upstream php-upstream {
    server php:9002;
}

server {
    listen 80;
    server_name localhost;

    location / {
        return 301 https://$host$request_uri;
    }
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    index index.php index.html index.htm;
    server_name localhost;
    root /var/www/symfony/public;
    error_log /var/log/nginx/project_error.log;
    access_log /var/log/nginx/project_access.log;

    ssl_certificate /etc/letsencrypt/live/domain2/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/domain2/privkey.pem;

    location / {
        try_files $uri /index.php$is_args$args;
    }

    location ~ ^/index\\.php(/|$) {
        fastcgi_pass php-upstream;
        fastcgi_split_path_info ^(. \\.php)(/.*)$;

        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        fastcgi_param HTTP_HOST $host;

        include fastcgi_params;

        fastcgi_buffer_size 128k;
        fastcgi_buffers 4 256k;
        fastcgi_busy_buffers_size 256k;

        internal;
    }

    location ~ \\.php$ {
        return 404;
    }
}

CodePudding user response:

Connections between containers always use the standard port number for the destination service. These connections don't require ports:, and ignore any port remapping that might be specified there.

That means, in the second Nginx proxy, you need to use the standard PHP-FPM port 9000 and not the remapped port:

upstream php-upstream {
    server php:9000;
}

If you're not going to access the FastCGI service directly from the host (and tools to do this are limited) then you can delete the ports: on both php containers, which further avoids this conflict. (You can also delete container_name: and Compose will pick a non-conflicting default.)

  • Related