微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Caddy reverse_proxy 和 React Router

如何解决Caddy reverse_proxy 和 React Router

我无法将我的 Caddyfile 设置为使用 React SPA 应用程序

  1. React 路由器路由有效
  2. /api/(例如 /api/foo)的调用被反向代理到另一个位置

在我当前的 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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?