How to use wildcards or regular expressions in server_name?
When using server_name in Nginx to match multiple domains or subdomains, it can be achieved through wildcards and regular expressions. 1. When using wildcards, the asterisk can only be used for the beginning or ending, and must be a complete label boundary. For example, .example.com can match first-level subdomains but does not include root domains or multi-level subdomains. If you need to match both root domains and first-level subdomains, it should be written as example.com *.example.com; 2. When using regular expressions, you must start with ~, such as ~^\w .(dev|test)$ can match domain names ending with .dev or .test, and support capture group calls; 3. The matching priority is the exact name > Longest wildcard prefix > Longest wildcard suffix > Regular expressions, and the default case is insensitive. It is recommended to use wildcards for simple matching, and use regulars for complex scenarios. After modifying configuration, the service should be tested and overloaded.
When using server_name
in Nginx configuration, if you want to match multiple domains or subdomains, you can simplify the configuration with wildcards and regular expressions. This approach is particularly suitable for scenarios where a large number of dynamic subdomains or pan-domains are required.
Here are some practical usages and suggestions.
Use wildcards to match subdomains
Nginx supports the use of asterisks *
for wildcard matching, but it should be noted that it can only be used for the beginning or end, and must be a complete label boundary.
For example:
server { listen 80; server_name *.example.com; # Processing logic}
This configuration will match a.example.com
, b.example.com
, but will not match example.com
or abexample.com
.
If you want to match both the root domain and any first-level subdomain, you can write it like this:
server_name example.com *.example.com;
Use regular expressions for more flexible matching
Regular expressions can be used when there are insufficient wildcards. When using regular in server_name
, you need to use a wavy line ~
to indicate that the regular mode is turned on.
For example, you want to match all domain names ending with .dev
or .test
:
server { listen 80; server_name ~^\w \.(dev|test)$; # Processing logic}
In this example:
-
^
means the beginning -
\w
Match one or more word characters (letters, numbers, underscores) -
\.
point number -
(dev|test)
means dev or test -
$
means end
Note: Capture groups in regular expressions can be called via variables in subsequent configurations, such as $1
, $2
, etc.
Frequently Asked Questions and Precautions
- Priority issue : Nginx matches
server_name
in the order in which the exact name > longest wildcard prefix > longest wildcard suffix > regular expressions. So if you have multipleserver
blocks, be careful about the priority between them. - Case is insensitive by default : Nginx is insensitive to domain names by default, which means that
Example.com
andexample.com
are equivalent. - Don't abuse the regulative : Although the regulative is powerful, if it is just a simple match, it is clearer and more efficient to use wildcards.
- Testing the configuration is very important : after modifying the configuration, be sure to run
nginx -t
to check whether the syntax is correct, and then the overloading service takes effect.
Basically that's it. Mastering the use of wildcards and regular expressions can make you more comfortable when configuring Nginx, especially when facing complex scenarios such as multi-tenant and multi-subdomains.
The above is the detailed content of How to use wildcards or regular expressions in server_name?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Oracle wildcard characters include "%", "_", "[]" and "[^]". Detailed introduction: 1. The wildcard character "%" means matching any character, including zero characters. Using the wildcard character "%" in Oracle can implement fuzzy query. When the wildcard character "%" is used in the query statement, the query will return all characters matching the specified character. Pattern matching string; 2. The wildcard character "_" means matching any single character. In Oracle, the wildcard character "_" can be used to achieve exact matching. When using wildcard characters in query statements, etc.

Regular expression wildcards include ".", "*", "+", "?", "^", "$", "[]", "[^]", "[a-z]", "[A-Z] ","[0-9]","\d","\D","\w","\W","\s&quo

The wildcard character "*" can represent any string and can match zero, single or multiple characters; while the wildcard character "?" only represents a single string, and this word must exist. Wildcard is a special statement, mainly including asterisk "*" and question mark "?", used to fuzzy search files; the number of matches for "*" is not limited, while the number of matching characters for "?" is limited. When looking for a folder, you can use it to replace one or more real characters; when you don't know the real characters or you are too lazy to enter the full name, you often use wildcards to replace one or more real characters.

The wildcards * and ? have some differences in usage and matching range. Specific differences: 1. In terms of matching range, the wildcard * can match any length of character sequence, including letters, numbers, punctuation marks, spaces, etc., while the wildcard ? can only match one character; 2. In terms of usage, the wildcard * is used Fuzzy matching can match multiple characters or character sequences. The wildcard character ? is used for exact matching and can only match one character.

Word wildcard characters include "?", "*", "[]", "!", "%", etc. Detailed introduction: 1. Question mark (?): indicates matching any single character. For example, "c?t" can match words such as "cat" and "cut"; 2. Asterisk (*): indicates matching zero or more characters. For example, "colr" can match words such as "color" and "colour"; 3. Square brackets ([]): means matching any character within the square brackets. For example, "[aeiou]" can match any vowel; 4. exclamation point, etc.

Learn from scratch: Master wildcard characters in Linux commands In Linux systems, wildcard characters are special characters used to match file names. Mastering these wildcards can help us locate and operate files on the command line more efficiently. This article will focus on several commonly used Linux wildcard characters, with specific code examples, hoping to help beginners better understand and use these wildcard characters. 1. Asterisk (*) The asterisk wildcard represents zero or more arbitrary characters and can match any long string. Here is an example: ls

In Java, generic wildcards allow generic types to be represented without specifying concrete types. Upper limit wildcard (

To master common CSS selector wildcard examples, specific code examples are required. CSS selectors are a very important part of web development. They allow us to select and style HTML elements based on different element attributes. Among CSS selectors, wildcards are a very useful selector that can match any type of HTML element. In this article, we will introduce commonly used CSS wildcards and provide specific code examples. Wildcard (*) The wildcard "*" represents selecting all HTML elements. It can be used to set all
