Regular Expressions 101

Community Patterns

nginx server_name tokenizer

0

Regular Expression
PCRE (PHP <7.3)

/
^(((((?<feature>feature-[\w-~]+)\.)?((?<isAPI>api)\.)?)|((?<namespace>[\w-~]+)\.)|((?<noMatch>[\w-~\.]+)\.)))?(?<domain>\w+\.\w+)$
/
gm

Description

This regex sets different parts of the url host in variables :

  • $domain
  • $namespace
  • $feature
  • $isAPI
  • $noMatch

In the host name we want to be able to determine the following:

  • Domain: if not on a handled DNS domains, an error 503 'no Service' is returned
  • Subdomain: The subdomain contains parts that will redirect the request upstream or send errors, considering the following.
    • empty subdomain redirects to www namespace with code 301
    • Feature: when the first member of the subdomain is prefixed by feature-, redirects to the proper 'feature' upstream built server feature-*.app:8080
    • API: when the subdomain contains api as last element, it is redirected to the API node server (eventually the one of a dedicated feature) [feature-*|default].api:8081
    • Namespace: any single string subdomain different from the 2 above, redirected upstream to the default staging server default.app:8080.
    • noMatch: all uncatched subdomain pattern results in an error 503 no Service

If no upstream server is found, the nginx proxy will return an error 502 maintenance

Submitted by anonymous - 4 years ago