如何解决Caddy reverse_proxy 和 React Router
我无法将我的 Caddyfile
设置为使用 React SPA 应用程序
在我当前的 Caddyfile
下面,React 路由器似乎正在工作(访问 mysite.com/faq
没有给出 404),但对 API 后端(例如 mysite.com/api/foo
)的调用似乎正在尝试加载 React Router 路由。
我们如何解决这个 Caddyfile
?
www.example.com {
redir https://example.com{uri}
}
example.com {
root * /root/example/frontend/build
file_server
encode gzip zstd
reverse_proxy /api/* api.example.com:8000
try_files {path} /index.html
tls admin@example.com
log {
output file /root/example/logs/access.log {
roll_size 100mb
roll_keep 5
roll_keep_for 720h
}
}
}
更新:此 Caddyfile
也不起作用,React 路由器不再起作用,访问 https://example.com/faq
时出现错误 404。但是,反向代理似乎正在工作:API 服务器在我们访问 https://example.com/api/foo
时获得了命中,但它错误地将它们作为 http://api.example.com:8000/api/foo
而不是 http://api.example.com:8000/foo
www.example.com {
redir https://example.com{uri}
}
example.com {
root * /root/example/frontend/build
file_server
encode gzip zstd
reverse_proxy /api/* api.example.com:8000
@notAPI {
not {
path /api/*
}
file {
try_files {path} {path}/ /index.html?{query}
}
}
rewrite @notAPI {http.matchers.file.relative}
tls admin@example.com
log {
output file /root/example/logs/access.log {
roll_size 100mb
roll_keep 5
roll_keep_for 720h
}
}
}
使用 Caddy v2.4.3
Caddy 使用 caddy start --config ~/foo/Caddyfile
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。