【问题标题】:How to route URLs to index.html for localized Angular application in lighttpd?如何将 URL 路由到 index.html 以在 lighttpd 中本地化 Angular 应用程序?
【发布时间】:2021-12-25 01:55:36
【问题描述】:

Angular 应用程序被部署到 lighttpd 服务器中。例如,用户正在浏览 example.com/product/12 并将此链接发送给某人。如果不重写 URL,则会出现 404 错误。

鉴于:

  • 本地化版本位于子目录中,例如/en, /it, /cn
  • 每个语言子目录都有自己的index.html
  • Angular 生成像 styles.920e98031c0f5e2e220f.css 这样的块

我只想将不存在的文件的 URL 重写为 /language_code/index.html

第一个解决方案是:

server.error-handler-404 = "/index.html"

但是每个本地化目录中有几个index.html

另一种方法是这样的:

url.rewrite-once = ( "(?!\.\w+$)" => "/index.html" )

但是:

  • 如果 URL 包含一个点,但它是路由的一部分,例如: /user/my.nick?
  • Angular 在文件名中生成带有点的块(可以通过更多的重写规则来处理)

没有按预期工作的第三种方法(并且可能性能较低 - 我们需要检查文件是否存在):

url.rewrite-if-not-file = (
  "^/(de|en)/" => "/$1/index.html"
)

我理解正确吗?这是否意味着:如果请求的文件不存在且文件路径以/de//en/ 开头,则重写为/de/index.html/en/index.html

但尽管文件存在,但所有 URL 都被重写为 /de/index.html/en/index.html

本地化 Angular 2+ 应用程序的正确配置是什么?

版本:

  • Lighttpd 版本:1.4.59 (ssl)
  • Docker 镜像:sebp/lighttpd:latest

【问题讨论】:

    标签: angular configuration lighttpd


    【解决方案1】:

    使用 lighttpd mod_magnet 和 lua 的内容协商: https://wiki.lighttpd.net/AbsoLUAtion#Content-Negotiation

    编辑:另一个选项是server.error-handler-404 指向一个 FastCGI 或 CGI 脚本,该脚本确定丢失文件的所需目标。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-24
      • 2020-01-01
      • 2017-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多