Detailed nginx multi domain configuration method

  • 2020-05-12 06:58:48
  • OfStack

preface

Nginx's powerful regular expression support makes the configuration of server_name very flexible. nginx multi-domain configuration is to set up multiple server configurations in the configuration file, and use server_name to filter the domain information in each server configuration.

The implementation method is as follows:

For example, here is an conf file:


server 
{ 
listen 80; 
server_name www.web1.com;       # Binding domain  
index index.htm index.html index.php; # The default file  
root /home/www.web1.com;       # Website root directory 
include location.conf;         # Calls to other rules can also be removed 
}

server 
{ 
listen 80; 
server_name www.web2.com;       # Binding domain  
index index.htm index.html index.php; # The default file  
root /home/www/web2.com;       # Website root directory 
include location.conf;         # Calls to other rules can also be removed 
}

The above configuration information is the most simple multi-domain configuration method in an nginx configuration. The official server_name and nginx also provide many filtering methods of regular matching. For details, please refer to the official nginx documentation.

Matters needing attention

In particular, server_name is invalid when there is only one server configuration in the nginx configuration file. This means that when any domain name is bound to IP, whatever domain name server_name fills in will be matched to the 1-only server. server_name is only valid if there are multiple server.

conclusion

The above is about the nginx multi-domain name configuration of all the content, I hope the content of this article to your study or work can bring 1 definite help, if you have any questions can leave a message to communicate.


Related articles: