使用Caddy代替NGNIX作为反向代理

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 个赞

抱歉顶起了旧帖子,我正在尝试让 Caddy 与 Discourse 协同工作。在您的 Caddy 配置中,您使用了“proxy”,但当我使用它时,它会显示语法错误并且无效。难道“proxy”现在已经被更改为“reverse_proxy”了吗?

这是我的配置:

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

我想是的。你试过了吗?

1 个赞

谢谢 Matt!
我可以确认,不使用容器的设置在 2025 年仍然有效 :smiley:

forum.website.com {
        reverse_proxy localhost:8080
}

只使用“proxy”不起作用。

对我也很好用!

但是,我遇到了“混合内容”警告,这是遵循那个非常直接的设置出现的:


为了解决这些问题,我不得不在配置文件 (app.ymlweb_only.yml) 的 env 部分添加一个额外的指令:

# 强制 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 个赞