Home > Article > Operation and Maintenance > How to use Nginx web server caddy
Caddy is a powerful, highly scalable web server that has currently gained 38K Stars on GitHub. Caddy is written in Go language and can be used for static resource hosting and reverse proxy.
Compared with Nginx’s complex configuration, its original Caddyfile configuration is very simple;
The configuration can be dynamically modified through the Admin API it provides;
supports automated HTTPS configuration by default, and can automatically apply for HTTPS certificates and configure them;
Can be expanded to tens of thousands of sites;
can be executed anywhere, with no additional dependencies;
is written in Go language , memory security is more guaranteed.
First of all, we install Caddy directly on CentOS 8. Installation using the DNF tool is undoubtedly the simplest. The Docker installation method will be introduced later.
Use the following command to install Caddy through the DNF tool. After successful installation, Caddy will be registered as a system service;
dnf install 'dnf-command(copr)' dnf copr enable @caddy/caddy dnf install caddy
Use systemctl status caddy to check the status of Caddy. You can find that Caddy has been registered as a system service. service, but it is not enabled yet.
Let’s experience the basic use of Caddy. They are common operations for web servers. You can definitely use them!
Let us try to use Caddy to get started, specifying that Caddy runs on the 2015 port and returns the "Hello, world!" message.
Using the caddy command directly will output the common commands of Caddy. Basically, you will know how to use it by reading the introduction. The common commands are marked;
Use caddy The start command allows the Caddy service to run in the background;
Caddy uses the JSON format configuration file by default. However, since it is more troublesome to write the JSON format configuration, Caddyfile is also provided. Simple configuration form, use the following command to automatically convert Caddyfile into JSON configuration;
caddy adapter
We can first create a file named Caddyfile with the following content, and then use caddy adapter to convert it into JSON configuration, Then use caddy reload to make the configuration effective. The configuration will listen to the 2015 port and return Hello, world!;
:2015 respond "Hello, world!"
Then we use the curl command to access localhost:2015 and the specified information will be returned;
Of course we can also use the Admin API provided by Caddy to view the configuration information, just use the following command;
curl localhost:2019/config/
The current JSON configuration is as follows, if you use JSON configuration directly You need to write the following configuration, using Caddyfile is indeed much more convenient!
{ "apps": { "http": { "servers": { "srv0": { "listen": [":2015"], "routes": [{ "handle": [{ "body": "Hello, world!", "handler": "static_response" }] }] } } } } }
The following case will use Caddyfile for configuration. We need to understand its syntax. The specific syntax rules of Caddyfile are as follows.
Introduce the keywords in the above picture to help understanding.
Keyword | Explanation | Use |
---|---|---|
Global options block | Server global configuration | Can be used to configure whether to enable HTTPS and Admin API, etc. |
Snippet | Reusable configuration snippets | After definition, it can be referenced through the import keyword |
Site Block | Single website configuration | Static proxy can be configured through file_server and reverse_proxy Dynamic agents can be configured |
Matcher definition | Match definition | By default the directive will have a global impact, through which the scope of influence can be specified |
Comment | Comment | Use the # symbol starting with |
Site address | Website address | HTTPS is used by default. If you need to enable HTTP, you need to specify the |
Directive | directive | directive given at the beginning of http:// Caddy’s powerful features |
反向代理就是当请求访问你的代理服务器时,代理服务器会对你的请求进行转发,可以转发到静态的资源路径上去,也可以转发到动态的服务接口上去。我们以代理域名为例,讲解如何进行静态和动态代理。
静态代理就是将请求代理到不同的静态资源路径上去,这里我们将对docs.macrozheng.com的请求代理到我的文档项目中,对mall.macrozheng.com的请求代理到mall的前端项目中。
首先我们修改下本机的host文件:
192.168.3.106 docs.macrozheng.com
192.168.3.106 mall.macrozheng.com
然后将我们的文档项目和mall前端项目上传到Caddy的html目录中去,并进行解压操作:
修改Caddyfile文件,使用如下配置,修改完成后使用caddy reload命令刷新配置;
http://docs.macrozheng.com { root * /mydata/caddy/html/docs file_server browse } http://mall.macrozheng.com { root * /mydata/caddy/html/mall file_server browse }
如果你的Caddyfile文件格式不太合格的话,会出现如下警告,直接使用caddy fmt --overwrite格式化并重写配置即可解决;
通过docs.macrozheng.com即可访问部署好的文档项目了:
通过mall.macrozheng.com即可访问到部署好的前端项目了。
动态代理就是把代理服务器的请求转发到另一个服务上去,这里我们将把对api.macrozheng.com的请求代理到演示环境的API服务上去。
首先我们修改下本机的host文件,添加如下规则
192.168.3.106 api.macrozheng.com
修改Caddyfile文件,使用如下配置,修改完成后使用caddy reload命令刷新配置;
http://api.macrozheng.com { reverse_proxy http://admin-api.macrozheng.com }
之后通过api.macrozheng.com/swagger-ui.html即可访问到mall-admin的API文档页面了。
如果我们的服务器带宽比较低,网站访问速度会很慢,这时我们可以通过让Caddy开启Gzip压缩来提高网站的访问速度。这里我们以mall的前端项目为例来演示下它的提速效果。
我们需要修改Caddyfile文件,使用encode指令开启Gzip压缩,修改完成后使用caddy reload命令刷新配置;
http://mall.macrozheng.com { root * /mydata/caddy/html/mall encode { gzip } file_server browse }
有个比较大的JS文件压缩前是1.7M;
压缩后为544K,访问速度也有很大提示;
另外我们可以看下响应信息,如果有Content-Encoding: gzip这个响应头表明Gzip压缩已经启用了。
有的时候我们的网站更换了域名,但还有用户在使用老的域名访问,这时可以通过Caddy的地址重写功能来让用户跳转到新的域名进行访问。
我们需要修改Caddyfile文件,使用redir指令重写地址,修改完成后使用caddy reload命令刷新配置;
http://docs.macrozheng.com { redir http://www.macrozheng.com }
此时访问旧域名docs.macrozheng.com会直接跳转到www.macrozheng.com去。
有时候我们需要使用同一个域名来访问不同的前端项目,这时候就需要通过子目录来区分前端项目了。
比如说我们需要按以下路径来访问各个前端项目;
www.macrozheng.com #访问文档项目
www.macrozheng.com/admin #访问后台项目
www.macrozheng.com/app #访问移动端项目
我们需要修改Caddyfile文件,使用route指令定义路由,修改完成后使用caddy reload命令刷新配置。
http://www.macrozheng.com { route /admin/* { uri strip_prefix /admin file_server { root /mydata/caddy/html/admin } } route /app/* { uri strip_prefix /app file_server { root /mydata/caddy/html/app } } file_server * { root /mydata/caddy/html/www } }
Caddy能自动支持HTTPS,无需手动配置证书,这就是之前我们在配置域名时需要使用http://开头的原因,要想使用Caddy默认的HTTPS功能,按如下步骤操作即可。
首先我们需要修改域名的DNS解析,直接在购买域名的网站上设置即可,这里以docs.macrozheng.com域名为例;
请使用以下命令确认DNS解析记录是否正确,注意所配置的服务器的80和443端口需要在外网中可以正常访问:
curl "https://cloudflare-dns.com/dns-query?name=docs.macrozheng.com&type=A" \ -H "accept: application/dns-json"
修改Caddyfile配置文件,进行如下配置;
docs.macrozheng.com { root * /mydata/caddy/html/docs file_server browse }
然后使用caddy run命令启动Caddy服务器即可,是不是非常方便!
caddy run
当然Caddy也是支持使用Docker进行安装使用的,其使用和直接在CentOS上安装基本一致。
首先使用如下命令下载Caddy的Docker镜像;
docker pull caddy
然后在/mydata/caddy/目录下创建Caddyfile配置文件,文件内容如下;
http://192.168.3.105:80 respond "Hello, world!"
之后使用如下命令启动caddy服务,这里将宿主机上的Caddyfile配置文件、Caddy的数据目录和网站目录挂载到了容器中;
docker run -p 80:80 -p 443:443 --name caddy \ -v /mydata/caddy/Caddyfile:/etc/caddy/Caddyfile \ -v /mydata/caddy/data:/data \ -v /mydata/caddy/html:/usr/share/caddy \ -d caddy
之后使用docker exec进入caddy容器内部执行命令;
docker exec -it caddy /bin/sh
输入Caddy命令即可操作,之后的操作就和我们直接在CentOS上安装一样了。
The above is the detailed content of How to use Nginx web server caddy. For more information, please follow other related articles on the PHP Chinese website!