Usa Caddy invece di NGNIX come il tuo 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 Mi Piace

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 Mi Piace

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 Mi Piace

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 Mi Piace

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

3 Mi Piace

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

2 Mi Piace

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

Questo non funziona sul mio server. Per me, ho usato:

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

E questo è per caddy v1. Per caddy v2, usa:

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

Basta sostituire “:” con “/”.

1 Mi Piace

Quel percorso funziona se stai seguendo la guida nell’OP e stai eseguendo Caddy in docker e montando i volumi come specificato. Se non stai seguendo la guida, ci saranno percorsi diversi, sì.

3 Mi Piace

Il dominio e il sottodominio del loro forum sono ora denominati sotto questo.

1 Mi Piace

Mi scuso per aver riaperto vecchie discussioni, sto cercando di far funzionare Caddy con Discourse. Nella tua configurazione di Caddy, usi “proxy”, ma quando lo uso, dice che c’è un errore di sintassi e che non è valido. “proxy” non è ora stato cambiato in “reverse_proxy”?

ecco la mia configurazione:

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

Penso di sì. Ci hai provato?

1 Mi Piace

Grazie Matt!
Posso confermare che la configurazione senza container funziona ancora nel 2025 :smiley:

forum.website.com {
        reverse_proxy localhost:8080
}

Usare solo “proxy” non ha funzionato.

Funziona benissimo anche per me!

Tuttavia, ho ricevuto avvisi di “Contenuto misto” seguendo quella configurazione molto semplice:


Per risolverli, ho dovuto aggiungere una direttiva aggiuntiva alla sezione env del file di configurazione (app.yml o web_only.yml):

# FORCE SSL
DISCOURSE_FORCE_HTTPS: true

Per riferimento, questi sono i passaggi attuali per una configurazione non dockerizzata che utilizza Caddy come proxy inverso:

1) Regola il file di configurazione di Discourse

  • Commenta i certificati
    templates:
    #  - "templates/web.ssl.template.yml"
    #  - "templates/web.letsencrypt.ssl.template.yml"
    
  • Cambia il mapping delle porte e disabilita il mapping 443
    expose:
    - "8080:80"   # http
    # - "443:443" # https
    
  • Forza HTTPS per servire file statici
    env:
    DISCOURSE_FORCE_HTTPS: true
    

2) Ricostruisci Discourse

./launcher rebuild app

3) Configura Caddy

  • Installa Caddy, utilizzando solo le impostazioni predefinite ufficiali: Install — Caddy Documentation

  • Regola /etc/caddy/Caddyfile

    forum.example.com {
          reverse_proxy localhost:8080
    }
    

    Se hai multisito, puoi semplicemente elencare i tuoi domini:

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

    Puoi anche eseguire systemctl status caddy per verificare la posizione predefinita del file di configurazione.

4) Esegui Caddy

systemctl start caddy

Ricarica la configurazione dopo le modifiche:

cd /etc/caddy
caddy reload
2 Mi Piace

Ciao, grazie per il tutorial.

Ci sono vantaggi nell’usare Caddy per una configurazione non multi-sito? Performance o altro?

Non so a riguardo… Io uso questa configurazione sui miei server di staging ora perché è davvero facile aggiungere o modificare istanze senza incorrere in problemi di certificati.

2 Mi Piace

Ho fatto qualcosa di simile in una configurazione multisito per semplificare l’impostazione SSL…
… ma ho aggiornato a Caddy v2 e sto usando docker-compose con una configurazione multisito.

In web.yml:

  • utilizzando solo templates/web.socketed.template.yml e nessun file yml SSL.
  • commentando le porte \"443:443\", \"80:80\" ecc.
  • aggiungendo DISCOURSE_HOSTNAME_ALIASES e DISCOURSE_FORCE_HTTPS: true

Questo utilizza l’ultima versione di Caddy 2, motivo per cui potrebbe apparire diverso da alcune delle configurazioni di Caddy v1 menzionate in precedenza in questo argomento.

Questo è il file bash che crea inizialmente i file pertinenti e avvia caddy:

#!/usr/bin/env bash

# Crea le directory necessarie
mkdir -p /var/caddy
mkdir -p /var/caddy/data
mkdir -p /var/caddy/config



# Crea il Caddyfile semplificato
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

# Crea 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

# Naviga nella directory di caddy e avvia
cd /var/caddy

# Avvia Caddy
docker compose up -d
2 Mi Piace