PostgreSQL 的 NGINX TLS 终止

2024-01-09

我一直在尝试使用 NGINX 作为 PostgreSQL 数据库的 TLS 终结器,但没有成功。

当我尝试连接到数据库时,出现以下错误:

服务器意外关闭连接 这可能意味着服务器在处理请求之前或处理过程中异常终止。

当我删除ssl选项中listen我可以连接,没有任何错误。 我尝试过使用相同的 NGINX 设置运行另一个服务(Eclipse Mosquitto),启用了 TLS,并且工作正常。

我使用 Postico 作为数据库工具。

这是我正在使用的 NGINX 设置。

# nginx.conf

stream {
    server {
          listen 20000 ssl; # Can’t connect with postgre but with mosquito
          # listen 20000; # Can connect with postgre and mosquitto
          proxy_pass 192.168.1.123:30000;
          include /home/custom/ssl_conf.conf;
    }
}

# ssl_conf.conf

ssl_certificate           /etc/nginx/fullchain.pem;
ssl_certificate_key       /etc/nginx/privkey.pem;
ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
ssl_dhparam /etc/nginx/ssl/dhparam.pem;
ssl_protocols TLSv1.2;
ssl_prefer_server_ciphers on;

简而言之:这是不可能的,因为 PostgreSQL 在 SSL 握手之前有自己的握手。

为了避免这种情况,您可以简单地将 PostgreSQL 设置为在其级别使用 SSL,并使用 Nginx 的 TCP 流作为传递(通信由 PostgreSQL 进行端到端加密)。

Source: https://www.postgresql.org/message-id/d05341b9-033f-d5fe-966e-889f5f9218e5%40proxel.se https://www.postgresql.org/message-id/d05341b9-033f-d5fe-966e-889f5f9218e5%40proxel.se

可悲的是,这证实了我的担心。将 SNI 添加到 PostgreSQL 协议 不会帮助解决您的用例,因为 PostgreSQL 协议有 它自己的握手发生在 SSL 握手之前,因此会话 看起来不像 SSL 到 HA 代理。

就像 HA 代理不支持 IMAP[1] 的 STARTTLS 一样,我不认为 无论 SNI 与否,它将永远支持 PostgreSQL 协议的 SSL。

为了解决您的用例,我建议使用像 stunnel 这样的东西,它 支持 SNI,将未加密的 PostgreSQL 协议封装在 SSL 中。

在 Nginx 的 TCP 流级别设置 SSL 将触发以下错误(也在此报告:https://www.postgresql.org/message-id/flat/15688-55463748a04474a5%40postgresql.org https://www.postgresql.org/message-id/flat/15688-55463748a04474a5%40postgresql.org):

  • 当尝试连接时psql你得到:
psql: error: server closed the connection unexpectedly
    This probably means the server terminated abnormally
    before or while processing the request.
  • 当查看 Nginx 错误日志时,您会看到:
2021/02/01 19:18:01 [info] 6175#6175: *3 client 127.0.0.1:57496 connected to 127.0.0.1:5433
2021/02/01 19:18:01 [info] 6175#6175: *3 SSL_do_handshake() failed (SSL: error:1408F10B:SSL routines:ssl3_get_record:wrong version number) while SSL handshaking, client: 127.0.0.1, server: 127.0.0.1:5433
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

PostgreSQL 的 NGINX TLS 终止 的相关文章

随机推荐