0

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

0

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

you right on the location path, but somehow when I do it on the server_name, i got an error nginx: [emerg] directive "server_name" is not terminated by ";", but I have the ;, any thoughts?
The server_name regular expression needs to be quoted if it contains braces. The server_name expression in your question is invalid anyway, as it is not a server name.
seems like no need to escape forward-slashes in nginx location. thanks for your help.
Correct. But you have some .s in your question that should be escaped. Also, not sure what [c_1_] means.
I figured it out how to do it with nginx, somethings needed to be change, not everything that work in https://regex101.com/ also work in nginx.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.