如何让nginx-gridfs正确使用正则表达式位置匹配的结果

发布于 2024-12-09 20:31:34 字数 773 浏览 2 评论 0原文

简而言之,这不起作用:

location ~* /grid/(.+)/ {
         gridfs filestore field=filename type=string root_collection=storage.$1;
}

这是使用 https://github.com/mdirolf/nginx-gridfs

它有很多问题。这只是一个仅供参考,因为我不认为它可以用位置正则表达式来完成,因为:

  1. 组不是进程,并且 $1 被逐字用作“存储。$1”集合名称
  2. 不知何故,可能在 C 代码中,文件名是通过从匹配中剥离字符、删除正则表达式中指定的确切字符数来获取的。意思是,“/grid/(.+)/”的长度为 10 个字符,这是从整个 url 中删除的字符数(显然,减去域名)。我对此进行了相当多的测试(不仅仅是通过 C 代码),这就是它正在做的事情。

我假设这仍然可以通过某种方式重写或另一个 nginx 配置来完成。

作为最后的手段,我将联系项目维护者,但 nginx 的配置似乎足够灵活,可以解决这些问题,正如广告中所宣传的那样:

location ~* /grid/ABC/ {
         gridfs filestore field=filename type=string root_collection=storage.ABC;
}

In short, this doesn't work:

location ~* /grid/(.+)/ {
         gridfs filestore field=filename type=string root_collection=storage.$1;
}

This is using https://github.com/mdirolf/nginx-gridfs

There are multiple problems with it. This is just an FYI, as I don't think it can be done with location regex, because of:

  1. group is not processes, and $1 is being used verbatim as a "storage.$1" collection name
  2. Somehow, probably in the C code, file name is picked up by stripping characters from the match, by removing exact number of characters that is specified in regex. Meaning, "/grid/(.+)/" is 10 characters long, and this is how many characters are stripped from the entire url (minus domain, obviously). I've tested this quite a bit (short from just going through C code), and this is what it's doing.

I'm assuming this can be still done with rewrite somehow or another nginx config.

As a last resort, I'll reach out to the project maintainer, but nginx's configuration is flexible enough, it seems, to work around the issues, as this works as advertized:

location ~* /grid/ABC/ {
         gridfs filestore field=filename type=string root_collection=storage.ABC;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文