gpt4 book ai didi

regex - 如何在该位置使用Nginx Regex

转载 作者:行者123 更新时间:2023-12-04 17:16:26 26 4
gpt4 key购买 nike

该Web项目将静态内容放入一些/content/img文件夹中。
网址规则是:/img/{some md5}
但在文件夹中的位置:/content/img/{前两位数字}/

例子

url:      example.com/img/fe5afe0482195afff9390692a6cc23e1
location: /www/myproject/content/img/fe/fe5afe0482195afff9390692a6cc23e1

这个nginx位置是正确的,但是不安全(符号点在regexp中不好):
        location ~ /img/(..)(.+)$ {
alias $project_home/content/img/$1/$1$2;
add_header Content-Type image/jpg;
}

下一个位置更正确,但不起作用:
        location ~ /img/([0-9a-f]\{2\})([0-9a-f]+)$ {
alias $project_home/content/img/$1/$1$2;
add_header Content-Type image/jpg;
}

帮助我找到错误以获取更正确的nginx位置。

最佳答案

在POSIX BRE模式中,必须在限定符中转义花括号,并且NGINX不使用该正则表达式。在这里,您不应该转义限制量大括号,但需要告诉NGINX,您将大括号作为正则表达式模式字符串的一部分进行了传递。

因此,您需要用双引号将整个模式括起来:


location ~ "/img/([0-9a-fA-F]{2})([0-9a-fA-F]+)$"

这是一个 regex demo

请注意,在当前情况下,您可以重复子模式:
 /img/([0-9a-fA-F][0-9a-fA-F])([0-9a-fA-F]+)$
^^^^^^^^^^^^^^^^^^^^^^^^

关于regex - 如何在该位置使用Nginx Regex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41261567/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com