【问题标题】:Inconsistent timeout errors on nginx servernginx 服务器上出现不一致的超时错误
【发布时间】:2016-02-18 04:15:11
【问题描述】:

我有一个应用程序试图从 Javascript 应用程序发出 ajax 请求,以从 nginx 服务器检索 xml 文件。通常一切都很好,但我经常在 nginx 日志中看到错误(并从我的应用程序错误报告中得到错误),即 nginx 在获取期间遇到超时:

2015/11/16 21:15:21 [error] 1208#0: *4894044 upstream timed out (110: Connection timed out) while connecting to upstream, client: 209.95.138.54, server: www.servername.com, request: "GET /Shape%20Textures/Metal/Born%20to%20Shine.jpg?agentView=436314 HTTP/1.1", upstream: "http://127.0.0.1:3000/AQO/Shape%20Textures/Metal/Born%20to%20Shine.jpg?agentView=436314", host: "www.servername.com.com", referrer: "https://www.servername.com.com/?nid=39956&mode=edit"

我们有时也会遇到类似的错误:

2015/11/17 19:03:16 [error] 1002#0: *54042 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 137.164.121.52, server: www.servername.com, request: "POST /projects/?q=node/36375/update_project_time_and_stats HTTP/1.1", upstream: "http://127.0.0.1:3000/projects/?q=node/36375/update_project_time_and_stats", host: "www.servername.com", referrer: "https://www.servername.com/AQO/?nid=36375&mode=edit"

我似乎有类似的帖子有类似的超时,但所有这些帖子似乎都可以重现。这个问题从来没有发生在我身上,但是在实时服务器上运行时,我每天会看到 5-30 次这样的超时。

这是我的 nginx 配置:

client_max_body_size 50M;

server {
    server_name servername.com;
    return 301 $scheme://www.servername.com$request_uri;
}

server {
    listen 80;
    listen 443 ssl;
    fastcgi_read_timeout 120;
    ssl_certificate /path/to/ssl/star_servername_com.pem;
    ssl_certificate_key /path/to/ssl/star_servername_com.key;

    # Redirect all non-SSL traffic to SSL.
    if ($ssl_protocol = "") {
      rewrite ^ https://$host$request_uri? permanent;
    }

    root /usr/share/nginx/html;
    index index.html index.htm;

    # Make site accessible from http://localhost/
    server_name www.servername.com;

    location / {
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_pass http://127.0.0.1:3000;
            proxy_read_timeout 120s;
    }
}

由于我无法重现它,我想知道如何才能找到这个问题?

【问题讨论】:

  • 检查你的上游服务器日志?
  • 我从没听说过上游服务器日志,用谷歌搜索也没找到太多,这个可以通过 nginx 启用吗?
  • 我指的是127.0.0.1:3000服务器日志。
  • 有一个 node.js 服务器在端口 3000 上运行。没有与超时相关的内容。

标签: nginx timeout reverse-proxy


【解决方案1】:

连接到时上游超时(110:连接超时) 上游

表示你的上游服务器没有及时接受连接

读取响应时上游超时(110:连接超时) 来自上游的标头

表示你的上游服务器没有及时响应

所以请检查您的上游服务器 127.0.0.1:3000。它可能设置了少量传入连接或某种 DDoS 保护,或者此时负载非常重或其他。

【讨论】:

  • 有一个 node.js 服务器在端口 3000 上运行,但据我所知 node.js 对传入连接没有任何限制。 Ubuntu 的默认传入连接为 1024,由 ulimit -n 显示
  • 如果你的 Node.js 服务器负载很重或者运行很长的任务,你可以尝试增加 nginx 的 proxy_connect_timeout 和 proxy_read_timeout。它们的默认值为 60 秒,您可能需要增加此设置
【解决方案2】:

我想参考this answer,它显示了如何优化您的设置。

虽然不是最好的解决方案,但很大程度上取决于您想要实现的目标,您可以简单地将proxy_read_timeout 增加到例如300。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-10
    • 1970-01-01
    • 2020-06-28
    • 2021-03-24
    • 2019-04-01
    相关资源
    最近更新 更多