0
0
Nginxdevops~5 mins

Wildcard and regex server names in Nginx - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you want your web server to respond to many similar website names without listing each one, you can use wildcard or regex patterns. This helps handle groups of websites easily with fewer settings.
When you want to serve all subdomains of example.com like blog.example.com and shop.example.com with one configuration.
When you want to match any domain that starts with www and ends with .net, like www1.net or www-shop.net.
When you want to block or redirect requests from domains matching a pattern, such as all domains ending with .test.
When you want to catch all requests that do not match any specific server name and show a default page.
When you want to simplify configuration by using patterns instead of listing many similar domain names.
Config File - nginx.conf
nginx.conf
http {
    server {
        listen 80;

        # Wildcard server name to match all subdomains of example.com
        server_name *.example.com;

        location / {
            return 200 'Hello from wildcard subdomain!';
        }
    }

    server {
        listen 80;

        # Regex server name to match domains starting with www and ending with .net
        server_name ~^www.*\.net$;

        location / {
            return 200 'Hello from regex matched domain!';
        }
    }

    server {
        listen 80 default_server;

        # Default server for unmatched requests
        server_name _;

        location / {
            return 200 'Default server response';
        }
    }
}

This configuration has three servers:

  • The first uses a wildcard *.example.com to match any subdomain of example.com.
  • The second uses a regex ~^www.*\.net$ to match domains starting with 'www' and ending with '.net'.
  • The third is the default server that catches all other requests not matched by the above.

This setup shows how to use wildcard and regex server names in nginx to handle many domains efficiently.

Commands
Check the nginx configuration file syntax to make sure there are no errors before restarting the server.
Terminal
nginx -t
Expected OutputExpected
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Reload nginx to apply the new configuration without stopping the server.
Terminal
systemctl reload nginx
Expected OutputExpected
No output (command runs silently)
Send a request to nginx with the Host header set to blog.example.com to test the wildcard server name response.
Terminal
curl -s -H 'Host: blog.example.com' http://localhost
Expected OutputExpected
Hello from wildcard subdomain!
Send a request with a Host header matching the regex pattern to test the regex server name response.
Terminal
curl -s -H 'Host: www123.net' http://localhost
Expected OutputExpected
Hello from regex matched domain!
Send a request with a Host header that does not match any server name to test the default server response.
Terminal
curl -s -H 'Host: unknown.domain' http://localhost
Expected OutputExpected
Default server response
Key Concept

If you remember nothing else from this pattern, remember: nginx matches server names in order, using exact names first, then wildcards, then regex, so order and pattern type matter.

Common Mistakes
Using wildcard server names with a leading dot like .example.com instead of *.example.com
nginx does not recognize leading dot as a wildcard and will not match subdomains correctly.
Use an asterisk before the domain like *.example.com to match all subdomains.
Placing regex server names before exact or wildcard server names in the configuration
nginx processes server names in order and regex server names have lower priority, so placing them first can cause unexpected matches.
Place exact and wildcard server names before regex server names in the configuration.
Not escaping dots in regex server names, e.g., using ~^www.*.net$ instead of ~^www.*\.net$
Dots in regex match any character, so the pattern will match unintended domains.
Escape dots with double backslashes like \. to match literal dots.
Summary
Use *.example.com to match all subdomains of example.com with a wildcard server name.
Use ~^pattern$ syntax to define regex server names for flexible domain matching.
Always test nginx configuration with nginx -t before reloading to avoid errors.
Order server blocks carefully: exact names first, then wildcards, then regex.
Use a default server block to catch unmatched requests and avoid errors.