Multiple domain name configuration method for an nginx server

  • 2020-05-14 06:09:25
  • OfStack

Nginx's powerful regular expression support makes the configuration of server_name very flexible, and if you want to do multiple user blogs, it's easy to implement a level 2 domain for each user.

Let me talk about the use of server_name:

The matching order of server_name

The server_name instruction in Nginx is mainly used to configure the name-based virtual host, and the matching order of server_name instruction after receiving the request is as follows:

1. Accurate server_name match, for example:


server {
listen 80;
server_name ssdr.info www.ssdr.info;
...
}

2. String starting with * wildcard:


server {
listen 80;
server_name *.ssdr.info;
...
}

3. String ending with * wildcard:


server {
listen 80;
server_name www.*;
...
}

4. Match regular expressions:


server {
listen 80;
server_name ~^(?.+)\.howtocn\.org$;
...
}

Nginx will match server name in the order of 1,2,3,4, and only 1 match will stop the search, so when we use this instruction, 1 must distinguish its matching order (similar to location instruction).

One useful feature of the server_name directive 1 is the ability to use the regular expression capture feature to minimize configuration files that are too long for routine maintenance. Here are two specific applications:

Configure multiple sites in 1 server block:


server
{
listen 80;
server_name ~^(www\.)?(.+)$;
index index.php index.html;
root /data/wwwsite/$2;
}

The site's home directory should look something like this:


/data/wwwsite/ssdr.info
/data/wwwsite/linuxtone.org
/data/wwwsite/baidu.com
/data/wwwsite/google.com

This allows you to configure multiple sites with just one server block.

Configure multiple level 2 domains for one site in one server block.

In the actual website directory structure, we usually create a separate directory for the 2-level domain name of the site. Similarly, we can use the regular capture to configure multiple 2-level domain names in 1 server block:


server
{
listen 80;
server_name ~^(.+)?\.howtocn\.org$;
index index.html;
if ($host = ssdr.info){
rewrite ^ http://www.ssdr.info permanent;
}
root /data/wwwsite/ssdr.info/$1/;
}

The directory structure of your site should look like this:


/data/wwwsite/ssdr.info/www/
/data/wwwsite/ssdr.info/nginx/

Such access www. ssdr. info root directory to/data wwwsite/ssdr info/www, nginx. ssdr. As/when info data/wwwsite/ssdr info nginx/and so on.

The following if statement redirects the ssdr.info orientation to www.ssdr.info, which not only addresses home directory access to the site, but also increases the domain weight of www.ssdr.info in seo.

Multiple regular expressions

If you use regular in server_name, and the following location field USES regular matching, you will not be able to use references like $1, $2, and so on. The solution is to assign it to a named variable via the set directive:


server
{
listen 80;
server_name ~^(.+)?\.howtocn\.org$;
set $www_root $1;
root /data/wwwsite/ssdr.info/$www_root/;
location ~ .*\.php?$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/wwwsite/ssdr.info/$fastcgi_script_name;
include fastcgi_params;
}
}

Nginx different domain name reverse proxy to another server proxy_pass and $host

Want 1 VPS specializes in another 1 VPS front-end, back-end VPS every add a domain name, the front-end VPS will add a domain name to the reverse proxy at the same time, as a front-end VPS if 1 1 to add the back-end VPS domain names, so this special trouble, can let its automatic reverse proxy back-end VPS, use proxy_pass and $host can easily implement.

The following example USES lnmp as the setup environment to save effort

Modify the nginx. conf file of the front-end VPS to:


server {
listen 80;
server_name $host;
location / {
proxy_pass http://www.31.gd/;
proxy_set_header Host $host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 60;
proxy_read_timeout 600;
proxy_send_timeout 600;
}

Change the 1 below.


server {
listen 80;
server_name *.ssdr.info;
...
}
0

In this way, the front-end VPS can reverse proxy any domain name to the back-end VPS. As long as the domain name is resolved to the front-end VPS and the back-end VPS is bound to the domain name, the front-end VPS can be directly accessed

1 nginx with multiple domain name multiple tomcat configuration

Multiple domain names, 2 of which need support for generic domain name resolution:
1, www. abc. com
2, www. bcd. com
3, *. efg com
4, *. hij com

Among them, 1,2, 3 are 1 tomcat and 4 are independent tomcat. A front-end nginx enables this deployment by configuring multiple virtual hosts.
Enter/etc nginx/conf d directory, all of the virtual host configuration files are stored in the directory, configuration.

Configuration support for generic domain names


server {
listen 80;
server_name *.ssdr.info;
...
}
1

The key to universal domain name resolution is the red part. If there is no red part, the tomcat virtual host of 8080 and 8081 at the back end will not be able to obtain the domain name information, resulting in the failure of the back end tomcat to obtain the corresponding domain name information.

When the back-end TOMCAT supports generic domain name resolution, host name needs to be set to localhost to support generic domain name pointing.

Nginx multi-domain configuration

nginx can bind multiple domain names and write multiple domain name rules into a configuration file, or set up multiple domain name configuration files respectively. For the convenience of management, I usually set up one file for each domain name, and some similar domain names can also be written into 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 web126. com build 1 files in this directory: www. web126. com. conf then write rules in this document, such as:


server {
listen 80;
server_name *.ssdr.info;
...
}
2

Then restart the nginx server and the domain name binding is successful.

Nginx server restart command: / 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. As long as the above single domain name is repeatedly written down, ok is as follows:


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

3. Add 301 to domain name without www

If the domain name without www is added to the 301 jump, it is also 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 *.ssdr.info;
...
}
4

4. Add a 404 page

Add 404 pages, can also be added directly inside, such as:


server {
listen 80;
server_name *.ssdr.info;
...
}
5

Finally, there is another method that needs to be noted. It may be necessary to prohibit IP from directly accessing port 80 or to prevent the domain name of non-website from binding to our IP
The following processing can be placed on the top 1 server:


server {
listen 80;
server_name *.ssdr.info;
...
}
6

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


Related articles: