PGAdmin4 配置在反向代理后面,但无法连接到 Postgresql 服务器

问题描述 投票:0回答:1

我在通过 SSH 访问的 Debian 11 主机上安装了 Posgresql 16 服务器,当我使用 PSQL cli 时,一切都按预期工作。 然后我安装并配置了 PGAdmin4 Web,并使用反向代理配置了 NGINX,以便我可以从浏览器访问 PGAdmin4 Web 界面。

防火墙配置正确。

NGINX 指令如下:

    location /pgadmin4/ {
         proxy_pass http://127.0.0.1:80/pgadmin4/;
         proxy_redirect off;
         proxy_set_header Host $host;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_set_header X-Forwarded-Proto $scheme;

         proxy_set_header X-Script-Name /pgadmin4;
         proxy_buffering off;
    }

我可以通过 URL 访问 PGAdmin4 Web 界面: https:///pgadmin4/

问题是我无法通过服务器对话框连接到 Postgresql 服务器,我系统地收到一条错误消息: 无法连接到服务器:连接超时已过。

在文件 /usr/pgadmin4/web/config.py 中,我将 DEFAULT_SERVER = '127.0.0.1' 替换为 DEFAULT_SERVER = '0.0.0.0'。

在文件 /etc/postgresql/16/main/pg_hba.conf 中,我添加了以下行:

host    all             all             0.0.0.0/0               md5

在 PGAdmin4 对话框中,我配置了以下选项(假设主机名是 example.tld):

连接选项卡:

  • 主机名/地址: example.tld
  • 维护数据库:postgres
  • 用户名:postgres

SSH 隧道选项卡:

  • 隧道主机:example.tld
  • 隧道端口:22
  • 用户名:pgadmin(普通 Unix 用户)
  • 身份验证:密码

我还创建了一个新的数据库(书店),我可以使用以下命令从 shell 中的用户“pgadmin”下直接访问该数据库,并相应地修改了连接选项卡:

psql -U pgadmin -d bookstore -h 127.0.0.1 -p 5432 -W

我在网上阅读了无数文档,但仍然没有运气。

我被困住了,任何帮助将不胜感激。

我在“连接”选项卡中尝试了使用不同数据库和用户的配置,其中 PSQL 没有任何问题。 当我尝试使用端口 5432 时,我立即收到一条错误消息,如下所示: Cannot use port 5432 because behind a reverse proxy on port 80

在 SSH 隧道选项卡中,当我使用不存在的用户(例如 test)或错误的密码时,我会收到不同的错误消息:Error when a wrong user or wrong password is used

postgresql reverse-proxy pgadmin
1个回答
0
投票

postgres CLI 工具默认使用

local
类连接,在
/etc/postgres/16/pg_hba.conf
中配置,默认为:

...
# Database administrative login by Unix domain socket
local   all             postgres                                peer
...
# "local" is for Unix domain socket connections only
local   all             all                                     peer
...

local
套接字是unix 套接字。

因为你说可以使用

psql
CLI 连接,并且假设你没有通过
psql -h HOST
提供主机,这意味着你的 postgres 服务器已配置(在
pg_hba.conf
中)以允许通过 unix 套接字进行连接。因此,你应该能够告诉 pgadmin 使用
local
连接,通过使用 unix 套接字路径作为主机名,并禁用 SSH 隧道(SSH 隧道仅当 pgadmin 和 postgres 服务器位于不同的主机上时才有用)

所以查看

pg_hba.conf
并找出启用了什么。

如果你想允许 pgadmin 通过

localhost
(而不是
local
/ unix 套接字)连接,那么你需要向
pg_hba.conf
添加这样一行:

# IPv4 local connections:
host    all             all             127.0.0.1/32            md5

# IPv6 local connections:
host    all             all             ::1/128                 md5

还有其他 postgresql 配置文件,它们将启用 unix 套接字或指定要侦听的 IP 地址。

© www.soinside.com 2019 - 2024. All rights reserved.