# '768 worker\_connections' 不足错误

**URL:** <https://meta.discourse.org/t/768-worker-connections-are-not-enough-error/192376>\
**Category:** Self-hosting\
**Created:** [2021年六月2日 09:13 UTC](https://meta.discourse.org/t/768-worker-connections-are-not-enough-error/192376 "2021-06-02T09:13:19Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![bartv](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bartv/32/130052_2.png) [@bartv](https://meta.discourse.org/u/bartv)\
**Post date:** [2021年六月2日 09:13 UTC](https://meta.discourse.org/t/768-worker-connections-are-not-enough-error/192376/1 "2021-06-02T09:13:19Z")

</div>

嘿！

由于今天进行了重建，我们正经历大量的服务器错误。这似乎是一个 nginx 连接问题；在 nginx/error.log 中，我偶尔会看到如下爆发的 `768 worker_connections are not enough` 消息：

`2021/06/02 10:42:21 [alert] 1143#1143: *28468 1768 worker_connections are not enough while connecting to upstream, client: (IP removed), server: _, request: "POST /message-bus/8fc08436f86f47479cf0dad3deb5c4dc/poll?dlp=t HTTP/1.1", upstream: "http://127.0.0.1:3000/message-bus/8fc08436f86f47479cf0dad3deb5c4dc/poll?dlp=t", host: "blenderartists.org", referrer: "https://blenderartists.org/t/convert-multiple-objects-to-single-mesh-with-vertex-grouping/489173/2"`

有什么办法可以解决这个问题吗？我们的 CPU 和内存资源非常充足——能否增加“工作进程连接数”（worker connections）？

---

<div class="post-metadata">

**Author:** ![bartv](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bartv/32/130052_2.png) [@bartv](https://meta.discourse.org/u/bartv)\
**Post date:** [2021年六月2日 10:43 UTC](https://meta.discourse.org/t/768-worker-connections-are-not-enough-error/192376/2 "2021-06-02T10:43:11Z")

</div>

更新：我暂时增加了工作进程连接数，但依然会出现这些错误（频率降低，且在高工作进程数量下出现）。我真的很想知道最近是否有某些变更可能导致此问题，或者我该如何更好地追踪定位。

```plaintext
## 构建后需要运行的任何自定义命令
run:
  - exec: echo "开始执行自定义命令"

  - replace:
      filename: "/etc/nginx/letsencrypt.conf"
      from: "worker_connections 768" 
      to: "worker_connections 1768"

```

---

<div class="post-metadata">

**Author:** ![DrewH](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/drewh/32/222752_2.png) [@DrewH](https://meta.discourse.org/u/DrewH)\
**Post date:** [2021年六月2日 14:51 UTC](https://meta.discourse.org/t/768-worker-connections-are-not-enough-error/192376/3 "2021-06-02T14:51:47Z")

</div>

有趣的是，这在重建后发生了。您最近是否执行过任何批量操作？建议您检查 Sidekiq 日志，看看那里是否也有大量任务。

---

<div class="post-metadata">

**Author:** ![bartv](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bartv/32/130052_2.png) [@bartv](https://meta.discourse.org/u/bartv)\
**Post date:** [2021年六月2日 15:25 UTC](https://meta.discourse.org/t/768-worker-connections-are-not-enough-error/192376/4 "2021-06-02T15:25:06Z")

</div>

我最近确实执行了一些批量操作，因为我们切换到了缩略图预览 TC，但我的 Sidekiq 队列中没有任何任务，我可以肯定地排除这一点。

---

<div class="post-metadata">

**Author:** ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)\
**Post date:** [2021年六月2日 15:28 UTC](https://meta.discourse.org/t/768-worker-connections-are-not-enough-error/192376/5 "2021-06-02T15:28:58Z")

</div>

我们两天前升级了 nginx 版本，所以请密切关注。您的网站是否有超过 500 个并发访问者？

另外，您的整个网站都通过 Cloudflare 托管，因此情况可能会因此有所不同。

---

<div class="post-metadata">

**Author:** ![bartv](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bartv/32/130052_2.png) [@bartv](https://meta.discourse.org/u/bartv)\
**Post date:** [2021年六月2日 15:31 UTC](https://meta.discourse.org/t/768-worker-connections-are-not-enough-error/192376/6 "2021-06-02T15:31:45Z")

</div>

> [@Falco](#):
>
> 您的网站是否有超过 500 名并发访问者？

我不确定——也许有？有什么方法可以检查吗？

> [@Falco](#):
>
> 另外，您的整个网站都位于 Cloudflare 之后，因此情况可能会因此有所不同。

没错。但我已禁用所有加速功能，基本上只用它来缓存图片和头像。直到今天之前，这从未成为问题。

---

<div class="post-metadata">

**Author:** ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)\
**Post date:** [2021年六月2日 15:48 UTC](https://meta.discourse.org/t/768-worker-connections-are-not-enough-error/192376/7 "2021-06-02T15:48:33Z")

</div>

> [@bartv](#):
>
> 我完全没头绪——我们或许可以试试？有什么办法可以检查吗？

哈哈，通常人们会使用 Google Analytics 或类似工具来获取这类信息。Discourse 仪表盘也提供了每日页面浏览量（pageviews）和用户访问数，可以用来进行类似的估算。

> [@bartv](#):
>
> 我已经禁用了任何加速功能。

事实并非如此，您的整个网站都是通过 Cloudflare 提供的：

```plaintext
curl -I https://blenderartists.org/ \nHTTP/2 200 \ncf-cache-status: DYNAMIC\ncf-request-id: 0a6ef945b3000002fe272b2000000001\nserver: cloudflare\ncf-ray: 6591c4b5ec5902fe-MIA\nalt-svc: h3-27=":443"; ma=86400, h3-28=":443"; ma=86400, h3-29=":443"; ma=86400, h3=":443"; ma=86400

```

不过，这可能与问题完全无关，因为您的 nginx 报错的是 **上游** （upstream）连接问题，而非下游连接。这意味着 nginx 与 unicorn 之间的连接资源已耗尽。

由于我们借助 message\_bus（实时更新服务）为每位访客保持一个开放连接，如果您的网站有一定人气，这种情况是可以预期的。

调大 `worker_processes` 和 `worker_connections` 是安全的，而且在您的情况下听起来很合理。我们默认将 `worker_processes` 设置为您的 CPU 核心数。请问您有多少个 CPU 核心？

---

<div class="post-metadata">

**Author:** ![bartv](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bartv/32/130052_2.png) [@bartv](https://meta.discourse.org/u/bartv)\
**Post date:** [2021年六月2日 15:55 UTC](https://meta.discourse.org/t/768-worker-connections-are-not-enough-error/192376/8 "2021-06-02T15:55:27Z")

</div>

> [@Falco](#):
>
> 哈哈，通常人们会使用 Google Analytics 或类似工具来获取这类信息。Discourse 仪表盘提供每日页面浏览量和用户访问量，也可以用来估算这些数据。

确实如此 🙂 我们很久以前就停用那个了……我们每天的页面浏览量约为 25 万（包含机器人），所以 500 看起来并不算异常。用户访问量是不是只统计登录后的访问？

> [@Falco](#):
>
> 并非如此，你的整个网站都是通过 Cloudflare 提供服务的：

没错——我们确实需要通过 Cloudflare 转发请求，但我们不会让它们触碰我们的 JavaScript 等文件。

> [@Falco](#):
>
> 增加 `worker_processes` 和 `worker_connections` 是安全的，听起来在你的情况下也很合理。我们将 `worker_processes` 默认设置为你 CPU 的核心数。你有多少个 CPU 核心？

我们有 12 个核心，64GB 内存。典型负载约为 2，我们使用了 50% 的内存。

---

<div class="post-metadata">

**Author:** ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)\
**Post date:** [2021年六月2日 16:10 UTC](https://meta.discourse.org/t/768-worker-connections-are-not-enough-error/192376/9 "2021-06-02T16:10:33Z")

</div>

> [@bartv](#):
>
> 我们有 12 个核心，64GB 内存。典型负载约为 2，我们使用了 50% 的 RAM。

天哪，这也太奇怪了！

连接数的计算公式是 `worker_processes * worker_connections`，即 `12 * 768`，结果应该是（敲键盘声）9216。但你的日志显示是 1768……

在你的 app.yml 上尝试以下操作：

```plaintext
## 构建后运行的任何自定义命令
run:
  - exec: echo "开始执行自定义命令"

  - replace:
      filename: "/etc/nginx/nginx.conf"
      from: "worker_connections 768" 
      to: "worker_connections 2000"
  - replace:
      filename: "/etc/nginx/nginx.conf"
      from: "worker_processes auto" 
      to: "worker_processes 10"

```

请注意，你第 2 个帖子中的操作块针对的是错误的文件！

---

<div class="post-metadata">

**Author:** ![bartv](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bartv/32/130052_2.png) [@bartv](https://meta.discourse.org/u/bartv)\
**Post date:** [2021年六月2日 17:08 UTC](https://meta.discourse.org/t/768-worker-connections-are-not-enough-error/192376/10 "2021-06-02T17:08:08Z")

</div>

:facepalm: 我贴错了代码——我先试了 letsencrypt 模板，但后来把 nginx.conf 改成了 1768 个工作连接。

我会试试你的参数，稍后回来反馈结果。

---

<div class="post-metadata">

**Author:** ![bartv](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bartv/32/130052_2.png) [@bartv](https://meta.discourse.org/u/bartv)\
**Post date:** [2021年六月2日 17:41 UTC](https://meta.discourse.org/t/768-worker-connections-are-not-enough-error/192376/11 "2021-06-02T17:41:45Z")

</div>

恐怕问题依然存在：

```plaintext
2021/06/02 17:40:03 [alert] 2102#2102: *262491 在连接到上游服务器时，2000 个工作连接数不足，客户端：<ip removed>，服务器：_，请求："POST /message-bus/0e453fae0c604c29a876e6ede05b7341/poll?dlp=t HTTP/1.1"，上游："http://127.0.0.1:3000/message-bus/0e453fae0c604c29a876e6ede05b7341/poll?dlp=t"，主机："blenderartists.org"，引用页："https://blenderartists.org/t/weight-paint-not-painting/551282"

```

---

<div class="post-metadata">

**Author:** ![bartv](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bartv/32/130052_2.png) [@bartv](https://meta.discourse.org/u/bartv)\
**Post date:** [2021年六月3日 13:30 UTC](https://meta.discourse.org/t/768-worker-connections-are-not-enough-error/192376/12 "2021-06-03T13:30:02Z")

</div>

我已将 worker\_connections 提升至 4000，目前情况良好：crossed\_fingers:

---

<div class="post-metadata">

**Author:** ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)\
**Post date:** [2022年二月18日 17:12 UTC](https://meta.discourse.org/t/768-worker-connections-are-not-enough-error/192376/13 "2022-02-18T17:12:21Z")

</div>

现在我们更容易覆盖了：

> <https://github.com/discourse/discourse_docker/pull/604>
>
> MessageBus connections on busy sites can easily use more than the max
> default nu…mber of connections.
> 
> Refer to http://nginx.org/en/docs/ngx\_core\_module.html#worker\_connections

---

<div class="post-metadata">

**Author:** ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)\
**Post date:** [2022年二月18日 19:48 UTC](https://meta.discourse.org/t/768-worker-connections-are-not-enough-error/192376/14 "2022-02-18T19:48:48Z")

</div>

太棒了！那么我们会在 `app.yml`/`web_only.yml` 中做类似这样的事情吗？

```plaintext
params:
  nginx_worker_connections: 4000

```

---

<div class="post-metadata">

**Author:** ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)\
**Post date:** [2022年二月18日 19:51 UTC](https://meta.discourse.org/t/768-worker-connections-are-not-enough-error/192376/15 "2022-02-18T19:51:25Z")

</div>

确实如此。我们在同一个补丁中还将默认值提高到了 4k，因此管理员可能需要仔细评估他们是否仍需要提高该值。

---

<div class="post-metadata">

**Author:** ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)\
**Post date:** [2022年二月18日 19:54 UTC](https://meta.discourse.org/t/768-worker-connections-are-not-enough-error/192376/16 "2022-02-18T19:54:11Z")

</div>

在一个网站上，我还将工作进程的 CPU 数量增加了一倍。我需要也删除那个吗？

---

<div class="post-metadata">

**Author:** ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)\
**Post date:** [2022年三月20日 19:54 UTC](https://meta.discourse.org/t/768-worker-connections-are-not-enough-error/192376/17 "2022-03-20T19:54:27Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
