How to use regular expressions to automatically match wildcard domain names in nginx

How to use regular expressions to automatically match wildcard domain names in nginx

Nginx uses regular expressions to automatically match wildcard domain names to directories

Nginx can be used to realize the function of virtual host and bind multiple URLs.
Usually the primary domain name is hard-coded in the configuration, so if you want to add a match, you need to add it manually.

Basic writing

The most basic way to write it is server{
listen 80;
server_name www.a.com;
root /html/a;
…
}

server{
listen 80;
server_name www.b.com;
root /html/b;
…
}

Regular expression

After the domain name is bound, it is automatically matched to the corresponding directory, and there is no need to write configurations for each domain name one by one.
The matching directory is: html/domain name/secondary domain name, for example, www.baidu.com will match the baidu.com/www directory

server {
 listen 80;
 # $subdomain will match the second-level domain name and above # $domain will match the first-level domain name # $tld will match the root node name server_name ~^((?<subdomain>.*)\.)?(?<domain>[^.]+)\.(?<tld>[a-zA-Z]+)$;

 #If there is no $subdomain, jump to www
 if ($subdomain = "") {
  # set $subdomain "_";
  set $subdomain "www";
  return 301 $scheme://www.$domain.$tld$request_uri;
 }

 root /usr/share/nginx/html/${domain}.${tld}/${subdomain};
}

other

There are many articles about nginx's matching order, so I won't go into details.
Because regular expressions have the lowest matching priority, they are only matched when no other matches are found, so there is no need to worry about affecting other exact matches.

Most people don’t have so many domain names to match. Instead, they want to block some malicious domain name pointers.

However, when testing, in order not to affect the running web page, you can add an IP domain name record in the hosts and use a non-existent domain name for testing, which is more useful at this time.

Summarize

This is the end of this article about how nginx uses regular expressions to automatically match wildcard domain names to directories. For more information about how nginx uses regular expressions to match wildcard domain names to directories, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Nginx if statement plus regular expression to achieve string truncation
  • Detailed explanation of Nginx regular expressions
  • Detailed explanation of nginx location configuration regular expression example
  • Nginx pseudo-static Rewrite regular resource summary
  • Nginx rewrite regular matching rewriting method example
  • nginx configuration location summary location regular writing and rewrite rule writing
  • Python regular analysis of nginx access log
  • How to use nginx to intercept specified URL requests through regular expressions
  • Introduction to Nginx regular expression related parameters and rules

<<:  MySQL optimization tips: analysis of duplicate removal implementation methods [millions of data]

>>:  JS implements city list effect based on VUE component

Recommend

HTML uses the title attribute to display text when the mouse hovers

Copy code The code is as follows: <a href=# ti...

How to solve the margin collapse problem in CSS

First, let's look at three situations where m...

MySQL column to row conversion, method of merging fields (must read)

Data Sheet: Column to row: using max(case when th...

The difference between key and index in MySQL

Let's look at the code first: ALTER TABLE rep...

Detailed explanation of JSONObject usage

JSONObject is just a data structure, which can be...

Two usages of iFrame tags in HTML

I have been working on a project recently - Budou...

Nginx defines domain name access method

I'm building Nginx recently, but I can't ...

How to use gdb to debug core files in Linux

1.core file When a Segmentation fault (core dumpe...

Tips and precautions for using MySQL index

1. The role of index In general application syste...

How to hide and forge version number in Nginx

1. Use curl command to access by default: # curl ...

Detailed explanation of Angular data binding and its implementation

Table of contents Preface What is data binding? T...

How to solve the Mysql transaction operation failure

How to solve the Mysql transaction operation fail...