Nginx Server Names

Beginner
5 min read
Updated: Nov 2026
3 examples
server_name

Definition

A server_name list tells Nginx which server block should handle a request for a given Host header. Exact names, wildcards, and regex names are all allowed.

Match order: exact name, then longest leftmost wildcard (*.example.org), then longest rightmost wildcard (mail.*), then the first matching regex.

Config

Start with this shape. Adjust paths for your server.

Nginx
server {
    listen 80;
    server_name example.org www.example.org;
    root /sites/example;
}

server {
    listen 80;
    server_name example.net www.example.net;
    root /sites/net;
}

If no name matches, Nginx uses the default server for that listen port — usually the first server, or the one marked default_server.

Key Directives

NamePlain meaning
server_nameHost names this server block answers.
default_serverlisten parameter that marks the fallback server for a port.
*.example.orgWildcard matching one or more labels before example.org.
~^www\d+\.example\.net$Regex name — must start with ~.

Examples

Example 1 — Exact Names

List every hostname you want this site to answer.

Nginx
server {
    listen 80;
    server_name example.org www.example.org;
    root /var/www/example;
}

How It Works

  1. Host: www.example.org selects this block.
  2. Exact names are the fastest match type.
  3. Prefer listing www and apex when both are used.

Example 2 — Wildcard and Catch-all Style Names

*.example.org matches www and deeper labels. A lone _ is often used as a dummy name on a default server.

Nginx
server {
    listen 80;
    server_name *.example.org;
}

server {
    listen 80 default_server;
    server_name _;
    return 444;
}

How It Works

  1. *.example.org matches www.example.org and a.b.example.org.
  2. _ is just an invalid hostname — not magic.
  3. default_server on listen sets the fallback, not server_name.

Example 3 — Regex Server Name

Start the name with ~ and escape dots. Use anchors.

Nginx
server {
    listen 80;
    server_name ~^www\d+\.example\.net$;
    # matches www1.example.net, www42.example.net, ...
}

How It Works

  1. ~ marks a PCRE pattern.
  2. Quote the pattern if it contains { } braces.
  3. Regex is slowest — prefer exact names when you can.

Frequently Asked Questions

Nginx uses the default server for that IP:port.
Add default_server to the listen directive of the server you want as fallback.
No. It is a common invalid name so it never collides with a real domain.
Yes. List them in one server_name directive.
Use the Punycode (ASCII) form in server_name.

Next: Request processing

Keep going through the beginner Nginx path.

Request processing →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful