skip to Main Content

I have searched StackOverflow for my problem but I always seem to be hitting the 502 Bad Gateway with my Nginx Docker configuration. I am trying to access pgadmin4 using my domain mydomain.com/pgadmin instead of mydomain.com:8060 where 8060 is the port exposed by it’s docker container. My docker-compose.yml file looks like this:

version: '3.5'

services:
  reverse-proxy:
    image: nginx:1.19.6
    restart: always
    ports:
      - "80:80"
      - "443:443"
        
  postgres:
    image: postgres:12
    ports:
      - "5432:5432"
        
  pgadmin:
    image: dpage/pgadmin4
    depends_on:
      - postgres
    ports:
      - "8060:80"
      
networks:
  default:
    external:
      name: defaultnetwork

The default.conf file of my nginx container looks like this:

upstream pgadmin {
    server 127.0.0.1:8060;
}

server {
    listen       80;
    listen  [::]:80;
    server_name  mydomain.com;
    
    root   /usr/share/nginx/html;
    index  index.html index.htm;
    
    location /pgadmin {
        proxy_pass http://pgadmin;
    } 
}

With this configuration, I keep getting the 502 Bad Gateway error. Could someone kindly point to me where I am going wrong. I would really appreciate it.

Thanks.

[EDIT] This is from the docker logs:

2021/02/03 08:07:42 [error] 23#23: *2 connect() failed (111: Connection refused) while connecting to upstream, client: ***.***.***.***, server: mydomain.com, request: "GET /pgadmin HTTP/1.1", upstream: "http://127.0.0.1:8082/pgadmin", host: "mydomain.com"

2

Answers


  1. The 502 problem comes from the loopback IP here:

    upstream pgadmin {
    server 127.0.0.1:8060;
    }

    127.0.0.1 or localhost for the NGINX container is the NGINX container itself. You should use the name of the service instead:

    upstream pgadmin {
        server pgadmin:8060;
    }
    

    Name of the service comes from the docker-compose.yml:

    services:
      pgadmin: # <- this
        image: dpage/pgadmin4
    

    If you hit 404 after these changes, this is because you have to change base path of the application. Try using this config:

        location /pgadmin/ {
            proxy_set_header X-Script-Name /pgadmin;
            proxy_set_header Host $host;
            proxy_pass http://pgadmin;
            proxy_redirect off;
        }
    
    Login or Signup to reply.
  2. Since your containers are working in the same network, you should access the Pgadmin container via 80th port from your Nginx container.

    You should replace this line server 127.0.0.1:8060 with server pgadmin:80 in your Nginx config.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search