【问题标题】:nginx location deny file extension not allowed by regex正则表达式不允许 nginx 位置拒绝文件扩展名
【发布时间】:2019-10-30 10:18:41
【问题描述】:

nginx 上,我试图只允许某些扩展名并拒绝 所有其他,例如 gif、zip、exe、php给定子树。

我想使用正则表达式来定义允许的扩展。

这是我尝试使用负面展望

location /wp-content/userimages/ {
  location ~* (\.(?!(jpg|png|jpeg)))$ {
    deny all;;
  }
}

【问题讨论】:

    标签: regex nginx pcre nginx-location


    【解决方案1】:

    (\.(?!(jpg|png|jpeg)))$ 正则表达式匹配输入末尾的.,因为\.$ 之间没有消费模式。您需要为扩展添加一个消费模式,例如[^.]+ 匹配除. 之外的任何 1+ 字符:

    \.(?!(png|jpe?g)$)[^.]+$
    

    注意,我将jpeg|jpg 缩小为jpe?g,并将字符串结尾检查添加到前瞻中,以便可以匹配以禁止扩展名开头的扩展名(如png2(如果你遇到过)),并且png 不能。

    【讨论】:

    • 是的,非常锋利,$ 在前瞻和结尾!
    猜你喜欢
    • 1970-01-01
    • 2013-03-05
    • 1970-01-01
    • 2010-09-19
    • 1970-01-01
    • 1970-01-01
    • 2013-03-18
    • 1970-01-01
    • 2020-09-11
    相关资源
    最近更新 更多