Detailed explanation of nginx's default_server definition and matching rules

Detailed explanation of nginx's default_server definition and matching rules

The default_server directive of nginx can define a default server to handle some requests that do not match the server_name. If it is not explicitly defined, the first defined server will be selected as the default_server.

After understanding the above rules, we can capture unbound domain name access or direct IP access, and redirect them to 403 pages.

1. nginx implicit default server

http {
  # If no default server is explicitly declared, the first server will be implicitly set as the default server
  server {
    listen 80;
    server_name _; # _ is not the point __ It is also OK ___ You can also return 403; # 403 forbidden
  }
  
  server {
    listen 80;
    server_name www.a.com;
    ...
  }
  
  server {
    listen 80;
    server_name www.b.com;
    ...
  }
}

Many people copy and paste and spread the idea that server_name should be set to '_', but it actually has nothing to do with it. '_' is just a request recovery service that has nothing to do with business domain names. If our online businesses are all accessed by clear business domain names, some non-business domain name or IP access caused by wildcard resolution will be recycled by this server.

When the default server is not explicitly defined, nginx will use the first configured server as the default server, that is, when the request does not match any server_name, this server will handle the request. Therefore, when we access directly using the IP address, it will be handed over to the first server defined here for processing, 403 forbidden.

2. Define a default server explicitly

http {
  server {
    listen 80;
    server_name www.a.com;
    ...
  }
  
  server {
    listen 80;
    server_name www.b.com;
    ...
  }
  
  # Display the definition of a default server
  server {
    listen 80 default_server;
    server_name _;
    return 403; # 403 forbidden
  }
  
}

It is recommended to explicitly specify the default server, because when we configure virtual hosts or multiple services, there will be multiple server configuration files. If we use implicit method to select the first loaded server as the default server, we have to always confirm who is loaded first... creating risks...

3. Directly specify server_name as ip (can only prohibit ip access)

http {
  server {
    listen 80;
    server_name www.a.com;
    ...
  }
  
  server {
    listen 80;
    server_name www.b.com;
    ...
  }
  
  # Directly specify ip server_name
  server {
    listen 80;
    server_name xxx.xxx.xxx.xxx;
    return 403; # 403 forbidden
  }
  
}

All three methods above can prohibit direct access by IP, and 1 and 2 can also prohibit access by unbound domain names (for example, if you have resolved the primary domain name).

Note: When nginx batch loads configuration conf, it will load them in ascii order, which will be loaded in the order of server_a.conf server_b.conf server_c.conf. If default_server is not defined, server_a will be used as the default server to handle requests that are not bound to a domain name/IP.

This is the end of this article about the detailed explanation of nginx's default_server definition and matching rules. For more relevant nginx default_server matching rules, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Implementation of Nginx forwarding matching rules
  • Nginx configuration location matching rules example explanation
  • Detailed analysis of matching rules when Nginx processes requests
  • Nginx Location directive URI matching rules detailed summary
  • Nginx location matching rule example
  • A brief summary of nginx matching rules (recommended)
  • Detailed explanation of Nginx location matching rules
  • Detailed explanation of the location directive matching rules of the Nginx server
  • Introduction to location matching rules in Nginx

<<:  MySQL Installer 8.0.21 installation tutorial with pictures and text

>>:  Basic syntax of MySQL index

Recommend

Trash-Cli: Command-line Recycle Bin Tool on Linux

I believe everyone is familiar with the trashcan,...

Example test MySQL enum type

When developing a project, you will often encount...

Several methods to clear floating (recommended)

1. Add an empty element of the same type, and the...

Jenkins builds Docker images and pushes them to Harbor warehouse

Table of contents Dockerfile pom.xml Jenkins Conf...

Detailed explanation of MySQL clustered index and non-clustered index

1. Clustered Index Table data is stored in the or...

Nginx defines domain name access method

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

VPS builds offline download server (post-network disk era)

motivation Due to learning needs, I purchased a v...

Example code for implementing complex table headers in html table

Use HTML to create complex tables. Complex tables...

Implementation of CSS border length control function

In the past, when I needed the border length to b...

MySQL Daemon failed to start error solution

MySQL Daemon failed to start error solution A few...

CSS Back to Top Code Example

Most websites nowadays have long pages, some are ...

Detailed steps for configuring Tomcat server in IDEA 2020

The steps for configuring Tomcat in IDEA 2020 are...

Analysis of SQL integrity constraint statements in database

Integrity constraints Integrity constraints are f...

Two ways to connect WeChat mini program to Tencent Maps

I've been writing a WeChat applet recently an...