B
B
blackbeard2018-03-29 16:30:36
Nginx
blackbeard, 2018-03-29 16:30:36

How to set a range of digits in nginx (regular expression)?

There is a certain url that ends with a number. I need one rule to work with one range (for example, up to a thousand), and another rule for the rest. I have already found the required line in the config, but I can’t set the range (
As an example - [0-9] for all digits, but I need a certain range. Optional - it can be by value, i.e. two-digit / three-digit, etc.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
I
Ildar Saribzhanov, 2018-03-29
@Black_beard_ast

Probably something like this

location ~ "^/[\d]{2}" {
    # ...
}

location ~ "^/[\d]{3}" {
    # ...
}

location ~ "^/[\d]{4}" {
    # ...
}

But it's in ranks

R
RidgeA, 2018-03-29
@RidgeA

Well, regular expressions don't work with numbers, they don't work
They work with strings
You need to specify a regular expression for which the string matches from '1' to ''1000'' - at least you can check the number of consecutive digits in a string in a string. There are quantifiers for this.

S
sim3x, 2018-03-29
@sim3x

\d{2,3}
https://regex101.com/r/dlqwAz/1

A
Alexey Skobkin, 2018-03-31
@skobkin

You can use this approach.
https://stackoverflow.com/questions/8592488/regex-...
But in general, it's better to parse request parameters inside the application, and not in Nginx.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question