Nginx location regex - character class and range of matches

I am trying to set up a regular expression for the path /s/<4-6 character string here> where I take a string with 4-6 characters as $ 1.

I tried using the following two entries, but both failed

 location ~ ^/s/([0-9a-zA-Z]){4,6}+$ { ... location ~ ^/s/([0-9a-zA-Z]{4,6})+$ { ... 

The first one contains the "unknown directive", and the second contains "pcre_compile () failed: missing)"

EDIT

The following routes will be served by this location:

 /s/1234 (and I would capture '1234' in $1) /s/12345 (and I would capture '12345' in $1) /s/123456 (and I would capture '123456' in $1) /s/abcd (and I would capture 'abcd' in $1) /s/abcde (and I would capture 'abcde' in $1) /s/abcdef (and I would capture 'abcdef' in $1) /s/a1b2c (and I would capture 'a1b2c' in $1) 

The following routes will NOT be served by this location:

 /s/1 /s/12 /s/123 /s/a /s/ab /s/abc /s/abc1234 /s/12345678 

etc...

+6
source share
1 answer

If you want to write 4 to 6 characters, why didn't you put a quantifier inside the parentheses?

Something like this is possible:

 location ~ "^/s/([0-9a-zA-Z]{4,6})$" {... 

Curly braces are used both in the regular expression and to control the block, you must enclose your regular expression in quotation marks (single or double) (<- wiki nginx)

+22
source

Source: https://habr.com/ru/post/943464/


All Articles