When we get incoming request with test.com “Host” header – first server block will be used. Same block will be selected for test.org, since it does not match any of the specified hostnames. In that case Nginx selects first server block that matches listen directive (port 80 in our case). Nginx first matches against listen entry and only after that against server_name.

Here is the code to match any request with any Host header on specific port:

* will match any request to 8888 port. Sometimes useful for locations where u access resource by ip.

There is another interesting case – sometimes you want to specify a server block that will be used for all requests that doesn’t contain Host header (bots, crawlers, your own special clients or you just want to drop them):

server {
listen 80;
server_name "";
return 400;
}

* will be matched for requests without Host header and return 400 Bad request error.

Nginx server_name match order

It’s important to know the order in which Nginx matches “Host” header against server_name entry.

Longest left side wildcard like “*.test.com” ( yes, you can use wildcards in your server_name directive, but be careful because you can only use it before the first dot, or after the last one. If you need to match something in the middle – use regular expressions. )

Longest right side wildcard like “test.*”

First match against regular expression ( Nginx evaluates directives top to bottom, so the topmost matching entry wins )

* Important! Please note ~ (tilde) sign at the beginning of the expression. I’ve seen too many people who accidentally skipped it and later started pulling hairs out trying to figure out why it didn’t work.. One other interesting thing here is (?<subdomain>.+).

As you probably already guessed that’s a named capture. Here we capture subdomain part and Nginx conveniently converts it into $subdomain variable which we then use in the location entry to switch the root. This is pretty powerful stuff that allows us to create dynamic configurations like giving each developer personal directory on the server for example.

Performance considerations

For frequently requested names it’s better to use direct matches, as those are the fastest and most efficient to execute. Regular expressions are the slowest option. I know that we like to create short and complex solutions, but in this case it makes sense to explicitly define commonly used names: server_name test.com www.test.com *.test.com;

If you have a ton of names in your configuration, you may want to adjust server_names_hash_max_size (set it to the number close to the number of server names).

I helped to build and maintain the infrastructure for Game of Thrones, the biggest and most popular show in the world.

Do you want to know the single most important thing that I learned over the years?

NONE OF IT REALLY MATTERED…

Yes, it was fun for a while.

Yes, like most of us engineers I was making good money.

But at the end of the day, I would still have to show up at work and sell my time.

Sometimes I would come in, sit in my cubicle and dream about things I could do instead of staring at the screen all day long…

I could go to the beach with my wife and my son.

I could fly to El Classico game in Barcelona with my brother and watch Messi scoring amazing goals.

I could organize a surfing trip to South Africa and other awesome places around the world. Places I’ve never seen.

I could work on my own projects that would make the impact in the world or at the very least, make me some money.

Hell, I could just sit home and do absolutely nothing!

And yet there I was still in my cubicle 12 years later with big hopes and dreams and pretty much nothing to show for…

Sounds familiar?

The tipping point for me was when I started buying games on Steam and GoG and playing them in my mind.

Nothing to install, no need to upgrade video cards, no need to feel bad in front of my wife, no time to waste…

You are right, I was spiraling down and needed a break, but more so I felt like I needed some radical changes in my life.

I’m sure you heard this saying before: “Insanity: doing the same thing over and over again and expecting different results”

It became clear that the road I was walking on would lead me to mediocre life.

The problem was that I didn’t want to be mediocre. I wanted my life to be awesome, full of fun, happiness and excitement!

I wanted to make a difference in the world, leave a legacy, make my kids proud, live without regrets, discover my true purpose.

So about a year ago, I set out on my new journey…

I left my old comfortable job, attended multiple high profile non-technical events (including Tony Robbins UPW), joined an expensive business program, hired a personal coach and mentor, met a bunch of people who were able to disconnect from the Matrix and never looked back.

And let me tell you – there is another world out there, something we technical guys don’t get to experience!

There is hope.

Now, here is my question for you:

Do you want to continue to be just a tool in someone else’s hands or you want to upgrade yourself and become a Rain Maker?

If you want to find out who you really are, take full control of your life, step outside your comfort zone in order to grow physically, mentally and financially and help others along the way, then the Red pill is for you. Just drop your email in the field below and we’ll be in touch.

Take a Blue pill and you will forget that we ever met. You will close this popup and continue reading articles about Nginx, Kubernetes, Docker, secretly dreaming of life that you could have… (or pathetically thinking that you will have it one day just by perfecting technical skills)