Berlin
Berlin

Reputation: 1464

Nginx regex in location path or server_name

I am trying to use regex with nginx to allow requests containing some conditions without success.

for example one of the errors ginx: [emerg] pcre_compile() failed: missing ) in "(^/(unsafe)\/([0-9]" in /etc/nginx/sites-enabled/file.conf:33.

The regex should be ok according to https://regex101.com/, so I guess the issue with the nginx and this regex, so I need some help how to do it.

Regex:

^(http|https):\/\/(my[.]domain[.]com)\/(unsafe)\/([0-9]{0,4})x([0-9]{0,4})\/((ch)|[a-z0-9]{0,32})\/(([3_]+[a-z0-9]{0,32}\.jpg)|([c_1_]+[a-z0-9]{0,32}\.jpg)|([a-z0-9]{0,32}\.jpg)|(fb\/[0-9]{0,6}.jpg)|([a-z0-9]{0,6}\/[0-9]{0,6}.jpg))

Urls:

https://my.domain.com/unsafe/1530x55/7550/3_c892b2c2de030f40.jpg
https://my.domain.com/unsafe/1030x1345/71b0c5d3f50/c_1_c8928dc3e030f40.jpg
https://my.domain.com/unsafe/1030x4443/79481b0c5d3f50/c892333e030f40.jpg
https://my.domain.com/unsafe/0x0/ch/fb/157233.jpg
https://my.domain.com/unsafe/0x0/ch/6chars/123456.jpg

1.

server_name ~^(my[.]domain[.]com)\/(unsafe)\/([0-9]{0,4})x([0-9]{0,4})\/((ch)|[a-z0-9]{0,32})\/(([3_]+[a-z0-9]{0,32}\.jpg)|([c_1_]+[a-z0-9]{0,32}\.jpg)|([a-z0-9]{0,32}\.jpg)|(fb\/[0-9]{0,6}.jpg)|([a-z0-9]{0,6}\/[0-9]{0,6}.jpg)); or:

location ~ (/(unsafe)\/([0-9]{0,4})x([0-9]{0,4})\/((ch)|[a-z0-9]{0,32})\/(([3_]+[a-z0-9]{0,32}\.jpg)|([c_1_]+[a-z0-9]{0,32}\.jpg)|([a-z0-9]{0,32}\.jpg)|(fb\/[0-9]{0,6}.jpg)|([a-z0-9]{0,6}\/[0-9]{0,6}.jpg))) { empty }

Thanks

Upvotes: 0

Views: 927

Answers (1)

Richard Smith
Richard Smith

Reputation: 49792

If you have braces in your regular expression, nginx requires it to be quoted. It's mentioned for the rewrite directive, but I think it's applicable for most nginx directives that accept a regular expression.

Upvotes: 1

Related Questions