One Nginx server configures multiple locations

The company test environment uses nginx to deploy multiple front-end projects. I found two ways online:

  • Add multiple locations to the configuration file, each location corresponds to a project. For
    example, using port 80, location / access the official website; location /train access the training management system
  • Configuring Multiple Sites
    I chose to configure multiple locations.
   location / {
         root   /data/html/;
         index  index.html index.html;
    }
    location /train {
         root   /data/trainning/;
         index  index.html index.html;
    }

Access after configuration. http://xxxx/train prompts 404
for a long time to figure out, if a specific url needs to use an alias, root cannot be used, the directory specified by alias is accurate, root is the upper-level directory of the specified directory, and it can be changed after modification. used

location /train {
     alias  /data/trainning/;
     index  index.html index.html;
}

===========================================
Additional
======= ====================================
In the message, a small partner asked about the difference between alias and root. Personal understanding:
The main difference between root and alias is how nginx interprets the uri behind the location, which causes the two to map requests to server files in different ways. The processing result of root is: root path + location path alias The processing result is: use alias path to replace location path alias is the definition of a directory alias, and root is the definition of the top-level directory. Another important difference is that the alias must be terminated with a "/", otherwise the file will not be found. . . And root is optional~~

Guess you like

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