Bird
Raised Fist0
Nginxdevops

Wildcard and regex server names in Nginx - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is a wildcard server name in nginx?
A wildcard server name uses an asterisk (*) to match multiple subdomains or domain patterns. For example, *.example.com matches app.example.com, blog.example.com, etc.
Click to reveal answer
intermediate
How do you define a regex server name in nginx?
You define a regex server name by prefixing the server name with a tilde (~). For example, server_name ~^www\d+\.example\.com$; matches www1.example.com, www123.example.com, etc.
Click to reveal answer
intermediate
What is the difference between a wildcard and a regex server name in nginx?
A wildcard uses simple patterns with an asterisk (*) to match subdomains, while regex uses full regular expressions for more flexible and complex matching.
Click to reveal answer
advanced
How does nginx prioritize server names when multiple match?
Nginx prioritizes exact server names first, then wildcard names starting with an asterisk, then wildcard names ending with an asterisk, and finally regex server names.
Click to reveal answer
advanced
Write an nginx server_name directive to match any subdomain of example.com except 'www'.
Use a regex to exclude 'www':
server_name ~^(?!www\.)[^.]+\.example\.com$;
This matches any subdomain except 'www.example.com'.
Click to reveal answer
Which nginx server_name matches all subdomains of example.com?
A~^www\d+\.example\.com$
B~^example\.com$
Cexample.*
D*.example.com
How do you indicate a regex server name in nginx?
APrefix with a tilde (~)
BUse an asterisk (*)
CUse square brackets []
DUse curly braces {}
Which server_name has the highest priority in nginx?
AWildcard server names starting with *
BExact server names
CRegex server names
DWildcard server names ending with *
What does the server_name directive ~^www\d+\.example\.com$ match?
Awww followed by one or more digits, like www1.example.com
BAny subdomain of example.com
COnly www.example.com
DAll domains ending with example.com
Which of these is NOT a valid wildcard server_name in nginx?
A*.example.com
Bwww.*
C*example.com
D~^.*\.example\.com$
Explain how to use wildcard and regex server names in nginx and when to choose each.
Think about simple subdomain matching vs complex patterns.
You got /4 concepts.
    Describe nginx server_name matching priority and why it matters.
    Consider what happens if multiple server names match a request.
    You got /3 concepts.

      Practice

      (1/5)
      1. What does a wildcard server name like *.example.com match in nginx?
      easy
      A. Any domain ending with example.com, including example.com itself
      B. Only the exact domain example.com
      C. Any subdomain of example.com, like blog.example.com or shop.example.com
      D. Only domains starting with a star character

      Solution

      1. Step 1: Understand wildcard usage in server names

        A wildcard * before a domain matches any subdomain part, but not the root domain itself.
      2. Step 2: Apply to *.example.com

        This matches domains like blog.example.com or shop.example.com, but not example.com alone.
      3. Final Answer:

        Any subdomain of example.com, like blog.example.com or shop.example.com -> Option C
      4. Quick Check:

        Wildcard * matches subdomains only [OK]
      Hint: Wildcard * matches subdomains, not the root domain [OK]
      Common Mistakes:
      • Thinking wildcard matches the root domain
      • Confusing wildcard with regex syntax
      • Assuming wildcard matches any domain ending
      2. Which of the following is the correct syntax to define a regex server name in nginx?
      easy
      A. server_name /www\d+\.example\.com/;
      B. server_name *example.com;
      C. server_name ^www\d+\.example\.com$;
      D. server_name ~^www\d+\.example\.com$;

      Solution

      1. Step 1: Recall regex server name syntax in nginx

        Regex server names start with a tilde ~ followed by the regex pattern.
      2. Step 2: Check each option

        server_name ~^www\d+\.example\.com$; uses ~^www\d+\.example\.com$ which is correct regex syntax. Others either miss the tilde or use invalid delimiters.
      3. Final Answer:

        server_name ~^www\d+\.example\.com$; -> Option D
      4. Quick Check:

        Regex server names start with ~ [OK]
      Hint: Regex server names always start with ~ in nginx [OK]
      Common Mistakes:
      • Omitting the ~ before regex pattern
      • Using * instead of ~ for regex
      • Using slashes / / as regex delimiters
      3. Given this nginx config snippet:
      server {
        server_name ~^www\d+\.example\.com$;
        return 200 'Matched regex server name';
      }

      What will be the response for a request to www12.example.com?
      medium
      A. Matched regex server name
      B. 500 Internal Server Error
      C. 404 Not Found
      D. Default nginx welcome page

      Solution

      1. Step 1: Understand the regex pattern

        The regex ^www\d+\.example\.com$ matches domains starting with 'www' followed by one or more digits, then '.example.com'.
      2. Step 2: Check if 'www12.example.com' matches

        'www12.example.com' matches the pattern because '12' are digits after 'www'.
      3. Final Answer:

        Matched regex server name -> Option A
      4. Quick Check:

        Regex matches www12.example.com [OK]
      Hint: Check if domain fits regex pattern exactly [OK]
      Common Mistakes:
      • Assuming regex matches partial strings
      • Ignoring start ^ and end $ anchors
      • Confusing regex with wildcard matching
      4. You wrote this nginx config:
      server {
        server_name ~www\d+\.example\.com$;
        return 200 'Hello';
      }

      Why is this regex server_name directive incorrect?
      medium
      A. Missing ^ anchor at start of regex
      B. Regex server name must start with ~ and pattern must start with ^
      C. Regex pattern missing delimiters like / /
      D. Regex server name must start with ~ and pattern must be valid

      Solution

      1. Step 1: Check regex server_name syntax

        Regex server names must start with ~ and the pattern should start with ^ to match from the beginning.
      2. Step 2: Identify missing ^ anchor

        The pattern www\d+\.example\.com$ lacks the ^ anchor, causing partial matches rather than full hostname matches.
      3. Final Answer:

        Regex server name must start with ~ and pattern must start with ^ -> Option B
      4. Quick Check:

        Regex patterns need ^ anchor at start [OK]
      Hint: Regex patterns should start with ^ for nginx server_name [OK]
      Common Mistakes:
      • Omitting ^ anchor in regex pattern
      • Thinking delimiters like / / are needed
      • Ignoring nginx error messages about regex
      5. You want nginx to serve requests for any subdomain of example.com except www.example.com. Which server_name config achieves this?
      hard
      A. server_name ~^(?!www\.)[^.]+\.example\.com$;
      B. server_name *.example.com www.example.com;
      C. server_name ~^www\d+\.example\.com$;
      D. server_name example.com *.example.com;

      Solution

      1. Step 1: Understand the requirement

        We want to match any subdomain except 'www.example.com'. This requires a regex with negative lookahead.
      2. Step 2: Analyze options

        server_name ~^(?!www\.)[^.]+\.example\.com$; uses ~^(?!www\.)[^.]+\.example\.com$ which matches any subdomain not starting with 'www.'. Others either include 'www' or don't exclude it.
      3. Final Answer:

        server_name ~^(?!www\.)[^.]+\.example\.com$; -> Option A
      4. Quick Check:

        Negative lookahead excludes www subdomain [OK]
      Hint: Use negative lookahead in regex to exclude specific subdomains [OK]
      Common Mistakes:
      • Using wildcard that includes www.example.com
      • Not using regex for exclusion
      • Confusing wildcard and regex syntax
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard