简体   繁体   English

正则表达式:匹配“ /”。 仅当未以“ / _local /”为前缀时。

[英]Regex: match '/.' only if not prefixed with '/_local/.'

I hate regex! 我讨厌正则表达式! My brain just does not work the way regex does. 我的大脑无法像正则表达式那样工作。 Can someone please help with the following problem I'm trying to solve ? 有人可以帮忙解决我正在尝试解决的以下问题吗?

I have a nginx config file currently blocking all attempts to access system or hidden files for security reasons (ie files beginning with '.') with the below location config : 我有一个nginx配置文件,当前出于安全原因阻止使用以下位置config的所有尝试访问系统或隐藏文件(即以“。”开头的文件)的尝试:

location ~ \/\. {
  access_log off;
  log_not_found off;
  deny all;
}

However, this also blocks my CouchDB server requests to example.com/db/_local/.XYZ 但是,这也会阻止我的CouchDB服务器对example.com/db/_local/.XYZ请求

I want to allow all requests to X/_local/.X but deny all other requests to X/.X . 我想允许对X/_local/.X所有请求,但拒绝对X/.X所有其他请求。

So my regex has to match all /. 所以我的正则表达式必须匹配所有/. but not /_local/. 但不是/_local/. .

Eg 例如

example.com/.secretfile          // deny
example.com/db/_local/.LONGHASH  // allow
example.com/other/.other         // deny

Can anyone RegEx gurus please help out ? RegEx专家们可以帮忙吗?

Assumed that Ngninx is fully PCRE-compatible, this should be a solution: 假设Ngninx与PCRE完全兼容,这应该是一个解决方案:

location ~ (?<!\/_local)\/\. {

This uses a “negative look-behind assertion”: (?<!\\/_local) means “not prefixed by /_local ”. 这使用“负向后断言”: (?<!\\/_local)表示“未以/_local前缀”。

Edit: Escaped forward slashes 编辑:转义的正斜杠

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM