我一直在尝试使用NGINX作为我的PostgreSQL数据库的TLS终结器,但没有成功。
当我尝试连接数据库时,出现以下错误:
服务器意外关闭了连接 这可能意味着服务器在处理请求之前或处理时异常终止。
当我删除 listen 中的 ssl 选项时,我可以连接而没有任何错误。 我尝试使用相同的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;
答案 0 :(得分:0)
如该similar question中所述,问题可能是Postgres使用了应用程序级协议协商,因此简单的代理可能不起作用。另请参见
答案 1 :(得分:0)
简而言之:这是不可能的,因为 PostgreSQL 在 SSL 握手之前有自己的握手。
为了避免这种情况,您可以简单地将 PostgreSQL 设置为在其级别使用 SSL,并使用 Nginx 的 TCP 流作为传递(通信是端到端加密的)。
来源:https://www.postgresql.org/message-id/d05341b9-033f-d5fe-966e-889f5f9218e5%40proxel.se
<块引用>可悲的是,这证实了我的担心。将 SNI 添加到 PostgreSQL 协议 不会帮助解决您的用例,因为 PostgreSQL 协议有 它自己的握手发生在 SSL 握手之前,因此会话 看起来不像 SSL 到 HA 代理。
<块引用>就像 HA 代理不支持 IMAP 的 STARTTLS[1] 我不认为 它将永远支持 PostgreSQL 协议的 SSL,无论 SNI 与否。
<块引用>为了解决您的用例,我建议使用类似 stunnel 的东西,它 支持 SNI,将未加密的 PostgreSQL 协议封装在 SSL 中。
在 Nginx 的 TCP 流级别设置 SSL 会触发以下错误(此处也报告: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.
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