Failed (111: Connection refused) while connecting to upstream

Hi All,

im trying to setup phpmyadmin and its not working, i am missing something but i cant figure out what. If someone can point my in the right direction that would be great.
my setup:
nginx-172.31.255.226-81:81
phpfpm-172.31.255.227-9000:9000
mariadb-172.31.255.229-3306:3306
phpmyadmin-172.31.255.228-8081:80
my nginx file:

server {
    listen 81;
    server_name _;
    autoindex off;
    access_log /var/log/nginx/access/phpmyadmin.access.log;
    error_log /var/log/nginx/error/phpmyadmin.error.log debug;
    index index.php;
    location / {
	proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-host $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-NginX-Proxy true;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://172.31.255.228:80;
        proxy_buffering off;
        proxy_buffer_size 16k;
        proxy_busy_buffers_size 24k;
        proxy_buffers 64 4k;
    }

   location ~ \.php(/|$) {
        try_files      $uri = 404;
        include /etc/nginx/params/fastcgi.conf;
        fastcgi_index  index.php;
	fastcgi_split_path_info ^(.+\.php)(/.*)$;
	fastcgi_pass 172.31.255.227:9000;
}
}

And the error i get when i go to http://192.168.0.43:81/:
2022/10/02 20:33:28 [error] 24#24: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.0.17, server: _, request: “GET / HTTP/1.1”, upstream: “http://172.31.255.228:80/”, host: “192.168.0.43:81”

I guess the IP address of the container is different. Maybe it was the IP first and it changed. You should not set the nginx proxy manually for containers. You can use nginx-proxy or traefik

An other thing I noticed is that you have an other rule for php files, but PHPMyAdmin is written in PHP, so even if you can make it work first, at one point it would forward your request to the other IP address, to the PHP FPM container. You should use different domain names for different services. The above mentioned tools can handle it for you.

Hi Rimelek,

I have manually set the ip address of each container so there shouldn’t be any issues regarding that.
With the php i am assuming its similar to my current setup where in my nginx file i load phpmydamin and point it to my .sock file, except in docker i assume you would point it to the ip of the php docker container instead

Thank you!

How did you do that? Can you access that IP addres from outside a container? If you can share a compose file where we can see how you are trying to set the static IP addresses, that would help.

PHP-FPM can listen on unix sockets or TCP sockets. You could configure PHP-FPM to listen on unix sockets even in containers, but that is not practical, because the socket has to be accessible by the webserver and PHP-FPM as well which you can achieve with volumes, but using TCP sockets is much easier and portable.

Since I don’t know how you installed PHPMyAdmin, based on your current configuration I assume it is not just the PHP-FPM variant, but the one that contains HTTPD, so I don’t understand what you would point to a socket file in case of PHPMyAdmin.

Hi,

I use portainer so i have attached images on how its setup, as i am a new user the forums will only let me post 1 image

As far as I know Portainer still needs configuration to create networks. So how did you configured that network?

Hi Rimelek,
Hers is a pic of my current network

and
2

i have exported my portainer settings, hopefully this is close to what you are after:

Networks:
 "Name": "Web",
                "Id": "3f3ef220b41239538402e5800697571fc163785be682672ecb75e5429b3a88d0",
                "Created": "2022-09-24T11:08:35.571775275+01:00",
                "Scope": "local",
                "Driver": "bridge",
                "EnableIPv6": false,
                "IPAM": {
                  "Driver": "default",
                  "Options": null,
                  "Config": [
                    {
                      "Subnet": "172.31.255.255/27",
                      "IPRange": "172.31.255.255/27",
                      "Gateway": "172.31.255.254"
                    }
                  ]
                },
                "Internal": false,
                "Attachable": false,
                "Ingress": false,
                "ConfigFrom": {
                  "Network": ""
                },
                "ConfigOnly": false,
                "Containers": {},
                "Options": {},
                "Labels": {}
"Containers": [
              {
                "Id": "edf529549f97e8b7ef400adf42c9d5f46c406aa066275fe2dd07edeef1f6d9e8",
                "Names": [
                  "/mdphpma"
                ],
                "Image": "docker.io/mariadb:latest",
                "ImageID": "sha256:14f1097913ecb422c61f9b35604339c1ac74394e7df7f84f765917b018605b5f",
                "Command": "docker-entrypoint.sh mariadbd",
                "Created": 1665090921,
                "Ports": [
                  {
                    "IP": "0.0.0.0",
                    "PrivatePort": 3306,
                    "PublicPort": 3306,
                    "Type": "tcp"
                  }
                ],
                "Labels": {},
                "State": "running",
                "Status": "Up About an hour",
                "HostConfig": {
                  "NetworkMode": "Web"
                },
                "NetworkSettings": {
                  "Networks": {
                    "Web": {
                      "IPAMConfig": {
                        "IPv4Address": "172.31.255.229"
                      },
                      "Links": null,
                      "Aliases": null,
                      "NetworkID": "3f3ef220b41239538402e5800697571fc163785be682672ecb75e5429b3a88d0",
                      "EndpointID": "ed822f07097df23133cf3dfb388f17fd82b12e674485be1b3e581f1244eadbe4",
                      "Gateway": "172.31.255.254",
                      "IPAddress": "172.31.255.229",
                      "IPPrefixLen": 27,
                      "IPv6Gateway": "",
                      "GlobalIPv6Address": "",
                      "GlobalIPv6PrefixLen": 0,
                      "MacAddress": "02:42:ac:1f:ff:e5",
                      "DriverOpts": null
                    }
                  }
                },
                "Mounts": [
                  {
                    "Type": "volume",
                    "Name": "md_phpma",
                    "Source": "/mnt/dietpi_userdata/docker-data/volumes/md_phpma/_data",
                    "Destination": "/var/lib/mysql",
                    "Driver": "local",
                    "Mode": "z",
                    "RW": true,
                    "Propagation": ""
                  }
                ]
              },
              {
                "Id": "421f0485585bf564d8d1e454436ccaa39d660b97cf585e20681ac43da2a449f7",
                "Names": [
                  "/phpmyadmin"
                ],
                "Image": "docker.io/phpmyadmin:fpm",
                "ImageID": "sha256:43d0ff92a6b0bf8def35503c9d386052061ee98b5783150304b17b0825e6e916",
                "Command": "/docker-entrypoint.sh php-fpm",
                "Created": 1665090906,
                "Ports": [
                  {
                    "IP": "0.0.0.0",
                    "PrivatePort": 80,
                    "PublicPort": 8081,
                    "Type": "tcp"
                  },
                  {
                    "PrivatePort": 9000,
                    "Type": "tcp"
                  }
                ],
                "Labels": {
                  "org.opencontainers.image.authors": "The phpMyAdmin Team \u003cdevelopers@phpmyadmin.net\u003e",
                  "org.opencontainers.image.description": "Run phpMyAdmin with Alpine, Apache and PHP FPM.",
                  "org.opencontainers.image.documentation": "https://github.com/phpmyadmin/docker#readme",
                  "org.opencontainers.image.licenses": "GPL-2.0-only",
                  "org.opencontainers.image.source": "https://github.com/phpmyadmin/docker.git",
                  "org.opencontainers.image.title": "Official phpMyAdmin Docker image",
                  "org.opencontainers.image.url": "https://github.com/phpmyadmin/docker#readme",
                  "org.opencontainers.image.vendor": "phpMyAdmin",
                  "org.opencontainers.image.version": "5.2.0"
                },
                "State": "running",
                "Status": "Up About an hour",
                "HostConfig": {
                  "NetworkMode": "Web"
                },
                "NetworkSettings": {
                  "Networks": {
                    "Web": {
                      "IPAMConfig": {
                        "IPv4Address": "172.31.255.228"
                      },
                      "Links": null,
                      "Aliases": null,
                      "NetworkID": "3f3ef220b41239538402e5800697571fc163785be682672ecb75e5429b3a88d0",
                      "EndpointID": "40ad16bb6288e9eb55c6ecc751e8b41ad7ceb65da7b06feeb53091e58e7f95b6",
                      "Gateway": "172.31.255.254",
                      "IPAddress": "172.31.255.228",
                      "IPPrefixLen": 27,
                      "IPv6Gateway": "",
                      "GlobalIPv6Address": "",
                      "GlobalIPv6PrefixLen": 0,
                      "MacAddress": "02:42:ac:1f:ff:e4",
                      "DriverOpts": null
                    }
                  }
                },
                "Mounts": [
                  {
                    "Type": "volume",
                    "Name": "phpma_etc",
                    "Source": "/mnt/dietpi_userdata/docker-data/volumes/phpma_etc/_data",
                    "Destination": "/etc/phpmyadmin",
                    "Driver": "local",
                    "Mode": "z",
                    "RW": true,
                    "Propagation": ""
                  },
                  {
                    "Type": "volume",
                    "Name": "phpma_html",
                    "Source": "/mnt/dietpi_userdata/docker-data/volumes/phpma_html/_data",
                    "Destination": "/var/www/html",
                    "Driver": "local",
                    "Mode": "z",
                    "RW": true,
                    "Propagation": ""
                  }
                ]
              },
              {
                "Id": "21a9c425e6893d62fb8eebfb8ccaeb8f6cb5d1aa14cc79816f890bd3f528e432",
                "Names": [
                  "/phpfpm"
                ],
                "Image": "docker.io/php:fpm",
                "ImageID": "sha256:fa1b5d320fa7279567b317dbd47023ff284b07617e3a14cb4aef2420bb644c45",
                "Command": "docker-php-entrypoint php-fpm",
                "Created": 1665088342,
                "Ports": [
                  {
                    "IP": "0.0.0.0",
                    "PrivatePort": 9000,
                    "PublicPort": 9000,
                    "Type": "tcp"
                  }
                ],
                "Labels": {},
                "State": "running",
                "Status": "Up About an hour",
                "HostConfig": {
                  "NetworkMode": "Web"
                },
                "NetworkSettings": {
                  "Networks": {
                    "Web": {
                      "IPAMConfig": {
                        "IPv4Address": "172.31.255.227"
                      },
                      "Links": null,
                      "Aliases": null,
                      "NetworkID": "3f3ef220b41239538402e5800697571fc163785be682672ecb75e5429b3a88d0",
                      "EndpointID": "4b30bc0cdc3e85a19ab931532c59c2ae3650bff5260ac77313b90ea9e02c0510",
                      "Gateway": "172.31.255.254",
                      "IPAddress": "172.31.255.227",
                      "IPPrefixLen": 27,
                      "IPv6Gateway": "",
                      "GlobalIPv6Address": "",
                      "GlobalIPv6PrefixLen": 0,
                      "MacAddress": "02:42:ac:1f:ff:e3",
                      "DriverOpts": null
                    }
                  }
                },
                "Mounts": [
                  {
                    "Type": "volume",
                    "Name": "php_usr",
                    "Source": "/mnt/dietpi_userdata/docker-data/volumes/php_usr/_data",
                    "Destination": "/usr/local/etc",
                    "Driver": "local",
                    "Mode": "z",
                    "RW": true,
                    "Propagation": ""
                  },
                  {
                    "Type": "volume",
                    "Name": "phpma_html",
                    "Source": "/mnt/dietpi_userdata/docker-data/volumes/phpma_html/_data",
                    "Destination": "/var/www/html/phpma",
                    "Driver": "local",
                    "Mode": "z",
                    "RW": true,
                    "Propagation": ""
                  }
                ]
              },
              {
                "Id": "e10a980e572b38bcd0c6ac1de9ac3faaec7736028c221cf6a6d028856d65075e",
                "Names": [
                  "/nginx"
                ],
                "Image": "docker.io/nginx:stable",
                "ImageID": "sha256:08a1cbf9c69edd2ab8e5250ae97703f60b9393fc5a4827cedda4b7387a5cfc6a",
                "Command": "/docker-entrypoint.sh nginx -g 'daemon off;'",
                "Created": 1665028510,
                "Ports": [
                  {
                    "IP": "0.0.0.0",
                    "PrivatePort": 443,
                    "PublicPort": 443,
                    "Type": "tcp"
                  },
                  {
                    "IP": "0.0.0.0",
                    "PrivatePort": 80,
                    "PublicPort": 80,
                    "Type": "tcp"
                  },
                  {
                    "IP": "0.0.0.0",
                    "PrivatePort": 81,
                    "PublicPort": 81,
                    "Type": "tcp"
                  }
                ],
                "Labels": {
                  "maintainer": "NGINX Docker Maintainers \u003cdocker-maint@nginx.com\u003e"
                },
                "State": "running",
                "Status": "Up 7 minutes",
                "HostConfig": {
                  "NetworkMode": "Web"
                },
                "NetworkSettings": {
                  "Networks": {
                    "Web": {
                      "IPAMConfig": {
                        "IPv4Address": "172.31.255.226"
                      },
                      "Links": null,
                      "Aliases": null,
                      "NetworkID": "3f3ef220b41239538402e5800697571fc163785be682672ecb75e5429b3a88d0",
                      "EndpointID": "5bcd58f7ffa5ec19d00fde51212918ae69547dcddd2ab72875777395372f7c73",
                      "Gateway": "172.31.255.254",
                      "IPAddress": "172.31.255.226",
                      "IPPrefixLen": 27,
                      "IPv6Gateway": "",
                      "GlobalIPv6Address": "",
                      "GlobalIPv6PrefixLen": 0,
                      "MacAddress": "02:42:ac:1f:ff:e2",
                      "DriverOpts": null
                    }
                  }
                },
                "Mounts": [
                  {
                    "Type": "volume",
                    "Name": "nginx_etc",
                    "Source": "/mnt/dietpi_userdata/docker-data/volumes/nginx_etc/_data",
                    "Destination": "/etc/nginx",
                    "Driver": "local",
                    "Mode": "z",
                    "RW": true,
                    "Propagation": ""
                  },
                  {
                    "Type": "volume",
                    "Name": "nginx_log",
                    "Source": "/mnt/dietpi_userdata/docker-data/volumes/nginx_log/_data",
                    "Destination": "/var/log/nginx",
                    "Driver": "local",
                    "Mode": "z",
                    "RW": true,
                    "Propagation": ""
                  },
                  {
                    "Type": "volume",
                    "Name": "phpma_html",
                    "Source": "/mnt/dietpi_userdata/docker-data/volumes/phpma_html/_data",
                    "Destination": "/var/www/html/phpma",
                    "Driver": "local",
                    "Mode": "z",
                    "RW": true,
                    "Propagation": ""
                  }
                ]
              },

Could you make it work with any container?
Can you also run two containers without Portainer to make sure Portainer does not do something that I haven’t noticed? The network setting looks good, so if you can’t make it work with two simple container connecting to the same network to ping or curl eachoter, than you must have some firewall on your machine which does not allow the traffic between the containers.,

Hi Rimelek,
i ran Debian to test and to each of the containers i can ping them

i then tested curl to tiny file manager to prove it worked

they i tried to curl the container itself then to the nginx port i assigned in the config file

I have found something interesting, this is my current config file:

server {
        listen 82;
        server_name _;
        root /var/www/html/pma;
	index index.php index.html index.htm;
	set $upstream 172.31.255.228:9000;
	access_log /var/log/nginx/access/phpmyadmin82.log;
        error_log /var/log/nginx/error/phpmyadmin82.log debug;
		
		 location ^~ / {
            try_files $uri = 404;
            fastcgi_pass $upstream;
            fastcgi_index index.php;
            include /etc/nginx/params/fastcgi.conf;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_path_info;
        }

        location ~ \.php$ {
            try_files $uri = 404;
            fastcgi_pass $upstream;
            fastcgi_index index.php;
            include /etc/nginx/params/fastcgi.conf;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_path_info;
        }
    }

now my phpmyadmin /var/www/html folder is mapped to phpma_html

in nginx if i map the phpma_html to /var/www/html/ i get the below error"
“/var/www/html/pma/index.php” is not found (2: No such file or directory)" (404 in the browser


)

now in nginx if i map the phpma_html to /var/www/html/pma i get the this error:
“FastCGI sent in stderr: “Primary script unknown” while reading response header from upstream” (also 404 in the browser)
but in both instances i can load the favicon.ico for some reason.

Since the favicon is just a static file and all your location rules refer to PHP files, I don’t think a working favicon is surprising. Sorry, but I usually don’t configure Nginx manually and I never use it to directly connect to PHP. I think the problem is still not Docker here, but nginx configuration.

I think it’s an nginx config issue as well, thanks for all your help so far.

ok, so i tried to work with wordpress since it goes through fpm as well and i got it to work with this config

server {
    listen 83;
    server_name _;
root /var/www/wordpress;
index index.php;

access_log /var/log/nginx/access/wordpress.log;
error_log /var/log/nginx/error/wordpress.log debug;

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

location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 172.31.255.253:9000;
fastcgi_index index.php;
include /etc/nginx/params/fastcgi.conf;
fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}

so i tried a similar config

server {
    listen 82;
    server_name _;
root /var/www/pma;
index index.php;

access_log /var/log/nginx/access/pma.log;
error_log /var/log/nginx/error/pma.log debug;

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

location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 172.31.255.228:9000;
fastcgi_index index.php;
include /etc/nginx/params/fastcgi.conf;
fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}

but get the error: “502 Bad Gateway”

found the bad gateway issue, i had an upstream server in the config that was causing it and now it loads!
thanks for your help @rimelek

1 Like