【问题标题】:nginx rewrite /api to root domain for SPA(Single page application)nginx 将 /api 重写为 SPA 的根域(单页应用程序)
【发布时间】:2022-02-22 19:28:42
【问题描述】:

我正在努力为 SPA 应用程序重新编写 nginx 规则。我的应用程序在https://example.com 上运行,它也可以在https://example.com/api 上运行。我想重写。当我输入https://example.com 时,nginx 规则(或任何其他建议)应该从https://example.com/api 提供,但前端域不应重定向到它。我想按原样保留https://example.com

我已经使用 nginx 为非 SPA 应用程序完成了此操作。但我不确定如何为 SPA(单页应用程序)执行此操作,因为 /api 是 URI 而不是 URL 路径。

截至目前,配置如下:

location / {

                try_files $uri $uri/ /index.html;
                 rewrite / /api$ last;
        }

【问题讨论】:

  • 你可以试试:try_files $uri $uri/ /api/index.html;
  • 对不起。它不工作。虽然我已经尝试过了,而且我知道在简单的基于后端的路由上它会起作用。但是对于 SPA 应用程序 /api 来自主要的 index.html 渲染。这就是问题

标签: nginx url-rewriting single-page-application


【解决方案1】:

我能够使用“kong API 网关”并实现它。 nginx 对我不起作用,我不得不使用入口控制来解决我的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-31
    • 2011-06-30
    • 2013-07-26
    • 1970-01-01
    • 1970-01-01
    • 2015-06-02
    • 2012-11-13
    • 1970-01-01
    相关资源
    最近更新 更多