# 无法将本地 Discourse 嵌入到本地 Ghost 实例

**URL:** https://meta.discourse.org/t/unable-to-embed-local-discourse-to-local-ghost-instance/174459
**Category:** Support
**Created:** [2020年十二月28日 22:20 UTC](https://meta.discourse.org/t/unable-to-embed-local-discourse-to-local-ghost-instance/174459 "2020-12-28T22:20:52Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![nehalist](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nehalist/32/204489_2.png) [@nehalist](https://meta.discourse.org/u/nehalist)
#### Post date: [2020年十二月28日 22:20 UTC](https://meta.discourse.org/t/unable-to-embed-local-discourse-to-local-ghost-instance/174459/1 "2020-12-28T22:20:52Z")

</div>

你好，

我想在本地 Ghost 安装上试用 Discourse。

基本上，我已经用最简单的方式设置好了所有内容；我遵循了“使用 Docker 安装 Discourse 进行开发的初学者指南”，并通过其 CLI 设置了 Ghost。

Ghost 运行在 localhost:2368，Discourse 运行在 localhost:9292。

我已将 `localhost:2368` 添加到“自定义/嵌入”允许的托管列表中（未设置任何类名、路径白名单或发布到分类等设置）。

在添加代码片段后（当然也正确替换了占位符），我确实看到了一个显示“正在加载讨论…"的 Discourse 嵌入内容，但几秒钟后控制台报错：

> 在 ‘DOMWindow’ 上执行 ‘postMessage’ 失败：提供的目标源（‘[http://localhost:9292](http://localhost:9292)’）与接收窗口的源（‘[http://localhost:2368](http://localhost:2368)’）不匹配。

我漏掉了什么？

谢谢！

---

<div class="post-metadata">

### Author: ![simbleau](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simbleau/32/204367_2.png) [@simbleau](https://meta.discourse.org/u/simbleau)
#### Post date: [2020年十二月28日 22:46 UTC](https://meta.discourse.org/t/unable-to-embed-local-discourse-to-local-ghost-instance/174459/2 "2020-12-28T22:46:23Z")

</div>

（非专家或团队成员）

如果出现该错误，说明 iframe 尚未加载完成。

在将 `localhost:2368` 添加到允许的主机列表时，请尝试带或不带末尾斜杠的形式，同时也试试使用通配符。

出现这种情况的原因是，页面可能已经加载，但其中没有任何内容会链接到外部资源，从而导致加载中断。

不过，我其实对很快尝试使用 Ghost 很感兴趣。因此我会关注这个主题，并希望这仅仅是你在使用过程中遇到的唯一问题。🙂

---

<div class="post-metadata">

### Author: ![nehalist](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nehalist/32/204489_2.png) [@nehalist](https://meta.discourse.org/u/nehalist)
#### Post date: [2021年一月1日 20:03 UTC](https://meta.discourse.org/t/unable-to-embed-local-discourse-to-local-ghost-instance/174459/3 "2021-01-01T20:03:16Z")

</div>

很遗憾，我目前还没有找到解决方案：frowning:

---

<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: [2021年一月2日 09:58 UTC](https://meta.discourse.org/t/unable-to-embed-local-discourse-to-local-ghost-instance/174459/4 "2021-01-02T09:58:26Z")

</div>

听起来您试图在非标准端口上运行生产实例？Discourse 不支持此操作。

---

<div class="post-metadata">

### Author: ![nehalist](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nehalist/32/204489_2.png) [@nehalist](https://meta.discourse.org/u/nehalist)
#### Post date: [2021年一月2日 10:22 UTC](https://meta.discourse.org/t/unable-to-embed-local-discourse-to-local-ghost-instance/174459/5 "2021-01-02T10:22:42Z")

</div>

生产环境实例？我按照指南在本地安装了 Discourse 用于开发——这难道不是本地使用的正确安装方式吗？安装过程非常顺利，只是嵌入功能出现了问题。

---

<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: [2021年一月2日 10:24 UTC](https://meta.discourse.org/t/unable-to-embed-local-discourse-to-local-ghost-instance/174459/6 "2021-01-02T10:24:20Z")

</div>

我认为你需要找到一种方法，使用实际的主机名而非端口来测试这一点。

---

<div class="post-metadata">

### Author: ![nehalist](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nehalist/32/204489_2.png) [@nehalist](https://meta.discourse.org/u/nehalist)
#### Post date: [2021年一月3日 22:31 UTC](https://meta.discourse.org/t/unable-to-embed-local-discourse-to-local-ghost-instance/174459/7 "2021-01-03T22:31:03Z")

</div>

那我试试用 nginx 搭建一个代理，谢谢。

---

<div class="post-metadata">

### Author: ![nehalist](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nehalist/32/204489_2.png) [@nehalist](https://meta.discourse.org/u/nehalist)
#### Post date: [2021年一月12日 13:16 UTC](https://meta.discourse.org/t/unable-to-embed-local-discourse-to-local-ghost-instance/174459/8 "2021-01-12T13:16:07Z")

</div>

我尝试添加了两个 nginx 反向代理，一个用于 Ghost，一个用于 Discourse，分别运行在 `ghost.localhost` 和 `discourse.localhost`。在将所有 URL 更改为代理地址后，出现了以下错误：

> 在 ‘Window’ 上执行 ‘postMessage’ 失败：在调用 ‘postMessage’ 时，目标源 ‘’ 无效。

当使用 `http://localhost:9292/` 而不是 `http://discourse.localhost/` 作为 `discourseUrl` 时，错误不再出现，但评论功能陷入了无限循环（“正在加载讨论”永远显示，并且每隔几秒就会发出一些请求）。

nginx 配置相当简单：

```plaintext
server {
  listen 80;
  server_name discourse.localhost;

  access_log /var/log/nginx/discourse.localhost-access.log;
  error_log /var/log/nginx/discourse.localhost-error.log;
  
  location / {
    proxy_pass http://127.0.0.1:9292;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header Host $http_host;
  }
}

```

Ghost 的配置与此类似，只需将 `discourse` 替换为 `ghost` 即可。
