使用 NGINX 作为 Postgres 代理的超时问题
Timeout issues using NGINX as a Postgres Proxy
我正在设置一个代理服务器来拦截所有流量并将其转发到我的 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 分钟连接超时 stream/proxy?
- 有谁知道使用 NGINX 以外的东西来完成我正在尝试做的事情的更好替代方法吗?
- 我在 Ubuntu 上尝试了
simpleproxy
,但是 2 小时后超时(是的,我们必须支持一些长的 运行ning 查询,例如 VACUUM
十亿以上行表的操作)
您在 /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 中的 运行 个进程以确认它何时完成。
我正在设置一个代理服务器来拦截所有流量并将其转发到我的 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 分钟连接超时 stream/proxy?
- 有谁知道使用 NGINX 以外的东西来完成我正在尝试做的事情的更好替代方法吗?
- 我在 Ubuntu 上尝试了
simpleproxy
,但是 2 小时后超时(是的,我们必须支持一些长的 运行ning 查询,例如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 中的 运行 个进程以确认它何时完成。