【问题标题】:Nginx upstream timeout error while running ruby on rails query在 Rails 查询上运行 ruby​​ 时 Nginx 上游超时错误
【发布时间】:2019-03-10 09:28:15
【问题描述】:

在 ruby​​ on rails 应用程序中,我有一个包含约 10,000 个条目的表,可以使用不同的参数进行搜索。在开发盒上,这工作正常,但在生产盒上,我得到一个错误。

2018/10/04 15:46:39 [error] 3418#3418: *6 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 192.168.1.5, server: my.site.com, request: "POST /quotes/quoteTable_js HTTP/1.1", upstream: "http://unix:///path/to/app/shared/tmp/sockets/puma.awi_staging.sock/items/itemTable_js", host: "192.168.1.25", referrer: "http://192.168.1.25/items"

我没有设置服务器,所以这里有点不够深入。我查看了以下问题

但是,这些都不起作用,或者我没有正确实现它们。

我的 nginx.conf 文件

user nginx;
worker_processes auto;
pid /run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

编辑

我在大约 7 秒后收到“我们很抱歉,但出了点问题”的 rails 消息。我尝试增加 keepalive_timeout,但没有任何改变。

【问题讨论】:

  • 问题在于您的 RoR 应用程序,而不是 Nginx。尝试使用 Nginx 修复它可能会浪费时间。有关您的 RoR 应用程序的更多信息将在此处更相关(即日志,或者可能有关数据库架构和查询的详细信息)
  • 您的生产应用程序很可能在集群模式下运行 Puma。这将需要更改/更新 ActiveRecord 初始化 - 特别是在分叉之前/之后关闭任何现有的数据库连接。未能管理 before_fork/after_fork 回调(或 on_worker_boot)可能会导致您描述的错误。

标签: ruby-on-rails nginx puma


【解决方案1】:

keepalive_timeout 参数控制空闲客户端连接保持打开多长时间以节省可能的重新连接成本,这不是您的问题所在。

对于上游超时有proxy_connect_timeoutproxy_send_timeoutproxy_read_timeout(nginx默认是60s,但你的配置文件中似乎有这些较低),你可以尝试增加后两者,但通常没有希望服务器响应时间这么长 - 因为长请求会阻塞工作人员,并且客户端可能会开始为所有请求超时,而不仅仅是“重”请求。

【讨论】:

  • 好的,谢谢。我现在不在办公室,所以我无法检查。我假设这将在底部的包含文件中(nginx/conf.d/*),或者如果没有,我只需将它添加到 nginx.conf 的 http 部分?
  • 首先要查找的是serversites-enabled/*中的块
猜你喜欢
  • 2017-03-25
  • 2018-02-07
  • 1970-01-01
  • 2015-06-16
  • 2021-02-23
  • 2015-01-27
  • 1970-01-01
  • 2014-03-01
  • 2018-08-17
相关资源
最近更新 更多