Руководство о том, как использовать регулярные выражения в Nginx разделе блока местоположения? - PullRequest
0 голосов
/ 21 января 2020

Nginx Синтаксис местоположения регулярного выражения

Выражения регулярного выражения могут использоваться с Nginx секцией блока местоположения, это реализовано с помощью механизма PCRE.

Что делает именно эту функцию поддерживают, так как она не полностью документирована?

1 Ответ

3 голосов
/ 21 января 2020

Nginx location:

Nginx блок блока местоположения имеет порядок поиска, модификатор, тип неявного соответствия и неявное переключение, чтобы остановить поиск по совпадению или нет. следующий массив опишите его для регулярных выражений.


# --------------------------------------------------------------------------------------------------------------------------------------------
# Search-Order       Modifier       Description                                                        Match-Type        Stops-search-on-match
# --------------------------------------------------------------------------------------------------------------------------------------------
#     1st               =           The URI must match the specified pattern exactly                  Simple-string              Yes
#     2nd               ^~          The URI must begin with the specified pattern                     Simple-string              Yes
#     3rd             (None)        The URI must begin with the specified pattern                     Simple-string               No
#     4th               ~           The URI must be a case-sensitive match to the specified Rx      Perl-Compatible-Rx      Yes (first match)                 
#     4th               ~*          The URI must be a case-insensitive match to the specified Rx    Perl-Compatible-Rx      Yes (first match)
#     N/A               @           Defines a named location block.                                   Simple-string              Yes
# --------------------------------------------------------------------------------------------------------------------------------------------

Группа захвата:

Поддерживается группа захвата, оценка выражения (), в этом примере location ~ ^/(?:index|update)$ соответствует URL-адресу, заканчивающемуся example.com / index и example.com / update

# -----------------------------------------------------------------------------------------
#    ()    : Group/Capturing-group, capturing mean match and retain/output/use what matched
#            the patern inside (). the default bracket mode is "capturing group" while (?:) 
#            is a non capturing group. example (?:a|b) match a or b in a non capturing mode
# ----------------------------------------------------------------------------------------- 
#    ?:    : Non capturing group
#    ?=    : Positive look ahead 
#    ?!    : is for negative look ahead (do not match the following...)
#    ?<=   : is for positive look behind
#    ?<!   : is for negative look behind
# -----------------------------------------------------------------------------------------

Вперед sla sh:

Не путать с регулярным выражением sla sh \, In nginx forward sla sh / используется для сопоставления с любым подобласти, включая ни одного примера location /. В контексте поддержки регулярных выражений применяется следующее объяснение

# -----------------------------------------------------------------------------------------
#     /    : It doesn't actually do anything. In Javascript, Perl and some other languages, 
#            it is used as a delimiter character explicitly for regular expressions.
#            Some languages like PHP use it as a delimiter inside a string, 
#            with additional options passed at the end, just like Javascript and Perl.
#            Nginx does not use delimiter, / can be escaped with \/ for code portability 
#            purpose BUT this is not required for nginx / are handled literally 
#            (don't have other meaning than /)
# -----------------------------------------------------------------------------------------

sla sh:

Первая цель специального символа регулярного выражения \ предназначена для экранирования следующего символа; Но обратите внимание, что в большинстве случаев \, за которым следует символ, имеют другое значение, полный список доступен здесь .

Nginx не требует экранирования впереди sla sh / он также не отрицает, что избежал его, как мы могли бы избежать любого другого персонажа. и, таким образом, \/ переводится / соответствует /. Одной из целей экранирования прямой косой черты в контексте nginx может быть переносимость кода.

Другие символы регулярного выражения

Вот неполный список выражений регулярного выражения, которые можно использовать

# -----------------------------------------------------------------------------------------
#     ~     : Enable regex mode for location (in regex ~ mean case-sensitive match)
#     ~*    : case-insensitive match
#     |     : Or
#     ()    : Match group or evaluate the content of ()
#     $     : the expression must be at the end of the evaluated text 
#             (no char/text after the match) $ is usually used at the end of a regex 
#             location expression. 
#     ?     : Check for zero or one occurrence of the previous char ex jpe?g
#     ^~    : The match must be at the beginning of the text, note that nginx will not perform 
#             any further regular expression match even if an other match is available 
#             (check the table above); ^ indicate that the match must be at the start of 
#             the uri text, while ~ indicates a regular expression match mode.
#             example (location ^~ /realestate/.*)
#             Nginx evaluation exactly this as don't check regexp locations if this 
#             location is longest prefix match.
#     =     : Exact match, no sub folders (location = /)
#     ^     : Match the beginning of the text (opposite of $). By itself, ^ is a 
#             shortcut for all paths (since they all have a beginning).
#     .*    : Match zero, one or more occurrence of any char
#     \     : Escape the next char
#     .     : Any char 
#     *     : Match zero, one or more occurrence of the previous char
#     !     : Not (negative look ahead)
#     {}    : Match a specific number of occurrence ex. [0-9]{3} match 342 but not 32
#             {2,4} match length of 2, 3 and 4
#     +     : Match one or more occurrence of the previous char 
#     []    : Match any char inside
# --------------------------------------------------------------------------------------------

Примеры:

location ~ ^/(?:index)\.php(?:$|/)

location ~ ^\/(?:core\/img\/background.png|core\/img\/favicon.ico)(?:$|\/)

location ~ ^/(?:index|core/ajax/update|ocs/v[12]|status|updater/.+|oc[ms]-provider/.+)\.php(?:$|/)

...