Sign up ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I have multiple "server" blocks in my nginx.conf. The documentation seems wrong about the order in which server_name matching takes places.

My conf looks something like:

server {
  listen 80
  server_name *

  # do stuff

server {
  listen 80
  server_name *

  # do something else

I'm trying to figure out why requests with host = are going to the correct place (the second server), when the documentation indicates it should be going to the first server.

Nginx documentation indicates that the server blocks are checked "in order" for matches, meaning any request with the host * should be caught by the first server, not the second server. (

Also, the * wildcard matches any number of subdomain parts, so * matches (

If this documentation is wrong, what is the actual matching order?

share|improve this question

2 Answers 2

From the nginx documentation (

When searching for a virtual server by name, if name matches more than one of the specified variants, e.g. both wildcard name and regular expression match, the first matching variant will be chosen, in the following order of precedence:

  1. exact name
  2. longest wildcard name starting with an asterisk, e.g. “*”
  3. longest wildcard name ending with an asterisk, e.g. “mail.*”
  4. first matching regular expression (in order of appearance in a configuration file)
share|improve this answer

The documentation says:

Compares the Host header of the incoming HTTP request against the server { ... } blocks in the Nginx configuration files and selects the first one that matches.

What is seeming to happen is that it chooses the best match from all the server blocks, so, matching the second config. I have no documentation to back that up, so you may run some tests to confirm that behavior. There is also this doc talking about name resolution.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.