二级目录部署vue3的Caddyfile
https://domain.com {
redir /portal /portal/ 308
handle /portal/* {
uri strip_prefix /portal
root * /www/domain.com/portal #打包后的静态文件
try_files {path} /index.html
file_server
encode zstd gzip #开启zip
}
}
在根目录部署laravel的Caddyfile
https://api.domain.com {
root /www/api.domain.com/my-laravel/public
encode gzip
# PHP-FPM Configuration for Caddy use docker php80
php_fastcgi php80:9000
file_server
}
如果laravel的接口需要开启跨域访问,则可以参考以下的Caddyfile,此外laravel也要根据版本,选择相应的开启cors的方法,此处不展开。
(cors) {
@cors_preflight{args.0} method OPTIONS
@cors{args.0} header Origin {args.0}
handle @cors_preflight{args.0} {
header {
Access-Control-Allow-Origin "{args.0}"
Access-Control-Allow-Methods "GET, POST, PUT, PATCH, DELETE, OPTIONS"
Access-Control-Allow-Headers *
Vary Origin
Access-Control-Max-Age "3600"
defer
}
respond "" 204
}
handle @cors{args.0} {
header {
Access-Control-Allow-Origin "{args.0}"
Access-Control-Expose-Headers *
defer
}
}
}
https://api.domain.com {
import cors {header.origin} # 允许任意来源的跨域访问
root /www/api.domain.com/my-laravel/public
encode gzip
# PHP-FPM Configuration for Caddy use docker php80
php_fastcgi php80:9000
file_server
}