Verwenden Sie Caddy anstelle von NGNIX als Ihren Reverse-Proxy

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 „Gefällt mir“

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 „Gefällt mir“

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 „Gefällt mir“

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 „Gefällt mir“

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

3 „Gefällt mir“

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

2 „Gefällt mir“

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

Das funktioniert nicht auf meinem Server. Ich habe verwendet:

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

Und das ist für caddy v1. Für caddy v2 verwenden Sie bitte:

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

Ersetzen Sie einfach “:” durch “/”.

1 „Gefällt mir“

Dieser Pfad funktioniert, wenn Sie der Anleitung im OP folgen und Caddy in Docker ausführen und die Volumes wie angegeben einbinden. Wenn Sie der Anleitung nicht folgen, gibt es natürlich andere Pfade.

3 „Gefällt mir“

Ihre Foren-Domain und Subdomain sind jetzt unter diesem benannt.

1 „Gefällt mir“

Entschuldigung, dass ich alte Threads wieder aufgreife. Ich versuche, Caddy mit Discourse zum Laufen zu bringen. In Ihrer Caddy-Konfiguration verwenden Sie „proxy“, aber wenn ich es verwende, wird eine Syntaxfehlermeldung angezeigt, dass es ungültig ist. Wurde „proxy“ nicht inzwischen in „reverse_proxy“ geändert?

Hier ist meine Konfiguration:

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

Ich glaube schon. Hast du es versucht?

1 „Gefällt mir“

Danke Matt!
Ich kann bestätigen, dass das Setup ohne Container auch 2025 noch funktioniert :smiley:

forum.website.com {
        reverse_proxy localhost:8080
}

Die Verwendung von nur “proxy” funktionierte nicht.

Es funktioniert auch für mich bestens!

Allerdings erhielt ich “Mixed Content”-Warnungen, die diesem sehr einfachen Setup folgten:


Um diese zu beheben, musste ich eine zusätzliche Direktive zum env-Abschnitt der Konfigurationsdatei (app.yml oder web_only.yml) hinzufügen:

# FORCE SSL
DISCOURSE_FORCE_HTTPS: true

Als Referenz sind dies die aktuellen Schritte für ein nicht-dockerisiertes Setup mit Caddy als Reverse-Proxy:

1) Anpassen der Discourse-Konfigurationsdatei

  • Zertifikate auskommentieren
    templates:
    #  - "templates/web.ssl.template.yml"
    #  - "templates/web.letsencrypt.ssl.template.yml"
    
  • Port-Mapping ändern und 443-Mapping deaktivieren
    expose:
    - "8080:80"   # http
    # - "443:443" # https
    
  • HTTPS für die Bereitstellung statischer Dateien erzwingen
    env:
    DISCOURSE_FORCE_HTTPS: true
    

2) Discourse neu erstellen

./launcher rebuild app

3) Caddy einrichten

  • Caddy installieren, nur mit den offiziellen Standardeinstellungen: Install — Caddy Documentation

  • /etc/caddy/Caddyfile anpassen

    forum.example.com {
          reverse_proxy localhost:8080
    }
    

    Wenn Sie Multisite haben, können Sie einfach Ihre Domains auflisten:

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

    Sie können auch systemctl status caddy ausführen, um den Standardspeicherort der Konfigurationsdatei zu überprüfen.

4) Caddy ausführen

systemctl start caddy

Konfiguration nach Änderungen neu laden:

cd /etc/caddy
caddy reload
2 „Gefällt mir“

Hey, danke f

Gibt es einen Vorteil bei der Verwendung von Caddy f

Ich bin mir da nicht so sicher… Ich verwende dieses Setup jetzt auf meinen Staging-Servern, weil es wirklich einfach ist, Instanzen hinzuzufügen oder zu ändern, ohne auf Zertifikatsprobleme zu stoßen.

2 „Gefällt mir“

Ich habe etwas Ähnliches bei einem Multisite-Setup gemacht, um die SSL-Einrichtung zu optimieren…
… aber auf Caddy v2 aktualisiert und verwende docker-compose mit einem Multisite-Setup.

In der web.yml:

  • Nur templates/web.socketed.template.yml und keine SSL yml-Dateien verwenden.
  • Die Ports \"443:443\", \"80:80\" usw. auskommentieren.
  • DISCOURSE_HOSTNAME_ALIASES und DISCOURSE_FORCE_HTTPS: true hinzufügen.

Dies verwendet die neueste Version von Caddy 2, weshalb es anders aussehen mag als einige der oben in diesem Thema erwähnten Caddy v1-Konfigurationen.

Dies ist die Bash-Datei, die die relevanten Dateien initial erstellt und Caddy startet:

#!/usr/bin/env bash

# Notwendige Verzeichnisse erstellen
mkdir -p /var/caddy
mkdir -p /var/caddy/data
mkdir -p /var/caddy/config



# Die vereinfachte Caddyfile erstellen
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 erstellen
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

# In das Caddy-Verzeichnis wechseln und starten
cd /var/caddy

# Caddy starten
docker compose up -d
2 „Gefällt mir“