Utilisez Caddy au lieu de NGNIX comme proxy inverse

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 « J'aime »

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 « J'aime »

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 « J'aime »

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 « J'aime »

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

3 « J'aime »

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

2 « J'aime »

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

Cela ne fonctionne pas sur mon serveur. Pour moi, j’ai utilisé :

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

Et ceci est pour caddy v1. Pour caddy v2, veuillez utiliser :

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

Remplacez simplement “:” par “/”.

1 « J'aime »

Ce chemin fonctionne si vous suivez le guide dans le OP et exécutez Caddy dans docker et montez les volumes comme spécifié. Si vous ne suivez pas le guide, il y aura différents chemins, oui.

3 « J'aime »

Leur nom de domaine et sous-domaine de forum sont maintenant nommés sous ceci.

1 « J'aime »

Désolé d’exhumer de vieux fils, j’essaie de faire fonctionner Caddy avec Discourse. Dans votre configuration Caddy, vous utilisez « proxy », mais quand je l’utilise, il indique qu’il y a une erreur de syntaxe et que ce n’est pas valide. N’est-ce pas que « proxy » a maintenant été remplacé par « reverse_proxy » ?

voici ma configuration :

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

Je pense que oui. L’avez-vous essayé ?

1 « J'aime »

Merci Matt !
Je peux confirmer que la configuration sans conteneur fonctionne toujours en 2025 :smiley :

forum.website.com {
        reverse_proxy localhost:8080
}

Utiliser simplement « proxy » n’a pas fonctionné.

Ça fonctionne très bien pour moi aussi !

Cependant, j’ai reçu des avertissements de « Contenu mixte » en suivant cette configuration très simple :


Pour résoudre ces problèmes, j’ai dû ajouter une directive supplémentaire à la section env du fichier de configuration (app.yml ou web_only.yml) :

# FORCER SSL 
DISCOURSE_FORCE_HTTPS: true

Pour référence, voici les étapes actuelles pour une configuration non dockerisée utilisant Caddy comme proxy inverse :

1) Ajuster le fichier de configuration de Discourse

  • Commenter les certificats
    templates:
    #  - "templates/web.ssl.template.yml"
    #  - "templates/web.letsencrypt.ssl.template.yml"
    
  • Changer le mappage de port et désactiver le mappage 443
    expose:
    - "8080:80"   # http
    # - "443:443" # https
    
  • Forcer HTTPS pour servir les fichiers statiques
    env: 
    DISCOURSE_FORCE_HTTPS: true
    

2) Reconstruire Discourse

./launcher rebuild app

3) Configurer Caddy

  • Installer Caddy, en utilisant simplement les paramètres par défaut officiels : Install — Caddy Documentation

  • Ajuster /etc/caddy/Caddyfile

    forum.example.com {
          reverse_proxy localhost:8080
    }
    

    Si vous avez un multisite, vous pouvez simplement lister vos domaines :

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

    Vous pouvez également exécuter systemctl status caddy pour vérifier l’emplacement du fichier de configuration par défaut.

4) Exécuter Caddy

systemctl start caddy

Recharger la configuration après les modifications :

cd /etc/caddy
caddy reload
2 « J'aime »

Salut, merci pour le tutoriel.

Y a-t-il un avantage à utiliser Caddy pour une configuration non-multisite ? Performance ou autre chose ?

Je ne sais pas à ce sujet… J’utilise simplement cette configuration sur mes serveurs de staging maintenant parce qu’il est vraiment facile d’ajouter ou de modifier des instances sans rencontrer de problèmes de certificat.

2 « J'aime »

J’ai fait quelque chose de similaire sur une configuration multisite pour rationaliser la configuration SSL…
… mais mis à jour vers Caddy v2, et en utilisant docker-compose avec une configuration multisite.

Dans le web.yml :

  • en utilisant uniquement templates/web.socketed.template.yml et aucun fichier yml SSL.
  • en commentant les ports \"443:443\", \"80:80\" etc.
  • en ajoutant DISCOURSE_HOSTNAME_ALIASES et DISCOURSE_FORCE_HTTPS: true

Cela utilise la dernière version de Caddy 2, c’est pourquoi cela peut sembler différent de certaines configurations Caddy v1 mentionnées ci-dessus dans ce sujet.

Voici le fichier bash qui crée initialement les fichiers pertinents et démarre caddy :

#!/usr/bin/env bash

# Créer les répertoires nécessaires
mkdir -p /var/caddy
mkdir -p /var/caddy/data
mkdir -p /var/caddy/config



# Créer le Caddyfile simplifié
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

# Créer 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

# Naviguer vers le répertoire caddy et démarrer
cd /var/caddy

# Démarrer Caddy
docker compose up -d
2 « J'aime »