介绍

nginx配置文件中常用root指定当前项目根目录

alias也是用来替换用户指定的项目目录的

接下来说一下他们的差别

项目配置文件

[root@localhost webroot]# tree.├── code│   └── test2.html└── test.html1 directory, 2 files[root@localhost webroot]# cat test.html aaa[root@localhost webroot]# cat code/test2.html bbb

nginx配置

alias重定向项目目录location /code/{alias /data/webroot/;}访问结果[root@localhost webroot]# curl http://192.168.1.128/html/test.htmlaaa访问到文件:/data/webroot/tesst.html
root重定向项目目录location /code/{root /data/webroot/;}访问同样结果[root@localhost webroot]# curl http://192.168.1.128/code/test2.htmlbbb访问到的文件:/data/webroot/code/tesst2.html

总结

nginx使用root来指定“$document_root”变量,请求访问到的文件是“$document_root”+URL

使用alias访问的就是“$document_root”+"匹配的后面一部分"

 绝对路径名对应的当前请求的root或alias指定的路径时,所有的符号链接解析为真正的路径

nginx教程

[第一章 nginx安装基本引导和进程信号](http://new.nginxs.net/read.php/post-201604241128/) 

[02-nginx IO模型](http://new.nginxs.net/read.php/post-201605070009/) 

[03-nginx负载均衡](http://new.nginxs.net/read.php/post-201605072039) 

[04-nginx的root和alias区别](http://new.nginxs.net/read.php/post-201605122203/)