Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx rewrite regex min,max repetition

I am using this rewrite in NGINX.
rewrite ^/test[^a-zA-Z0-9]{2}/?$ https://www.google.com permanent; // doesn't work

The server fails to start with I add the min {2} repetition in the regex. The server comes up when I remove that like here:
rewrite ^/test[^a-zA-Z0-9]/?$ https://www.google.com permanent; // this works

I have tried both {min,max} params. The error that I get when I use the min repetition is as below.
directive "rewrite" is not terminated by ";"

The context of this rewrite is server.

Can someone tell if what am I missing? Is there some module that need to be installed for this to work?
My prod NGINX version is 1.4 and I tried it on my local with 1.10.

like image 312
Abhishek Avatar asked May 02 '16 21:05

Abhishek


1 Answers

This is by design -- curly brackets are special in nginx.conf, if used as a part of a regular expression, then you have to use double quotes around your regular expression if you're using the braces.

http://nginx.org/r/rewrite

If a regular expression includes the “}” or “;” characters, the whole expressions should be enclosed in single or double quotes.

E.g.,

 rewrite "^/test[^a-zA-Z0-9]{2}/?$" https://www.google.com/ permanent;
like image 177
cnst Avatar answered Nov 04 '22 17:11

cnst