When you bind multiple domain names to nginx, you can write multiple domain name rules into one configuration file. You can also create multiple domain name configuration files for ease of management, each domain name creates a file, and some similar domain names can also be written in a general configuration file.
1. One file for each domain name
First, open the directory where the nginx domain name configuration file is stored:/usr/local/nginx/conf/servers. To bind a domain name www.111cn.net, create a file: www.111cn.net. conf and then write rules in this file, such:
The code is as follows: |
Copy code |
Server { Listen 80; Server_name www.111cn.net; # bind a domain name Index index.htm index.html index. php; # default file Root/home/www/111cn.net; # Website root directory Include location. conf; # call other rules, which can also be removed } |
Then restart the nginx server, and the domain name is successfully bound to the nginx server. restart the command:/etc/init. d/nginx restart
2. Writing multiple domain names in one file
The same applies to rules for adding multiple domain names to a file. You only need to write down the previous single domain name repeatedly, for example, server.
The code is as follows: |
Copy code |
{ Listen 80; Server_name www.111cn.net; # bind a domain name Index index.htm index.html index. php; # default file Root/home/www/111cn.net; # Website root directory Include location. conf; # call other rules, which can also be removed } Server { Listen 80; Server_name msn.111cn.net; # bind a domain name Index index.htm index.html index. php; # default file Root/home/www/mb.111cn.net; # Website root directory Include location. conf; # call other rules, which can also be removed } |
3. Add 301 redirection for domain names without www
If you want to add a 301 jump to a domain name without www, it is the same as binding a domain name. First, bind a domain name without www. Instead of writing a website directory, you can perform a 301 jump, for example:
The code is as follows: |
Copy code |
Server { Listen 80; Server_name 111cn.net; Rewrite ^/(. *) http://www.111cn.net/#1 permanent; } |
4. Add 404 web pages
To add 404 web pages, you can add them directly in the page, for example:
The code is as follows: |
Copy code |
Server { Listen 80; Server_name www.111cn.net; # bind a domain name Index index.htm index.html index. php; # default file Root/home/www/111cn.net; # Website root directory Include location. conf; # call other rules, which can also be removed Error_page 404/404 .html; }
|
After learning the above four rules and methods, you can solve the problem of nginx multi-domain configuration independently.