1

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 2

6

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;
    }
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @anemyte for this. I am one step closer. From the logs I can see that nginx correctly resolves the container's IP address. But the request looks like this: GET /pgadmin HTTP/1.1", upstream: "http://172.18.0.6:8082/pgadmin instead of this: GET /pgadmin HTTP/1.1", upstream: "http://172.18.0.6:8082. How can I fix this?
@realnsleo it works on port 80 by default, so you shouldn't specify port unless you've changed it in pgadmin config. That is just proxy_pass http://pgadmin as it was before.
Ah! Yes. Sorry. I have removed the ports from the docker-compose.yml file and I am now getting a 404 error. Any ideas?
@realnsleo I've managed to dig out a solution to it in web archive. I'll update the answer.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.