Caddy在CentOS 7的安装与使用

前言

Caddy是一个用Go语言编写的灵活和强大的web服务器,配置简单,自动HTTPS,可以为站点提供更好的安全性和性能。

Caddy 2 is a powerful, enterprise-ready, open source web server with automatic HTTPS written in Go

下面讲解一下caddy在CentOS 7环境下的安装和使用

安装

yum install yum-plugin-copr
yum copr enable @caddy/caddy
yum install caddy

安装完成后输入caddy version,看到以下提示,就说明Caddy安装成功了

v2.6.4 h1:2hwYqiRwk1tf3VruhMpLcYTg+11fCdr8S3jhNAdnPy8=

启动Caddy

在终端输入以下命令:

caddy start

这个命令会启动Caddy,您可以在终端里输入以下命令来验证:

 curl localhost:2019/config/

localhost:2019用于控制Caddy,默认情况下仅限于localhost

This is not your website: the administration endpoint at localhost:2019 is used for controlling Caddy and is restricted to localhost by default.

配置caddy

配置Caddy的一种方法是使用Caddyfile,如下:

  • 创建一个名字为Caddyfile的文件
touch Caddyfile

然后在文件里输入以下内容:

:2015

respond "Hello, world!"

重新启动caddy,在浏览器里输入localhost:2015,就能够看到"Hello, world!"字样了

配置Static files

localhost 

file_server
:80 {
    file_server browse {
           root /root
     }
}

配置Reverse proxy

reverse_proxy localhost:9000

配置HTTPS

example.com {
    root /var/www
    gzip
}

默认证书路径为:~/.local/share/caddy/certificates/acme-v02.api.letsencrypt.org-directory

启动时指定配置文件

caddy start --config /usr/local/Caddyfile

Caddy常用命令

Caddy命令格式如下:

caddy <command> [<args...>]
命令 说明
caddy version Prints the version
caddy run Starts the Caddy process in the foreground
caddy start Starts the Caddy process in the background
caddy stop Stops the running Caddy process
caddy reload Changes the config of the running Caddy process
caddy fmt Formats a Caddyfile

综合示例

Caddyfile内容如下:

xxx.com {
        handle_path /releases* {
          file_server browse {
             root /root/releases
          }
        }

        handle {
           reverse_proxy localhost:5000
        }
}
www.xxx.com {
   redir https://xxx.com{uri}
}

/releases的请求转发到静态文件服务, 其他请求转发到代理服务上

资源

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容