サブドメインではなくサブフォルダー(パスプレフィックス)から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;
          }
    - file:
        path: /etc/nginx/conf.d/outlets/server/set-real-ip-from-webroot.conf
        chmod: 644
        contents: |
          set_real_ip_from 123.456.789.ABC; # アップストリームプロキシの IP アドレス
    - file:
        path: /etc/nginx/conf.d/outlets/server/real-ip-header.conf
        chmod: 644
        contents: |
          real_ip_header x-forwarded-for;

エンドユーザーの IP アドレスを保持する詳細については、Handling the "chain of trust" of the end user's real IP を参照してください。

これらの変更を行った後、通常通り 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

次に、バックアップを取った後に remap コマンドを実行します。

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 がサブフォルダーで稼働するようになったため、Web クローラーがどのルートをクロールするかを制御するための 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
Install Discourse into domain.com/forums
Non-standard port breaks uploads
Avatar uploads broken on subfolder installs?
Moving Discourse to subdirectory caused all uploads to break
Image lightbox not working in subfolder install
Problem with discourse in a subfolder
Install Discourse as subfolder within Wordpress website?
Uploaded avatar or gravatar not working
Installation on v-server as a subfolder with other services in subfolders using apache
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?
Domain missing from some requests after migration from subfolder back to subdomain
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
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
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?
Discourse sign in redirects to root directory instead of subfolder
Setup Discourse with KeyCDN
National Flags
Setup wizard emoji wrong in subfolder install
How to run Discourse in a subfolder?
Handling the "chain of trust" of the end user's real IP
Recent Changes Breaking Subfolder Setup?
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
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