Nginx server configuration of personality secondary domain name and multiple domain examples

  • 2020-05-10 23:27:25
  • OfStack

Personality level 2 domain name
Configure the personalized level 2 domain name.
Effect:
Visit URL http: / / custom ofstack. com
The actual URL / / www. ofstack. com/auth/custom

What we do is we configure the server to convert the access url to the actual url

Now let's use the nginx configuration. The configuration is as follows:


server {
    listen    80;
    server_name *.ofstack.com;
    if ( $host ~* (\b(?!www\b)\w+)\.\w+\.\w+ ) {
      set $subdomain $1;
    }
    location / {
      rewrite ^/$ /auth/$subdomain last;
      proxy_pass //www.ofstack.com/;
    }
  }

Among them, if is used to filter some special level 2 domain names, such as www, and then get the variable value of level 2 domain name.

rewrite to the corresponding directory


rewrite ^/$ /auth/$subdomain last;

Plus the reverse proxy function of nginx:

proxy_pass //www.ofstack.com/;
You can do it.

In this configuration, in addition to if filtering level 2 domain name, other 2 grade domain name {sudomain}. ofstack. com, for server, the true path are www. ofstack. com/auth / {sudomain}.

If url has multiple path rules, you need 11 to configure it.

Multi-domain configuration
  nginx can bind multiple domain names and write multiple domain name rules to a configuration file, or set up multiple domain name configuration files respectively. For the convenience of management, I usually set up 1 file for each domain name, and some similar domain names can also be written in a general configuration file.
1. How to write 1 file per domain name
            first open nginx domain configuration file storage directory: / usr/local/nginx/conf/servers,. If you want to binding domain www ofstack. com build 1 files in this directory: www. your - domain. conf then write rules in this document, such as: server


{

listen
80;
server_name
www.ofstack.com;
 # Binding domain 

index
 index.htm index.html index.php; # The default file 

root
 /home/www/ofstack.com; # Website root directory 

include
 location.conf; # Calls to other rules can also be removed 

}

nginx then restart the server, the domain name is binding a success nginx command: server restart/etc/init d/nginx restart
2. How to write multiple domain names in one file
The rule for adding multiple domain names to a file is the same as the rule for adding multiple domain names to a file. Just write down the single domain name above again and again, ok, e.g. server


{

listen
80;

server_name

bbs.ofstack.com;
 # Binding domain 

index
 index.htm index.html index.php; # The default file 

root
 /home/www/bbs.ofstack.com;
 #bbs directory 

include
 location.conf; # Calls to other rules can also be removed 

}server

{

listen
80;

server_name

www.ofstack.com;
 # Binding domain 

index
 index.htm index.html index.php; # The default file 

root
 /home/www/www.ofstack.com;
 # Website root directory 

include
 location.conf; # Calls to other rules can also be removed 

}

3. Add a 301 jump to a domain name without www
If the domain name without www is added to the 301 jump, it is the same as binding domain name 1, binding the domain name without www first, but do not write the website directory, but 301 jump, such as:


server

{

listen
80;

server_name

ofstack.com;

rewrite
 ^/(.*) //www.ofstack.com/$1 permanent;

}

4. Add a 404 page

           


server

{

listen
80;

server_name
 www.ofstack.com; # Binding domain 

index
 index.htm index.html index.php; # The default file 

root
 /home/www/ofstack.com; # Website root directory 

include
 location.conf; # Calls to other rules can also be removed 

error_page
404

/404.html;

}

Learn the above four rules and methods, and you will be able to solve the multi-domain configuration problem of nginx on your own


Related articles: