无痛切换 caddy 代理服务器

本方案使用 Caddy 的自动HTTPS功能,部署完成后即可通过https访问您的服务。反向代理配置会自动处理SSL证书申请和续期,建议搭配防火墙做好端口安全管理。
创建配置文件
将下面的代码保存到 /srv/caddy/etc/Caddyfile,注意替换示例域名和证书邮箱。

{
    admin off
    email cert@example.org
}

www.example.org {
    reverse_proxy 127.0.0.1:6080
}

test.example.org {
    redir /admin /admin/ 301
    reverse_proxy /admin/* 127.0.0.1:8090
    reverse_proxy /* 127.0.0.1:8080
}

简要说明

admin off 禁用管理接口
email 设置SSL证书通知邮箱
www.example.org 将所有流量转发到本机6080端口
test.example.org 实现路径分流和URL重定向

创建服务脚本
将下面的代码保存到 /srv/caddy/docker,注意使用 lf 换行,并给予可执行权限。

#!/bin/sh
#
# @Author  Rehiy <wang@rehiy.com>
# @Website http://www.rehiy.com
#

APP_NAME=caddy

APP_FROM=caddy:alpine

APP_PATH=$(cd `dirname $0`; pwd)

#####################################################################################

if [ "$1" = "install" ] || [ "$1" = "update" ]; then
    docker pull $APP_NAME
fi

if [ "$1" = "remove" ] || [ "$1" = "update" ]; then
    docker rm -f $APP_NAME
fi

if [ "$1" = "install" ] || [ "$1" = "update" ]; then
    docker run -d --name $APP_NAME \
        --network host \
        --volume $APP_PATH/etc:/etc/caddy \
        --volume $APP_PATH/var:/data/caddy \
        $APP_FROM
fi

启动服务

/srv/caddy/docker install