How to use the nginx root, alias, and location directives

one. nginx root directive

1. Nginx configuration

The relevant configuration is as follows:


By configuring the root directory to the "/wwwroot/html/" location

In the virtual host method, the host name is test, you need to configure the host file of your PC, and you need to configure the nginx.conf file of the server, add the "include vhost/t.conf;" code, be careful not to carry Chinese quotation marks

2. Prepare some files, the file list is as follows


As shown in the figure above, we list the "directory structure and files" in the /wwwroot/html directory, and we will conduct experiments based on this directory structure in a while.

3. Access the page normally, as shown below


From the above figure, we can see that the access to /t/a.html is successful, that is, the "location /t/" configuration and root cooperation are normal

4. Access non-directory structures


As shown in the access result above, we cannot access c.html, why, in fact, the problem is not root, but "location /t/", according to this instruction, we must include "/t/" in our url, otherwise it is invalid, and we There is no other configuration in the virtual host, so c.html cannot be accessed

5. Root comparison configuration


As shown in the figure above, we configure the root directory to "/wwwroot/html/t/", and then reload the nginx configuration


Access the a.html file, which actually exists in this directory, and the results are as follows


The result of the above picture cannot really show the result due to the existence of the cache

The cache is broken through special parameters for testing, and the results are as follows:


From the above picture, there is no cache effect, but the page is still not found

After clearing the cache and testing again, the results are as follows


The result above is the case above.

summary


The top of the figure is the configuration of nginx's location, pay special attention to "/t/", we must carry this information in our access before we can access it.

The second line is the user's access

The third line is the root instruction of nginx, pay special attention to the "/" in the green area must be in one-to-one correspondence.

At the bottom is the directory and file display of the linux file system. Of course, the directory is the file in the /wwwroot/html/ directory.

从上图我们可以了解到:location指令的第一个“/”同root指令的最后一个“/”是一一对应的,然后,root的指定的目录中,包括 location指定的相关目录结构,图中最后一部分显示的结构。

用户访问中,相应的“/”也必须同location中的“/”进行相关对应。图中应该对应的“/”都已经用相同颜色标识出来,请注意!

二.nginx alias别名配置

1. 更新配置文件

别名的相关配置如下图,文件系统不变。


重新加载配置


2. 未清理缓存情况下的访问


3. 采用参数法突破缓存的结果


4. 清理缓存后访问结果


5. 更改配置文件进行对比实验


重新加载配置文件

清理浏览器缓存然后访问如下


从图中发现我们已经访问不了

访问c.html


可以访问了。为什么???

小结


从上图中我们了解到,location中 黄色“/”要同 alias中“/”相对应。

然后是服务器中的目录结构了。

再有就是html访问中(同location的关系)的情况,同上面root的情况类似。

总结:

1. Location管理服务器及用户浏览器的访问, 主要是http请求中url中特定部分同location指令特定部分要对应,需要对应在两个小结的图中已经给出了!

2. Root及alias指令主要是目录对应关系同location不同

3. Root的最后一个“/”要同location的第一个“/”对应,其他目录结构同location中剩余相同

4. Alias的最后一个“/”要同location的最后一个“/”对应

Guess you like

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