Nginx virtual directory alias and root directory

Posted by todd2006 on Thu, 10 Feb 2022 12:48:40 +0100

Nginx sets the virtual directory through alias. In the configuration of nginx, the alias directory is different from the root directory:

1) The directory specified by alias is accurate, that is, the files in the path directory accessed by location matching are directly found in the alias directory;
2) The directory specified by root is the upper level directory of the path directory whose location matches the access. This path directory must really exist under the directory specified by root;
3) The break of rewrite cannot be used in the directory block with alias tag (the specific reason is unknown); In addition, the directory specified by alias must be followed by the "/" symbol!!
4) In the alias virtual directory configuration, if the path directory matching the location is not followed by "/", then whether to add "/" after the path directory in the accessed url address will not affect the access, and it will automatically add "/" when accessing;
However, if "/" is added after the path directory matching the location, then "/" must be added to the path directory in the url address to be accessed, and "/" will not be added automatically during access. If "/" is not added, the access will fail!
5) In the root directory configuration, whether the path directory matching location is followed by "/" or not will not affect access.

For example (for example, the domain name configured by nginx is www.wangshibo.com):

Example 1

location /huan/ {
      alias /home/www/huan/;
}

Under the above alias virtual directory configuration, access http://www.wangshibo.com/huan/a.html The actual designation is / home/www/huan/a.html.

Note: the directory specified by alias must be followed by "/", that is, / home/www/huan / cannot be changed to / home/www/huan

The above configuration can also be changed to the root directory configuration as follows, so nginx will go to / home/www/huan to find it http://www.wangshibo.com/huan The access effect of the two configurations is the same!

location /huan/ {
       root /home/www/;
}

Example 2

In the above example, the directory name set by alias is consistent with the path directory name accessed by location matching, so it can be directly changed to the root directory configuration; What if they don't agree?

Take another example:

location /web/ {
      alias /home/www/html/;
}

visit http://www.wangshibo.com/web You will go to / home/www/html / to find access resources.
In this case, it cannot be directly changed to the root directory configuration.
If you have to change to the root directory configuration, you can only set HTML - > Web (as a soft connection, i.e. shortcut) under home/www,
As follows:

location /web/ {
     root /home/www/;
}
# ln -s /home/www/web /home/www/html / / keep the contents of / home/www/web and / home/www/html constant

Therefore, in general, good habits in nginx configuration are:

1) Configure the root directory in location /;
2) Configure the alias virtual directory in location /path.

As an example:

server {
          listen 80;
          server_name www.wangshibo.com;
          index index.html index.php index.htm;
          access_log /usr/local/nginx/logs/image.log;

    location / {
        root /var/www/html;
        }

   location /haha {                                          //The matching path directory haha does not need to exist in the directory specified by alias
       alias /var/www/html/ops/;                       //Be sure to bring the "/" symbol after it
       rewrite ^/opp/hen.php(.*)$ /opp/hen.php?s=$1 last;
    # rewrite ^/opp/(.*)$ /opp/hen.php?s=$1 last;
       }

   location /wang {                    //The matching path directory wang must actually exist in the directory specified by root (there must be a wang directory under / var/www/html)
      root /var/www/html;
     }

 }

Take another example:

[root@web01 vhosts]# cat www.kevin.com.conf
server {
      listen      80;
      server_name www.kevin.com;
     
      access_log  /data/nginx/logs/www.kevin.com-access.log main;
      error_log  /data/nginx/logs/www.kevin.com-error.log;
     
 location / {
      root /data/web/kevin;
      index index.php index.html index.htm;
      }
 
  location /document/ {
      alias /data/web/document/;
}
 
  }
 
 
[root@web01 vhosts]# ll /data/web/
total 4
drwxrwxr-x 2 app app   33 Nov 22 10:22 document
drwxrwxr-x 4 app app  173 Sep 23 15:00 kevin

After the above configuration, then:
visit http://www.kevin.com/admin You will find the / data/web/kevin/admin directory
visit http://www.kevin.com/document You will find the / data/web/document directory (which contains some static resources)