# 如何丢弃带有空用户代理的请求？

**URL:** <https://meta.discourse.org/t/how-to-drop-requests-with-an-empty-user-agent/259069>\
**Category:** Self-hosting\
**Created:** [2023年三月22日 16:38 UTC](https://meta.discourse.org/t/how-to-drop-requests-with-an-empty-user-agent/259069 "2023-03-22T16:38:25Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![CubeCoders](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cubecoders/32/298429_2.png) [@CubeCoders](https://meta.discourse.org/u/CubeCoders)\
**Post date:** [2023年三月22日 16:38 UTC](https://meta.discourse.org/t/how-to-drop-requests-with-an-empty-user-agent/259069/1 "2023-03-22T16:38:26Z")

</div>

我的 Discourse 网站收到了很多用户代理为空的恶意机器人/爬虫的访问。通常我会编辑 nginx 配置来处理这个问题，但在使用 Docker 安装方法时无法访问它。如果可以避免的话，我不想再添加一个反向代理来处理这个问题。

---

<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:** [2023年三月22日 23:27 UTC](https://meta.discourse.org/t/how-to-drop-requests-with-an-empty-user-agent/259069/2 "2023-03-22T23:27:35Z")

</div>

您可以参考 [discourse\_docker/templates/web.ratelimited.template.yml at main · discourse/discourse\_docker · GitHub](https://github.com/discourse/discourse_docker/blob/main/templates/web.ratelimited.template.yml) 来了解如何在容器内修改 nginx 配置。

---

<div class="post-metadata">

**Author:** ![CubeCoders](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cubecoders/32/298429_2.png) [@CubeCoders](https://meta.discourse.org/u/CubeCoders)\
**Post date:** [2023年三月26日 11:53 UTC](https://meta.discourse.org/t/how-to-drop-requests-with-an-empty-user-agent/259069/3 "2023-03-26T11:53:33Z")

</div>

如果其他人想专门做这件事，我创建了 `/var/discourse/templates/web.blockemptyua.yml`，内容如下：

```yml
run:
  - replace:
     filename: "/etc/nginx/conf.d/discourse.conf"
     from: /listen 443 ssl http2;/
     to: |
       listen 443 ssl http2;
       if ($http_user_agent = "") { return 403; }

```

然后在 `/var/discourse/containers/app.yml` 中，我将这个新的模板文件添加到了文件顶部的模板列表的_末尾_，然后运行 `./launcher rebuild app`。

Nginx 现在会拒绝所有 UA 为空的请求。

编辑：已将匹配/替换移到更好的位置。

---

<div class="post-metadata">

**Author:** ![Jagster](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jagster/32/192154_2.png) [@Jagster](https://meta.discourse.org/u/Jagster)\
**Post date:** [2023年三月26日 12:50 UTC](https://meta.discourse.org/t/how-to-drop-requests-with-an-empty-user-agent/259069/4 "2023-03-26T12:50:30Z")

</div>

我不知道 Docker，所以为了安全起见，因为我每次看到 `replace` 都会感到紧张……

这不是完全替换 `discourse.conf` 的内容，而是添加内容，对吗？

---

<div class="post-metadata">

**Author:** ![CubeCoders](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cubecoders/32/298429_2.png) [@CubeCoders](https://meta.discourse.org/u/CubeCoders)\
**Post date:** [2023年三月26日 13:46 UTC](https://meta.discourse.org/t/how-to-drop-requests-with-an-empty-user-agent/259069/5 "2023-03-26T13:46:28Z")

</div>

它会替换文件中的一行。它会找到第一个实例：

```nginx
listen 443 ssl http2;

```

并将其替换为：

```nginx
listen 443 ssl http2;
if ($http_user_agent = "") { return 403; }

```

这样我们只是在正确的位置向文件中添加一个新行。文件的其余部分都不会被触及。它使用与官方模板相同的机制来安全地修补文件。

---

<div class="post-metadata">

**Author:** ![Jagster](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jagster/32/192154_2.png) [@Jagster](https://meta.discourse.org/u/Jagster)\
**Post date:** [2023年三月26日 21:49 UTC](https://meta.discourse.org/t/how-to-drop-requests-with-an-empty-user-agent/259069/6 "2023-03-26T21:49:41Z")

</div>

正如我之前所说，我不知道 Docker。我只是一个只会复制粘贴的普通用户，拥有博士学位。

因此，给未来搜索者的一个提示：

`web.blockemptyua.yml` 不能是声明模板的第一个。更准确地说，它必须出现在 `web.template.yml` 之后（或者甚至在所有 `web.*` 模板之后）。

我认为这里的顺序很重要，如果我们尝试将其作为第一个模板，重建/引导将因错误而停止，因为那时还没有 `/etc/nginx/conf.d/discourse.conf` 文件。

嗯，每天都有新东西 😉

---

<div class="post-metadata">

**Author:** ![CubeCoders](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cubecoders/32/298429_2.png) [@CubeCoders](https://meta.discourse.org/u/CubeCoders)\
**Post date:** [2023年三月27日 11:05 UTC](https://meta.discourse.org/t/how-to-drop-requests-with-an-empty-user-agent/259069/7 "2023-03-27T11:05:02Z")

</div>

确实，它需要放在最后。我已经编辑了我的帖子以包含它 👍

---

<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:** [2023年四月26日 11:05 UTC](https://meta.discourse.org/t/how-to-drop-requests-with-an-empty-user-agent/259069/8 "2023-04-26T11:05:24Z")

</div>

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