从子文件夹(路径前缀)而非子域名提供 Discourse 服务

:warning: Discourse 官方关于子文件夹设置的声明

我们为企业级及以上的托管客户提供子文件夹设置支持。由于技术设置复杂性较高,除非您对自定义子文件夹设置非常有经验,否则我们强烈建议您不要使用此设置。

关键是您必须对以下内容有深入的了解:

  • Discourse Docker 容器中的 NGINX 设置
  • 使用代理链中的自定义标头安全地转发原始 IP
  • 前置代理服务器中的速率限制

如果所有这些听起来对您来说很奇怪,我们强烈建议您避免这种设置。

要在您的域名上从子文件夹(也称为路径前缀)提供 Discourse 服务,例如 https://www.example.com/=SUBFOLDER=,请按以下步骤操作!

Docker 配置

在您的 Docker 容器 yml 文件的 env 部分,添加 DISCOURSE_RELATIVE_URL_ROOT 设置以及您想使用的子文件夹。请确保它/ 结尾。

编辑此项将更新整个指南。

env:
  ...
  DISCOURSE_RELATIVE_URL_ROOT: /=SUBFOLDER=

run 部分需要进行一些更改,以便将所有 Discourse 路由发送到正确的位置。以下是一个包含子文件夹支持的完整 run 部分:

run:
    - exec:
        cd: $home
        cmd:
          - mkdir -p public/=SUBFOLDER=
          - cd public/=SUBFOLDER= & ln -s ../uploads & ln -s ../backups
    - replace:
       global: true
       filename: /etc/nginx/conf.d/discourse.conf
       from: proxy_pass http://discourse;
       to: |
          rewrite ^/(.*)$ /=SUBFOLDER=/$1 break;
          proxy_pass http://discourse;
    - replace:
       filename: /etc/nginx/conf.d/discourse.conf
       from: etag off;
       to: |
          etag off;
          location /=SUBFOLDER= {
             rewrite ^/=SUBFOLDER=/?(.*)$ /$1;
          }
    - replace:
         filename: /etc/nginx/conf.d/discourse.conf
         from: $proxy_add_x_forwarded_for
         to: $http_your_original_ip_header
         global: true

$http_your_original_ip_header 代表 Your-Original-Ip-Header,这是一个您在源站设置的受信任的 Header,其中包含实际的客户端 IP。

这是必需的,因为流量会经过一个中央代理,如果 Discourse 恰好有一个公共 IP,则可以伪造它。如果 Discourse 是私有的,您可能可以使用 X-Forwarded-For

完成这些更改后,像往常一样引导您的 Docker 容器,或者如果您正在更改现有容器,则重建它。

./launcher bootstrap app

或者

./launcher rebuild app

附件是一个独立的容器的完整示例 yml 文件。

subfolder-sample.yml (3.1 KB)

速率限制问题

如果您采用此设置,您可能需要在请求到达容器内的 NGINX 之前对其进行速率限制,这意味着您可能将避免使用我们的速率限制模板。在容器中配置 NGINX 以根据重新映射的 IP 进行限制非常困难,并且需要对模板进行复杂的更改。

现有帖子

如果您在现有站点(之前使用子域名)上执行此操作,您会发现上传的文件已损坏。有一个工具可以帮助修复所有路径以包含子文件夹。首先,进入 Docker 容器并导航到 Discourse 目录:

cd /var/discourse
./launcher enter app
cd /var/www/discourse

然后,在备份之后运行重映射命令:

RAILS_ENV=production bundle exec script/discourse remap '/uploads' '/=SUBFOLDER=/uploads'

另请参阅:Use a subfolder (path prefix) to serve Discourse with multiple servers sharing a domain 以了解更多晦涩的设置。

robots.txt

现在 Discourse 运行在子文件夹上,它无法提供其 robots.txt 文件来控制哪些路由被网络爬虫抓取。爬虫将查看您主站点的 robots.txt 文件(位于 https://www.example.com/robots.txt)。您需要复制 Discourse 的 robots.txt 文件内容(位于 https://www.example.com/=SUBFOLDER=/robots.txt)并将其放入您主站点的 robots.txt 文件中。

35 个赞
How can I change my installation from forum.website.com to www.website.com/forum?
Moving Discourse to subdirectory caused all uploads to break
Uploaded avatars and Gravatar not working with subfolder installation
Is it possible to install discourse inside a directory?
Launch Discourse on www.example.com/meta/ In a web folder type
Subfolder with SSL and nginx reverse proxy
404 errors (assets) on Subfolder installation
Uploaded avatar or gravatar not working
Install Discourse into domain.com/forums
Problem with discourse in a subfolder
Moving Discourse to subdirectory caused all uploads to break
Non-standard port breaks uploads
Avatar uploads broken on subfolder installs?
Image lightbox not working in subfolder install
Install Discourse as subfolder within Wordpress website?
Is it possible to install Discourse on the same domain of your main website
Subfolder install - external links get changed/redirected to site relative links
Discourse embedded on my WP
Reverse Proxy Assets
Reverse Proxy rewrites domain in external links in posts
Inheriting discourse install - need some assistance
Multisite installation ... in subdirectories?
Generals Subfolder recommendations and tips
Add to route headers?
Sorry, there was an error authorizing your account
Site Logo and Icons Break due to subfolder config
Site Logo and Icons Break due to subfolder config
Site Logo and Icons Break due to subfolder config
Running Discourse at / while serving a custom app at /tickets on the same domain
404 errors (assets) on Subfolder installation
404 errors (assets) on Subfolder installation
Moving Discourse from Subdomain to Subfolder
Blank Page After installing Discourse on Subfolder
Trailing slash and subfolder setup
Install Discourse as a subdirectory with existing Nginx
How to run discourse on subdirectory of external domain?
How to run discourse on subdirectory of external domain?
How to run discourse on subdirectory of external domain?
Digest email issue with 'stable'
Using other ports than 80 and 443 - HowTo?
Add secondary url / embed into Wordpress
Recent Changes Breaking Subfolder Setup?
Issue with FAQ link on edge case subfolder setup
Issue with FAQ link on edge case subfolder setup
Put discourse on a sub url
No subfolder prefixing for some resources
Upgrade failed - could use some help to try and fix using SSH
How to run Discourse in a subfolder?
Install Discourse without docker with an existing Rails app
Docker subfolder install not working due to Ember-CLI proxy error?
Multisite DISCOURSE_RELATIVE_URL_ROOT
How to install discourse into subdirectory instead of subdomain?
Setup wizard emoji wrong in subfolder install
Not find a Gravatar
Installing for production via Docker on existing server with Litespeed?
Use a subfolder (path prefix) to serve Discourse with multiple servers sharing a domain
Configure a Cloudfront reverse proxy for a subfolder install
Run other websites on the same machine as Discourse
Embedding Categories in Existing SPA
Does custom domain support the subpath format like 'example.com/forum'?
To use Discourse completely on shopify
Are separate subdomains required for making a discourse forum?
Problem when updating Discourse Forum
Domain missing from some requests after migration from subfolder back to subdomain
Error when I send a message in chat [downgraded to Stable from 3.1.0beta4]
Error when I send a message in chat [downgraded to Stable from 3.1.0beta4]
How to Change Discourse forum path
How to setup NGINX in the Discourse Docker container
How to setup NGINX in the Discourse Docker container
Possible to update add url path prefix for /t and /c
Implement the discourse forum in the laravel 11 project
Endless loading behind Cloudflare
Publish Discourse under URL
Cloudflare with subfolder setup
How to Set Up Discourse in a Subdirectory for Self-Hosting?
Setup wizard emoji wrong in subfolder install
National Flags
Discourse sign in redirects to root directory instead of subfolder
Last update breaks permalink for categories
Setup wizard emoji wrong in subfolder install
Ghost & Discourse SSO implementation
Wiki topics look incredibly out of date?
Too many redirects when reverse proxying to Discourse from a different server
Blank main page on one forum (subfolder + multisite install)
Moving Discourse to subdirectory caused all uploads to break
Feasible to reverse proxy using qualifier in URL?
Upgrade doesn't work when installed with subfolder
Setup Discourse with KeyCDN
Message bus does not respect subfolder during update process
Wiki Posts Not Green?
Base_url and base_uri
Embed whole Discourse board into Wordpresspage
Backup upload failing
Redirect to discourse from a route in another domain
Redirect to discourse from a route in another domain
Problem with discourse in a subfolder
Install Discourse as subfolder within Wordpress website?
Looking for a developper
Switching forum from subdomain to sub directory
How to install discourse with nginx as proxy on specific path?
All IPs recorded as 127.0.0.1
How to install discourse with nginx as proxy on specific path?
SetupWizard, blank screen, JS Error "`virtual-dom"
Trouble connecting drupal and discourse
Failed to Bootstrap - launcher rebuild app
Redirect from one server to another server and keep everything after the URL
Installation on v-server as a subfolder with other services in subfolders using apache