Nginx virtual directory alias and root directory

Nginx virtual directory alias and root directory

 

Nginx sets the virtual directory through alias. In the configuration of nginx, the alias directory and the root directory are different:
1) The directory specified by alias is accurate, that is, the files in the path directory where the location matches the access are directly in the alias directory 2
) The directory specified by root is the upper-level directory of the path directory accessed by location matching, and the path directory must exist in the directory specified by root;
3) The break of rewrite cannot be used in the directory block using the alias tag ( The specific reason is unknown); in addition, the "/" symbol must be added after the directory specified by alias! !
4) In the alias virtual directory configuration, if the path directory matching the location does not have "/" after it, then adding "/" after the path directory in the accessed url address does not affect the access, and it will automatically add "/" when accessing ";
    but if "/" is added after the path directory matching the location, then the path directory must be added with "/" in the accessed url address, and it will not automatically add "/" when accessing. If you don't add "/", the access will fail!
5) In the root directory configuration, the path directory matching the location with or without "/" will not affect access.


For example (for example, the domain name configured by nginx is www.wangshibo.com):
(1)
location /huan/ {
      alias /home/www/huan/;
}

Under the alias virtual directory configuration above, visiting http://www.wangshibo.com/huan/a.html actually specifies /home/www/huan/a.html.
Note: "/" must be added after the directory specified by alias, 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 that nginx will go to /home/www/huan to find the access resources of http://www.wangshibo.com/huan, the access effect after the two configurations is the same of!
location /huan/ {
       root /home/www/;
}

(2)
In the above example, the directory name set by alias is the same as the path directory name accessed by location matching, so it can be directly changed to the root directory configuration; what if it is inconsistent?
Another example:
location /web/ {
      alias /home/www/html/;
}

When you 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 make html->web (soft connection, that is, a shortcut) under /home/www, as follows:
location /web/ {
     root /home/www/;
}

# ln -s /home/www/web /home/www/html //That is, keep the content of /home/www/web and /home/www/html always

So, in general, a good practice in nginx configuration is:
1) Configure the root directory in location /;
2) Configure the alias virtual directory in location /path.

如下一例:
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/; //The following "/" symbol must be
       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 (the wang directory must exist under /var/www/html)
      root /var/www/html;
     }

 }

****************When you find that your talent can't support your ambition, please calm down and study***************

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324527809&siteId=291194637
Recommended