502 Bad Gateway when using reverse proxy with Docker and Nginx

krcsximq  于 2024-01-06  发布在  Nginx
关注(0)|答案(2)|浏览(271)

我已经搜索了StackOverflow来查找我的问题,但我的Nginx Docker配置似乎总是命中502 Bad Gateway。我试图使用我的域mydomain.com/pgadmin而不是mydomain.com:8060访问pgadmin4,其中8060是它的Docker容器所公开的端口。我的docker-compose.yml文件如下所示:

  1. version: '3.5'
  2. services:
  3. reverse-proxy:
  4. image: nginx:1.19.6
  5. restart: always
  6. ports:
  7. - "80:80"
  8. - "443:443"
  9. postgres:
  10. image: postgres:12
  11. ports:
  12. - "5432:5432"
  13. pgadmin:
  14. image: dpage/pgadmin4
  15. depends_on:
  16. - postgres
  17. ports:
  18. - "8060:80"
  19. networks:
  20. default:
  21. external:
  22. name: defaultnetwork

字符串
我的nginx容器的default.conf文件如下所示:

  1. upstream pgadmin {
  2. server 127.0.0.1:8060;
  3. }
  4. server {
  5. listen 80;
  6. listen [::]:80;
  7. server_name mydomain.com;
  8. root /usr/share/nginx/html;
  9. index index.html index.htm;
  10. location /pgadmin {
  11. proxy_pass http://pgadmin;
  12. }
  13. }


在这种配置下,我总是得到502 Bad Gateway错误。有人能好心地指出我哪里出错了吗?我真的很感激。
谢谢.
[编辑]这是从Docker日志:

  1. 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"

0tdrvxhp

0tdrvxhp1#

502问题来自于以下的问题:
上游pgadmin {服务器127.0.0.1:8060; }
NGINX容器的127.0.0.1localhost是NGINX容器本身。您应该使用服务的名称:

  1. upstream pgadmin {
  2. server pgadmin:8060;
  3. }

字符串
服务名称来自docker-compose.yml

  1. services:
  2. pgadmin: # <- this
  3. image: dpage/pgadmin4


如果在这些更改后遇到404,这是因为您必须更改应用程序的基本路径。尝试使用此配置:

  1. location /pgadmin/ {
  2. proxy_set_header X-Script-Name /pgadmin;
  3. proxy_set_header Host $host;
  4. proxy_pass http://pgadmin;
  5. proxy_redirect off;
  6. }

展开查看全部
ibrsph3r

ibrsph3r2#

由于您的容器在同一个网络中工作,因此您应该通过Nginx容器的第80个端口访问Pgadmin容器。
您应该在Nginx配置中将这一行server 127.0.0.1:8060替换为server pgadmin:80

相关问题