Subfolder Installation with Cloudflare

You can use service worker as a reverse proxy to redirect your subfolder to your Discourse instance.

Create a new service, http-handler

Create a worker with the following code and configure it at https://=discourse_hostname=/=discourse_subfolder=* (note the trailing asterisk).

addEventListener("fetch", (event) => {
      (err) => new Response(err.stack, { status: 500 })

async function handleRequest(request) {
  const url = new URL(request.url);
  request = new Request(request)
  request.headers.set("CF-Host", url.hostname)
  url.hostname = '=discourse_hostname=';
  return fetch(url.toString(), request)

Add this stanza to your app.yml. You’ll need to do this by hand, and not with the dashboard, but once you do you can still use the dashboard for rebuilds, changing plugins and so on.

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

Also add this template to your app.yml:

   - "templates/cloudflare.template.yml"

In the dashboard, edit your server and add this custom environment variable:


Contact @staff to have this staff-only setting added to your server:

  discourse_subfolder: =discourse_subfolder=

After you save that, you can run a build from the Dashboard and you should be all set!