文章我写在 https://www.yuque.com/renjun586/gg9lfo/kq8su7

下面我只是单纯把内容复制过来了。

一、官方文档
https://caddyserver.com/docs/quick-starts/caddyfile
参考博客:(很全)
https://blog.csdn.net/flysnow_org/article/details/120136851
二、centos安装caddy
[root@localhost opt]# yum install yum-plugin-copr
[root@localhost opt]# yum copr enable @caddy/caddy
[root@localhost opt]# yum install caddy -y
[root@localhost opt]# caddy run
三、启动caddy
1、进入caddy配置文件目录,一般在/etc/caddy/下有一个Caddyfile文件,如果没有可以手动创建。

The Caddyfile is an easy way to configure your Caddy web server.

Unless the file starts with a global options block, the first

uncommented line is always the address of your site.

To use your own domain name (with automatic HTTPS), first make

sure your domain’s A/AAAA DNS records are properly pointed to

this machine’s public IP, then replace “:80” below with your

domain name.

#log /var/log/caddy.log

:80 {

Set this path to your site’s directory.

root * /usr/share/caddy

Enable the static file server.

file_server

Another common task is to set up a reverse proxy:

reverse_proxy localhost:8080

Or serve a PHP site through php-fpm:

php_fastcgi localhost:9000

}

2、然后使用命令启动caddy:

在控制台运行

sudo caddy run --config Caddyfile

在后台运行

sudo caddy start --config Caddyfile

–config是为了指定启动的配置文件,如果不写,默认是当前目录下的Caddyfile

3、启动成功后,在浏览器输入服务器地址可以看到这个页面

四、项目实践:caddy部署vue前端dist文件,并代理请求到后端
1、编写Caddyfile配置文件
caddy的配置文件在/etc/caddy/下面,为了能不同服务区分不同的配置文件,添加类似于nginx的包含所有.conf的配置:
sudo echo ‘import ./conf/*’ >> /etc/caddy/Caddyfile

2、如图,9091这个服务是配置失败的,可以访问前端,但是请求到不了后端。9095这个是成功的。我想要达到的目的是为了,访问 服务器:9095 这个地址是能进入前端网页,然后点击前端的各个功能按钮,能把请求正确的发到后端的。而且为了避免前端代码写死后端服务地址,在前端是这样配置请求服务地址的:
// todo 迁移到新服务器地址
// window.BASE_URL = ‘http://192.168.43.85:8187’;
// window.BASE_URL = ‘http://122.112.244.71:8187’;
window.BASE_URL = ‘/QRV_Backend’;
这样项目部署就和服务器地址没任何关系了,在哪台服务器部署都不用改前端这地方的代码。
3、至于为啥9091不行,9095那样就可以,俺也没理清楚。可以参考的文档有:
https://caddy.community/t/caddy-with-vue-router/12352/5
https://blog.csdn.net/xgangzai/article/details/111306287
https://caddyserver.com/docs/caddyfile/directives

可选:添加caddy系统守护进程
可参考的文档:
● https://caddyserver.com/docs/running#linux-service

● https://blog.csdn.net/hero2040407/article/details/81015125?spm=1001.2101.3001.6650.16&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-16-81015125-blog-103020276.pc_relevant_multi_platform_whitelistv3&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-16-81015125-blog-103020276.pc_relevant_multi_platform_whitelistv3&utm_relevant_index=17

1、在/etc/systemd/system下添加服务unit文件:caddy.service
[Unit]
Description=Caddy
Documentation=https://caddyserver.com/docs/
After=network.target network-online.target
Requires=network-online.target

[Service]
User=caddy
Group=caddy
ExecStart=/usr/bin/caddy run --environ --config /etc/caddy/Caddyfile
ExecReload=/usr/bin/caddy reload --config /etc/caddy/Caddyfile
TimeoutStopSec=5s
LimitNOFILE=1048576
LimitNPROC=512
PrivateTmp=true
ProtectSystem=full
AmbientCapabilities=CAP_NET_BIND_SERVICE
CapabilityBoundingSet=CAP_NET_BIND_SERVICE
NoNewPrivileges=true

[Install]
WantedBy=multi-user.target
AmbientCapabilities=CAP_NET_BIND_SERVICE
CapabilityBoundingSet=CAP_NET_BIND_SERVICE
NoNewPrivileges=true
sending configuration to instance: caddy responded with error: HTTP 400: {“error”:“loading config: loading new config: http app module: start: tcp: listening on :9091: listen tcp :9091: bind: permission denied”}
这几行很重要,是个坑,不加这个监听不了端口。
2、添加好系统守护进程后,启动caddy服务
sudo systemctl daemon-reload
sudo systemctl enable --now caddy
systemctl status caddy
四、caddy命令
https://mp.weixin.qq.com/s?__biz=MzI3MjU4Njk3Ng%3D%3D&chksm=eb3106cfdc468fd90d1e744a5f3d596ad54fd91dd113c3c44b46c737b791752c764791949c20&idx=1&mid=2247485248&scene=21&sn=02a6a3dcf78c21604b1349bcaae218ff#wechat_redirect

Logo

前往低代码交流专区

更多推荐