我正在设置一个代理服务器来拦截所有流量并将其转发到我的 Postgres 数据库。我正在使用 NGINX 1.19.2 来完成此任务。
我遇到的问题是任何运行时间超过 10 分钟的查询都会超时。所有较短的查询似乎都运行得很好。
这是我的整个 nginx.conf
:
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
}
stream {
server {
# Allow up to 30s to establish a connection with the proxied database
proxy_connect_timeout 30s;
proxy_socket_keepalive on;
listen 5432;
proxy_pass my-server.rds.amazonaws.com:5432;
}
}
我已经尝试了几个与 NGINX 代理相关的配置选项(例如 proxy_read_timeout
),但是,这些选项在 stream { }
上下文中不起作用,并且需要在 http { }
上下文中。为了连接到 Postgres,我不能使用 http。
- 有谁知道我可以做些什么来消除 NGINX 流/代理的 10 分钟连接超时?
- 有谁知道使用 NGINX 以外的东西来完成我正在尝试做的事情的更好替代方法吗?
- 我在 Ubuntu 上尝试了
simpleproxy
,但在 2 小时后超时(是的,我们必须支持一些长时间运行的查询,例如对十亿行以上的表的VACUUM
操作)
- 我在 Ubuntu 上尝试了
最佳答案
您是否在 /var/log/nginx/error.log
中看到与这些请求相关的任何错误?
如果您看到 Connection reset by peer
这意味着您的上游 (Postgres) 正在关闭连接,而不是 Nginx。
还有,你确定不是你的下游?您是直接连接到此服务器还是有负载平衡器 -> Nginx -> Postgres?负载均衡器可能正在终止连接。
我会尝试将所有超时设置设置得高得离谱,它们会进入 server {}
上下文
proxy_cache_lock_timeout
proxy_connect_timeout
proxy_next_upstream_timeout
proxy_read_timeout
proxy_send_timeout
client_body_timeout
client_header_timeout
keepalive_timeout
lingering_timeout
resolver_timeout
send_timeout
# Not sure if you're using fastcgi
fastcgi_cache_lock_timeout
fastcgi_connect_timeout
fastcgi_next_upstream_timeout
fastcgi_read_timeout
fastcgi_send_timeout
请参阅文档以了解每个内容:
- http://nginx.org/en/docs/http/ngx_http_proxy_module.html
- http://nginx.org/en/docs/http/ngx_http_core_module.html
- http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html
对于 VACUUM
之类的事情,连接可能会终止,但进程可能仍在运行。您可以在 Postgres 中检查正在运行的进程以确认它何时完成。
关于ubuntu - 使用 NGINX 作为 Postgres 代理的超时问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63929218/