首页 > 解决方案 > 使用 nginx 将遗留 URL 重定向/重写到需要哈希的 Angular 应用程序

问题描述

我有一系列指向遗留应用程序的现有链接,这些链接需要由使用哈希 URL 格式的 Angular 应用程序处理。

例如,像这样的旧 URL:

example.com/downloads/anexistinguuid

需要在 nginx 中重定向到这个:

example.com/#/downloads/anexistinguuid

我尝试了以下方法:

   location /downloads {
        proxy_pass example.com/#/downloads/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

但这似乎不是正确的方法。

有很多关于如何从 URL 中删除散列的示例,但此时我无法更改 Angular 应用程序的 URL 策略,并且必须保持与已经存在的大量链接的兼容性,而我也可以t 改变。

标签: angularnginxredirecturl-rewriting

解决方案


您可能需要重定向而不是反向代理。

尝试:

location /downloads {
    return 301 /#$request_uri;
}

推荐阅读