リバースプロキシとしてNGINXの代わりにCaddyを使用してください

Here are some notes about how I got my test Discourse instance running with Caddy Server.

Cool stuff about Caddy:

Cons:

  • Not as battle tested as apache, nginx and cia.

How To

Preparing Discourse

First, you need to apply this changes to your app.yml:

templates:
  - "templates/postgres.template.yml"
  - "templates/redis.template.yml"
  - "templates/web.template.yml"
  - "templates/web.ratelimited.template.yml"
  - "templates/web.socketed.template.yml" # <<<----- THIS IS NEW

## Let this two commented out
#  - "templates/web.ssl.template.yml"
#  - "templates/web.letsencrypt.ssl.template.yml"

## Let this two commented out 
expose:
#  - "8080:80"   # http
#  - "443:443" # https

env:
  ## This should be commented out too
  #LETSENCRYPT_ACCOUNT_EMAIL: mymail@gmail.com

Preparing Caddy

In the spirit of Discourse, let’s put Caddy in a Docker image too :whale2:

First prepare with:

mkdir /var/caddy
nano /var/caddy/Caddyfile

Add the following to the Caddyfile

forum.example.com # your domain here

proxy / unix:/sock/nginx.http.sock {
  transparent
}

Save and exit.

Let’s test

Now you need to rebuild Discourse:

/var/discourse
./launcher rebuild app

And then run Caddy:

docker run -d \
    -v /var/caddy/Caddyfile:/etc/Caddyfile \
    -v /var/caddy:/root/.caddy \
    -v /var/discourse/shared/standalone:/sock \
    -p 80:80 -p 443:443 \
    -p 80:80/udp -p 443:443/udp \
    --restart=always \
    --name caddy \
    --entrypoint "/usr/bin/caddy" \
    abiosoft/caddy -quic -email MYEMAILHERE@gmail.com -agree --conf /etc/Caddyfile --log stdout

After all, your forum should be avaliable at your domain, using SSL + HTTP2 + QUIC. You can’t more hipster than that.

「いいね!」 19

I run Caddy’s Discourse forums with this Caddyfile and no container:

forum.caddyserver.com

timeouts off
proxy / localhost:8080 {
	transparent
}

I just set up Discourse (with one easy tweak) and ran Caddy on the host machine.

^ This setup has been tested, and I can confirm it has been running with no glitches for months.

「いいね!」 10

I like how you’ve proxied to the socket and left the ports unexposed.

Neat little guide that one can use as a guideline to easily incorporate their Discourse installation to an existing Caddy proxy, too. Cheers!

「いいね!」 3

But using nginx, as I can see now.

Well, I have more than 1 Discourse install with Caddy in the front, but I didn’t bother to replace the server header and it still shows nginx. Can be the same. Or they are just using the simple Discourse install and have no need to run a reverse proxy at all in the front.

「いいね!」 1

My Discourse sites behind Caddy show nginx as the server too. I guess that transparent setting might make Caddy, uh, transparent.

「いいね!」 3

That might be a bug from a recent change, it didn’t used to do that. :thinking:

「いいね!」 2

Dear @Falco

Thank you so much for your posting.

I would like to install discourse using caddy condition, but I was confused with your docker command.

I never heard about the caddy, so I follow the digital ocean document

My question is, in the current server situation, Should I change the path
from etc/Caddyfile to /etc/caddy/Caddyfile?

docker run -d \
    -v /var/caddy/Caddyfile:/etc/Caddyfile \
    -v /var/caddy:/root/.caddy \
    -v /var/discourse/shared/standalone:/sock \
    -p 80:80 -p 443:443 \
    -p 80:80/udp -p 443:443/udp \
    --restart=always \
    --name caddy \
    --entrypoint "/usr/local/bin/caddy" \
    abiosoft/caddy -quic -email MYEMAILHERE@gmail.com -agree --conf /etc/Caddyfile --log stdout

Sincerely

これは私のサーバーでは機能しません。私は以下を使用しました。

unix:/var/discourse/shared/standalone/nginx.http.sock

これはcaddy v1用です。caddy v2の場合は、以下を使用してください。

unix//var/discourse/shared/standalone/nginx.http.sock

単に「:」を「/」に置き換えるだけです。

「いいね!」 1

OPのガイドに従い、DockerでCaddyを実行し、指定通りにボリュームをマウントしている場合は、そのパスで動作します。ガイドに従っていない場合は、パスが異なることになります。

「いいね!」 3

彼らのフォーラムのドメインとサブドメインは、こちらの下で名前が付けられています。

「いいね!」 1

古いスレッドを掘り起こしてすみません。Discourse で Caddy を動作させようとしています。Caddy の設定では「proxy」を使用していますが、私がそれを使用すると、構文エラーであり無効であると表示されます。「proxy」は「reverse_proxy」に変更されたのではないでしょうか?

以下は私の設定です。

forum.example.com {
    reverse_proxy / unix//var/discourse/shared/standalone/nginx.http.sock {
        transparent
    }
}

そうだと思います。試してみましたか?

「いいね!」 1

マット、ありがとう!\n2025年でもコンテナなしの設定が機能することを確認できました :smiley:\n\n\nforum.website.com {\n reverse_proxy localhost:8080\n}\n\n「proxy」だけでは機能しませんでした。

私も問題なく動作しています!

しかし、非常に簡単なセットアップに従ったところ、「Mixed Content」という警告が表示されました。

これらを解決するために、設定ファイル(app.ymlまたはweb_only.yml)のenvセクションに次のディレクティブを追加する必要がありました。

# FORCE SSL
DISCOURSE_FORCE_HTTPS: true

参考として、Caddy をリバースプロキシとして使用する非 Docker 化されたセットアップの現在の手順を以下に示します。

1) Discourse 設定ファイルの調整

  • 証明書をコメントアウトする
    templates:
    #  - "templates/web.ssl.template.yml"
    #  - "templates/web.letsencrypt.ssl.template.yml"
    
  • ポートマッピングを変更し、443 マッピングを無効にする
    expose:
    - "8080:80"   # http
    # - "443:443" # https
    
  • 静的ファイルの提供のために HTTPS を強制する
    env:
    DISCOURSE_FORCE_HTTPS: true
    

2) Discourse の再構築

./launcher rebuild app

3) Caddy のセットアップ

  • Caddy をインストールします。公式のデフォルトを使用します: Install — Caddy Documentation

  • /etc/caddy/Caddyfile を調整します

    forum.example.com {
          reverse_proxy localhost:8080
    }
    

    マルチサイトがある場合は、ドメインをリストするだけです。

    forum.example.com, forum2.example.com, forum3.example.com {
          reverse_proxy localhost:8080
    }
    

    デフォルトの設定ファイルの場所を確認するために systemctl status caddy を実行することもできます。

4) Caddy の実行

systemctl start caddy

変更後に設定をリロードします。

cd /etc/caddy
caddy reload
「いいね!」 2

こんにちは、チュートリアルありがとうございます。

マルチサイト構成でない場合、Caddyを使用する利点はありますか?パフォーマンスなど、何かありますか?

それについてはよく分かりません。証明書の問題に悩まされることなく、インスタンスの追加や変更が非常に簡単なため、現在はステージングサーバーでこのセットアップを使用しています。

「いいね!」 2

SSL設定を合理化するために、マルチサイト設定でこれに似たことを行いました…
…しかし、Caddy v2に更新し、マルチサイト設定でdocker-composeを使用しています。

web.ymlで:

  • templates/web.socketed.template.ymlのみを使用し、SSL ymlファイルは使用しません。
  • ポート\"443:443\"\"80:80\"などをコメントアウトします。
  • DISCOURSE_HOSTNAME_ALIASESDISCOURSE_FORCE_HTTPS: trueを追加します。

これにより、Caddy 2の最新バージョンが使用されます。そのため、このトピックで前述した一部のCaddy v1構成とは異なる場合があります。

これは、関連ファイルを最初に作成し、caddyを起動するbashファイルです。

#!/usr/bin/env bash

# 必要なディレクトリを作成します
mkdir -p /var/caddy
mkdir -p /var/caddy/data
mkdir -p /var/caddy/config



# 単純化されたCaddyfileを作成します
cat > /var/caddy/Caddyfile << 'EOF'
{
    email your-email-address-here@example.com
}

community1.example.com, community2.example.com, community3.example.com {
    reverse_proxy unix//sock/nginx.http.sock
}
EOF

# docker-compose.ymlを作成します
cat > /var/caddy/docker-compose.yml << 'EOF'
services:
  caddy:
    image: caddy:latest
    container_name: caddy-proxy
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
      - "443:443/udp"
    volumes:
      - /var/caddy/Caddyfile:/etc/caddy/Caddyfile
      - /var/caddy/data:/data
      - /var/caddy/config:/config
      - /var/discourse/shared/standalone:/sock
EOF

# caddyディレクトリに移動して起動します
cd /var/caddy

# Caddyを起動します
docker compose up -d
「いいね!」 2