配置nginx以重写所有路径上的哈希
我首先使用nginx作为静态文件服务器,并尝试重写所有uri(如果它们包含指向模拟目录的散列) 举个例子可能会更清楚 假设用户请求页面url如以下所示:配置nginx以重写所有路径上的哈希,nginx,url-rewriting,nginx-location,Nginx,Url Rewriting,Nginx Location,我首先使用nginx作为静态文件服务器,并尝试重写所有uri(如果它们包含指向模拟目录的散列) 举个例子可能会更清楚 假设用户请求页面url如以下所示: /api/some/path/to/ecbac7cb-21ca-3d22-98ed-4d063f138d0b/some/other/path/02167621-5c01-45c5-98ba-eb3ba1bf4b2a/list /api/some/other/path/to/ecbac7cb-21ca-3d22-98ed-4d063f138d0
/api/some/path/to/ecbac7cb-21ca-3d22-98ed-4d063f138d0b/some/other/path/02167621-5c01-45c5-98ba-eb3ba1bf4b2a/list
/api/some/other/path/to/ecbac7cb-21ca-3d22-98ed-4d063f138d0b
我想先尝试获取文件,但回退会是这样的
/api/some/path/to/__any_hash__/some/other/path/__any_hash__/list.json
/api/some/other/path/to/__any_hash__.json
这是一个配置,我试图远。。。但很明显,它不起作用
server {
...
location /api {
try_files $uri $uri/index.html $uri.html $uri.json $uri.xml @rewrites;
}
location @rewrites {
rewrite /([0-9\-a-f]{36})/ __any_hash__ break;
try_files $uri.xml $uri.json @backend;
}
有人有想法吗?您的
位置@rewrites
应该是这样的:
location @rewrites {
rewrite "^(.*)/[0-9\-a-f]{36}(/.*)$" $1/__any_hash__$2 last;
return 404;
}
$1
和$2
捕获要替换的哈希前后的URI片段。last
导致替换后重新尝试location/api
。该过程将循环,直到完成所有替换。返回404
仅在完全替换的URI找不到文件时才会调用。为什么哈希长度限制为{10}
,这是复制/粘贴错误吗?@zeachco Oops yes-在我的测试中,我懒得键入36个字符。更新答案。