Nginx Server Names: Exact, Wildcard and Regex Matching
Names are routing rules, not DNS records
The server_name directive declares the names a server block can handle. It does not register a domain or create DNS records. For an ordinary origin-form HTTP/1.1 request, Nginx selects among the server blocks available on the receiving socket using the requested host. Several names can identify the same site.
server_name shop.test www.shop.test;Either name can select this block. Adding an alias does not create an automatic redirect between the names; redirects must be configured separately. Keep DNS and certificate configuration aligned with the names you intend to serve.
Wildcard and regular-expression names
server_name *.docs.test;# In a separate server block:server_name *.team.docs.test;# In another block:server_name mail.*;The leading-star form can cover several labels, so a.b.docs.test can match *.docs.test. It does not cover the bare docs.test. The special form .docs.test combines the bare name and its subdomains. A trailing-star form such as mail.* is supported but broader than an ordinary exact hostname. Stars inside a label such as doc*.test are not valid server-name wildcards.
A tilde introduces a regular expression. The anchors constrain the full name and the escaped dot is literal. This pattern matches demo7.test but not demo.test. Prefer exact names when they suffice; regular expressions need a supported PCRE build and careful review.
Predict which name wins
In one address/port group, an exact match wins over wildcard rules. Among leading-star names, choose the longest matching pattern; only if none matches, consider trailing-star names by longest match. Finally consider matching regex rules in declaration order. If nothing matches, use the listener fallback. This ordering is for server names, not location paths.
docs.test -> docsapi.docs.test -> docs wildcardapi.team.docs.test -> team wildcardmail.docs.test -> docs wildcardmail.test -> mail wildcarddemo42.test -> demoabsent.test -> unknown hostThe fourth example demonstrates that leading-star rules are considered before trailing-star rules. The full Chapter 7 lab is in the package, with fixed expected responses for each case.
Diagnose unexpected routing
curl --noproxy '*' --max-time 2 --resolve docs.test:18087:127.0.0.1 http://docs.test:18087/curl --noproxy '*' --max-time 2 -i http://127.0.0.1:18087/The first command supplies a local address mapping for this client invocation. It does not edit the machine’s DNS configuration. The second uses the literal IP as the host and should receive the explicit fallback 404 in this lab. If two server blocks on the same socket claim the same exact name, Nginx can warn about a conflicting name and ignore one mapping. Review warnings, remove the duplicate and verify the actual responses rather than assuming a syntax pass proves routing.
Clients can choose a Host value. Applications must still enforce identity, access policy and accepted hostnames. A virtual host is not a security boundary by itself.
Practice
Use the manual selection exercise to resolve a new set of overlapping hostnames. Next, learn why HTTPS sometimes chooses a certificate before HTTP routing begins.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Hostname matching
Apply the lesson to original routing scenarios.
Given docs.test and *.docs.test, which name wins for docs.test?
Both *.docs.test and *.team.docs.test match api.team.docs.test. Which wins?
Which rule wins for mail.docs.test: mail.* or *.docs.test?
Which name matches ~^demo[0-9]+\.test$?
Does declaring server_name shop.test create a DNS record?