问题
在Caddy
中,请求路径默认会在Caddy
的路径中查找。然而,如果是Vue项目,URI通常会是路由,如果此时刷新页面就可能导致页面出错。
因此,我们可以使用 try_files
来在页面找不到时重定向到index.html
。
try_files {path} /index.html;
然而,加入这种配置会导致 handle
的监听路径冲突,从而使handle
配置失效。
解决方法
为了解决这个问题,我们可以将 try_files {path} /index.html
配置单独放在 handle
中。这样既可以使API请求正常生效,又不会影响路由的配置。
handle /api/* {
reverse_proxy localhost:30000
}
handle {
file_server
try_files {path} /index.html
}
这样的配置使得 try_files
只在特定的路径下生效,保持了handle
配置的有效性。