Nginx Server_name Wildcard or Catch-all

Better Stack Team
Updated on October 7, 2024

In Nginx, you can configure server_name directives to handle requests for different domains using wildcard or catch-all patterns. This allows you to handle various subdomains or domains with a single server block. Here’s how you can configure wildcard and catch-all server names in Nginx:

1. Wildcard Subdomains

Wildcard subdomains match any subdomain under a given domain. For example, *.example.com will match www.example.com, blog.example.com, shop.example.com, etc.

Example Configuration:

 
server {
    listen 80;
    server_name *.example.com;

    root /var/www/html;
    index index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }
}

Explanation:

  • server_name *.example.com;: Matches all subdomains of example.com.

2. Catch-All Domain

A catch-all server block matches any domain or subdomain not explicitly defined in other server blocks. This is useful for handling unknown or wildcard domains.

Example Configuration:

 
server {
    listen 80 default_server;
    server_name _;  # Matches any server name

    root /var/www/html;
    index index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }
}

Explanation:

  • listen 80 default_server;: Designates this server block as the default for unmatched domains.
  • server_name _;: A catch-all pattern that matches any domain not specified in other server_name directives.

3. Specific and Wildcard Combination

You can combine specific domain names with wildcards to handle both known and unknown domains.

Example Configuration:

 
server {
    listen 80;
    server_name www.example.com example.com;

    root /var/www/html;
    index index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }
}

server {
    listen 80;
    server_name *.example.com;

    root /var/www/html/subdomains;
    index index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }
}

server {
    listen 80 default_server;
    server_name _;

    root /var/www/html/default;
    index index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }
}

Explanation:

  • First Block: Handles requests for www.example.com and example.com.
  • Second Block: Handles all subdomains of example.com.
  • Third Block: Acts as a catch-all for any other domains.

4. Example: Handling HTTPS

If you’re using SSL, configure both HTTP and HTTPS:

HTTP to HTTPS Redirect and Wildcard Handling:

 
# HTTP to HTTPS redirect
server {
    listen 80;
    server_name *.example.com example.com;

    return 301 https://$host$request_uri;
}

# HTTPS handling for specific and wildcard domains
server {
    listen 443 ssl;
    server_name *.example.com example.com;

    ssl_certificate /etc/nginx/ssl/example.com.crt;
    ssl_certificate_key /etc/nginx/ssl/example.com.key;

    root /var/www/html;
    index index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }
}

# Default server block for unmatched domains
server {
    listen 443 ssl default_server;
    server_name _;

    ssl_certificate /etc/nginx/ssl/default.crt;
    ssl_certificate_key /etc/nginx/ssl/default.key;

    root /var/www/html/default;
    index index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }
}

Explanation:

  • HTTP Block: Redirects all HTTP requests to HTTPS.
  • HTTPS Blocks: Handles both specific and wildcard domains with SSL.
  • Default HTTPS Block: Catches all other domains with default SSL certificates.

5. Testing and Validating Configuration

After configuring Nginx, test the configuration and reload Nginx to apply the changes:

Test Configuration:

 
sudo nginx -t

Reload Nginx:

 
sudo systemctl reload nginx

Summary

  • Wildcard Subdomains: Use .example.com to match all subdomains.
  • Catch-All: Use server_name _; with listen 80 default_server; for unmatched domains.
  • Combination: Combine specific names with wildcards for flexible handling.
  • HTTPS: Configure HTTP to HTTPS redirects and handle SSL for specific and catch-all domains.

By properly configuring server_name with wildcard and catch-all patterns, you can effectively manage multiple domains and subdomains in Nginx.

Got an article suggestion? Let us know
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github