# app.yml 中 allowlist 的正确 REGEX 是什么？

**URL:** <https://meta.discourse.org/t/whats-the-correct-regex-for-the-allowlist-in-app-yml/265020>\
**Category:** Self-hosting\
**Tags:** prometheus\
**Created:** [2021年二月22日 07:01 UTC](https://meta.discourse.org/t/whats-the-correct-regex-for-the-allowlist-in-app-yml/265020 "2021-02-22T07:01:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![andy212](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@andy212](https://meta.discourse.org/u/andy212)\
**Post date:** [2021年二月22日 07:01 UTC](https://meta.discourse.org/t/whats-the-correct-regex-for-the-allowlist-in-app-yml/265020/1 "2021-02-22T07:01:02Z")

</div>

> [@sam](#):
>
> `DISCOURSE_PROMETHEUS_TRUSTED_IP_ALLOWLIST_REGEX`

app.yml 文件中 \_REGEX 的正确格式是什么？

这样对吗？

```plaintext
  DISCOURSE_PROMETHEUS_TRUSTED_IP_ALLOWLIST_REGEX: "(35.247.25.222|34.83.69.98|35.192.150.184)"

```

我将尝试列出 [grafana.com](http://grafana.com) 用于抓取的所有 IP 地址（[https://grafana.com/api/hosted-grafana/source-ips.txt）。](https://grafana.com/api/hosted-grafana/source-ips.txt%EF%BC%89%E3%80%82)

---

<div class="post-metadata">

**Author:** ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)\
**Post date:** [2021年二月22日 07:04 UTC](https://meta.discourse.org/t/whats-the-correct-regex-for-the-allowlist-in-app-yml/265020/2 "2021-02-22T07:04:57Z")

</div>

看起来大体正确。不过您可能需要转义 `.` 并添加 `^` 和 `$`。

---

<div class="post-metadata">

**Author:** ![andy212](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@andy212](https://meta.discourse.org/u/andy212)\
**Post date:** [2021年二月22日 07:41 UTC](https://meta.discourse.org/t/whats-the-correct-regex-for-the-allowlist-in-app-yml/265020/3 "2021-02-22T07:41:52Z")

</div>

我可能应该补充说明一下，它实际上无法工作……

我尝试添加了我机器的 IP 地址，但如果不登录 Discourse，就无法访问指标。

您关于转义点号（dot）的观点我理解了。在这种情况下，正则表达式只会接收 IP 地址，因此转义点号不会改变结果，但确实会让代码更难阅读。此外，[grafana.com](http://grafana.com) 使用多达 100 个 IP 来拉取数据，所以这一行可能会变得太长……

---

<div class="post-metadata">

**Author:** ![dschaper](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dschaper/32/209535_2.png) [@dschaper](https://meta.discourse.org/u/dschaper)\
**Post date:** [2021年三月2日 05:44 UTC](https://meta.discourse.org/t/whats-the-correct-regex-for-the-allowlist-in-app-yml/265020/4 "2021-03-02T05:44:20Z")

</div>

> [@sam](#):
>
> 您可能需要转义 `.` 并添加 `^` 和 `$`

看起来 `$` 在从 `containers/<container>.conf` 翻译到容器环境时被转义了。

容器配置：

```conf
  ## Prometheus 指标允许的 IP
  DISCOURSE_PROMETHEUS_TRUSTED_IP_ALLOWLIST_REGEX: "^10.20.20.5$"

```

容器环境。

```plaintext
declare -x DISCOURSE_PROMETHEUS_TRUSTED_IP_ALLOWLIST_REGEX="^10.20.20.5\$"

```
