Usa Caddy en lugar de NGNIX como tu proxy inverso

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 Me gusta

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 Me gusta

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 Me gusta

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 me gusta

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

3 Me gusta

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

2 Me gusta

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

Esto no funciona en mi servidor. Para mí, usé:

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

Y esto es para caddy v1. Para caddy v2, usa:

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

Solo reemplaza “:” por “/”.

1 me gusta

Esa ruta funciona si sigues la guía en el OP y ejecutas Caddy en docker y montas los volúmenes como se especifica. Si no sigues la guía, habrá rutas diferentes, sí.

3 Me gusta

El dominio y subdominio de su foro ahora están bajo este.

1 me gusta

Disculpa por revivir hilos antiguos, estoy intentando hacer que Caddy funcione con Discourse. En tu configuración de Caddy, usas “proxy”, pero cuando lo uso, dice que hay un error de sintaxis y que no es válido. ¿No se ha cambiado “proxy” ahora a “reverse_proxy”?

aquí está mi configuración:

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

Creo que sí. ¿Lo has probado?

1 me gusta

¡Gracias Matt!
Confirmo que la configuración sin contenedor sigue funcionando en 2025 :smiley:

forum.website.com {
        reverse_proxy localhost:8080
}

Usar solo “proxy” no funcionó.

¡A mí también me funciona de maravilla!

Sin embargo, me aparecieron advertencias de “Contenido Mixto” siguiendo esa configuración tan sencilla:


Para resolver esto, tuve que añadir una directiva adicional a la sección env del archivo de configuración (app.yml o web_only.yml):

# FORZAR SSL 
DISCOURSE_FORCE_HTTPS: true

Para referencia, estos son los pasos actuales para una configuración no dockerizada que utiliza Caddy como proxy inverso:

1) Ajustar el archivo de configuración de Discourse

  • Comentar los certificados
    templates:
    #  - "templates/web.ssl.template.yml"
    #  - "templates/web.letsencrypt.ssl.template.yml"
    
  • Cambiar el mapeo de puertos y deshabilitar el mapeo 443
    expose:
    - "8080:80"   # http
    # - "443:443" # https
    
  • Forzar HTTPS para servir archivos estáticos
    env:
    DISCOURSE_FORCE_HTTPS: true
    

2) Reconstruir Discourse

./launcher rebuild app

3) Configurar Caddy

  • Instalar Caddy, solo usando los valores predeterminados oficiales: Install — Caddy Documentation

  • Ajustar /etc/caddy/Caddyfile

    forum.example.com {
          reverse_proxy localhost:8080
    }
    

    Si tienes multisitio, puedes simplemente listar tus dominios:

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

    También puedes ejecutar systemctl status caddy para verificar la ubicación del archivo de configuración predeterminado.

4) Ejecutar Caddy

systemctl start caddy

Recargar la configuración después de los cambios:

cd /etc/caddy
caddy reload
2 Me gusta

Hola, gracias por el tutorial.

¿Hay alguna ventaja en usar Caddy para una configuración que no es multi-sitio? ¿Rendimiento o algo más?

No sé sobre eso… Yo solo uso esta configuración en mis servidores de staging ahora porque es realmente fácil añadir o cambiar instancias sin tener problemas con los certificados.

2 Me gusta

Hice algo similar en una configuración multisitio para optimizar la configuración SSL…
… pero actualicé a Caddy v2 y estoy usando docker-compose con una configuración multisitio.

En web.yml:

  • usando solo templates/web.socketed.template.yml y sin archivos yml de SSL.
  • comentando los puertos \"443:443\", \"80:80\", etc.
  • agregando DISCOURSE_HOSTNAME_ALIASES y DISCOURSE_FORCE_HTTPS: true

Esto utiliza la última versión de Caddy 2, por lo que podría verse diferente a algunas de las configuraciones de Caddy v1 mencionadas anteriormente en este tema.

Este es el archivo bash que crea inicialmente los archivos relevantes y arranca caddy:

#!/usr/bin/env bash

# Crear directorios necesarios
mkdir -p /var/caddy
mkdir -p /var/caddy/data
mkdir -p /var/caddy/config



# Crear el Caddyfile simplificado
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

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

# Navegar al directorio de caddy e iniciar
cd /var/caddy

# Iniciar Caddy
docker compose up -d
2 Me gusta