# 添加路径到Cookie

**URL:** https://meta.discourse.org/t/add-path-to-cookie/40218
**Category:** Development
**Created:** [2016年二月26日 09:42 UTC](https://meta.discourse.org/t/add-path-to-cookie/40218 "2016-02-26T09:42:05Z")
**Posts on this page:** 11
**Page:** 3

<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: [2018年六月27日 09:09 UTC](https://meta.discourse.org/t/add-path-to-cookie/40218/42 "2018-06-27T09:09:18Z")

</div>

Yes would only be for subfolder setups, also not sure what to do about leftover cookie

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [2018年八月29日 21:26 UTC](https://meta.discourse.org/t/add-path-to-cookie/40218/43 "2018-08-29T21:26:31Z")

</div>

Were any of the recent subfolder changes helpful here on this front?

---

<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: [2018年八月29日 21:52 UTC](https://meta.discourse.org/t/add-path-to-cookie/40218/44 "2018-08-29T21:52:27Z")

</div>

I don’t recall changing anything in this department recently.

---

<div class="post-metadata">

### Author: ![Dylan-Brotherston](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dylan-brotherston/32/488781_2.png) [@Dylan-Brotherston](https://meta.discourse.org/u/Dylan-Brotherston)
#### Post date: [2025年五月25日 16:50 UTC](https://meta.discourse.org/t/add-path-to-cookie/40218/45 "2025-05-25T16:50:42Z")

</div>

7 年过去了，\_t cookie 仍未设置 Path  
 ![The image appears to show a screenshot of a blue screen with partially visible text including the date "Thu, 24 Jul 2025" and a timestamp. (Captioned by AI)](https://global.discourse-cdn.com/meta/original/4X/e/1/7/e17fb8739b485de8561cf6cb0529b27fa5df784a.png)  
运行版本 v3.4.2

我托管了两个服务器，每个服务器运行约 15 个 discourse 实例，使用子目录。未为 \_t cookie 设置 Path 是一个相当大的痛点。

`DISCOURSE_TOKEN_COOKIE` 也未在任何地方记录。

在 Google 上搜索只有 3 个结果：此帖子、一个指向定义该变量的 GitHub 的代码链接以及一个指向名为 `isharkfly` 的 404 链接。

我花了 6 个多月才找到这个帖子。

---

<div class="post-metadata">

### Author: ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)
#### Post date: [2025年五月28日 08:01 UTC](https://meta.discourse.org/t/add-path-to-cookie/40218/47 "2025-05-28T08:01:03Z")

</div>

很遗憾，你是对的 - `_t` cookie 仍然没有关联的路径。这是我们想做的事情，但“子文件夹”安装相当罕见，而同一域名上的_多个_子文件夹安装则更为罕见。所以它还没有被优先考虑。

我看了一下我们托管的一组子文件夹站点，我们的解决方法似乎是为域名上的每个站点重命名 cookie。因此，假设你使用的是我们的标准安装 YAML 文件，你需要在每个文件中添加如下内容：

```plaintext
env:
  DISCOURSE_TOKEN_COOKIE: _t_mysite

```

（其中 `mysite` 对于每个论坛都是不同的）

---

<div class="post-metadata">

### Author: ![Dylan-Brotherston](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dylan-brotherston/32/488781_2.png) [@Dylan-Brotherston](https://meta.discourse.org/u/Dylan-Brotherston)
#### Post date: [2025年五月28日 11:00 UTC](https://meta.discourse.org/t/add-path-to-cookie/40218/48 "2025-05-28T11:00:18Z")

</div>

是的。 我现在已经添加了这个。 但是用户仍然存在问题，即在切换实例时，他们需要重新验证的次数超过了应有的次数。

但最大的问题是发现此功能可用。 我只有通过阅读源代码才找到它，而我找到此帖子的唯一方法是在我已经知道它存在之后搜索确切的设置名称。

---

<div class="post-metadata">

### Author: ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)
#### Post date: [2025年五月28日 11:03 UTC](https://meta.discourse.org/t/add-path-to-cookie/40218/49 "2025-05-28T11:03:20Z")

</div>

> [@Dylan-Brotherston](#):
>
> 但最大的问题是发现这个可用。

明白了。感谢您在此发帖！

> [@Dylan-Brotherston](#):
>
> 但用户在切换实例时仍需要比预期更频繁地重新验证。

论坛是公开的吗？可以分享一些网址吗？

---

<div class="post-metadata">

### Author: ![Dylan-Brotherston](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dylan-brotherston/32/488781_2.png) [@Dylan-Brotherston](https://meta.discourse.org/u/Dylan-Brotherston)
#### Post date: [2025年五月28日 11:43 UTC](https://meta.discourse.org/t/add-path-to-cookie/40218/50 "2025-05-28T11:43:41Z")

</div>

它们是大学课程的论坛。因此，它们是私有的。

URL 格式为 `discourseX.university.url/\u003cyear\u003e/\u003ccourse-name\u003e`

---

<div class="post-metadata">

### Author: ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)
#### Post date: [2025年五月28日 12:30 UTC](https://meta.discourse.org/t/add-path-to-cookie/40218/51 "2025-05-28T12:30:47Z")

</div>

你的`DISCOURSE_TOKEN_COOKIE`是否包含了年份和课程名称？

---

<div class="post-metadata">

### Author: ![Dylan-Brotherston](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dylan-brotherston/32/488781_2.png) [@Dylan-Brotherston](https://meta.discourse.org/u/Dylan-Brotherston)
#### Post date: [2025年五月28日 14:06 UTC](https://meta.discourse.org/t/add-path-to-cookie/40218/52 "2025-05-28T14:06:11Z")

</div>

是的，它是 `_discourse-\u003cyear\u003e_\u003ccourse-name\u003e_t`

---

<div class="post-metadata">

### Author: ![Dylan-Brotherston](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dylan-brotherston/32/488781_2.png) [@Dylan-Brotherston](https://meta.discourse.org/u/Dylan-Brotherston)
#### Post date: [2025年九月5日 14:09 UTC](https://meta.discourse.org/t/add-path-to-cookie/40218/53 "2025-09-05T14:09:46Z")

</div>

我再次要求正确地将 `_t` cookie 的路径范围限定为实例的路径。

我遇到了 `DISCOURSE_TOKEN_COOKIE` 变通方法又一个问题。

我目前管理两个域，每个域运行 40 个，是的，四十个 Discourse 实例。（我明白这可能不是你们典型的用例）。

即使使用 `DISCOURSE_TOKEN_COOKIE` 重命名，cookie 也只限定于域，而不是路径。  
这意味着该域的所有请求都会发送该域的所有 cookie，而不管路径如何。

使用 40 个重命名的 cookie，每个请求都会发送一个有用的 cookie 和 39 个无用的 cookie。

嗯，它会发送 39 个无用的 cookie，除了在发送大约 30 个之后，请求太长了，我收到一个 400 响应，消息如下：

`请求头字段的大小超过服务器限制。`

[上一頁](https://meta.discourse.org/t/add-path-to-cookie/40218.md?page=2)
