全栈修行手册修习心得 · 随记成卷 订阅手记
慢慢写,认真生活。 / NOTES ON THE EVERYDAY

Caddy应用简单使用教程

部署与运维 AZ 3858浏览

1、快速实现本地目录浏览;需求场景,需要局域分享或web服务

#直接运行
caddy run

#然后通过 https://<local-ip>:2015 访问即可。有些时候内置端口有可能不是2015,这时候需要看控制台回显端口。

指定端口运行

#直接运行
caddy file-server --browse --listen :9527

#然后通过 https://<local-ip>:9527 访问即可。

2、配置域名启动

#加域名启动,使用http/https访问
caddy file-server --domain example.com

#反向代理访问
caddy reverse-proxy --from example.com --to localhost:9000

2、加载配置文件启动

#配置文件启动
caddy run --config ./Caddyfile

#配置文件样版

# redirect www to bare domain
www.example.com {
    redir https://example.com{uri} permanent
}

example.com {
    # log to stdout, which is captured by journalctl
    log {
        output stdout
        format console
    }

    # turn on standard streaming compression
    encode gzip zstd

    # reverse proxy /api to :3000
    reverse_proxy /api/* localhost:3000

    # reverse proxy some "well known" APIs
    reverse_proxy /.well-known/openid-configuration localhost:3000
    reverse_proxy /.well-known/jwks.json localhost:3000

    # serve static files from public folder, but not /api
    @notApi {
        file {
            try_files {path} {path}/ {path}/index.html
        }
        not path /api/*
        not path /.well-known/openid-configuration
        not path /.well-known/jwks.json
    }
    route {
      rewrite @notApi {http.matchers.file.relative}
    }
    root * /srv/example.com/public/
    file_server
}

更多请查看官方文档docs https://webinstall.dev/caddy/

转载请注明:TUTERM.COM » Caddy应用简单使用教程

这卷宗是你需要的?请宗主喝杯灵茶。

打赏宗主,支付宝(左)或微信(右)。

alipay weichat