如何在不发送重定向的情况下使 nginx 重新映射 URL 的一部分?

e-s*_*tis 3 url nginx mapping

我在一条看起来像这样的路径上有图片:

/0/1/2/3/4/01234/screenshots/1.jpg
Run Code Online (Sandbox Code Playgroud)

访问它的 URL 如下所示:

/static/0/1/2/3/4/01234/screenshots/1.jpg
Run Code Online (Sandbox Code Playgroud)

我希望它看起来像这样:

/0/1/2/3/4/01234/a-desc-of-the-picture/screenshots/1.jpg
Run Code Online (Sandbox Code Playgroud)

或者类似的东西。目标是在 SEO 的 URL 中包含关键字。

但是想告诉nginx服务

/0/1/2/3/4/01234/screenshots/1.jpg
Run Code Online (Sandbox Code Playgroud)

当他看到:

/0/1/2/3/4/01234/a-desc-of-the-picture/screenshots/1.jpg
Run Code Online (Sandbox Code Playgroud)

我不希望它将用户重定向到正确的 URL,我只希望它在内部进行映射。

是否可以 ?我怎样才能做到这一点?

我在这里看到了类似的东西,但我找不到将它应用于我的案例的方法。菜鸟友好的东西将不胜感激。

wom*_*ble 5

假设您有一个可正则表达式的基本路径,您可以执行以下操作:

location ~ ^/static/(././././.....)/[^/]+/(.*)$ {
    alias /location/on/filesystem/$1/$2;
}
Run Code Online (Sandbox Code Playgroud)

Nginx 的别名指令比 Apache 的等价指令更灵活。