Nginx的location里面的root、alias的使用技巧与区别
1. 介绍
1.1 介绍
福哥在将TFLinux的PHP+Apache的组合转换成PHP+FPM+Nginx的过程里遇到了一个问题,就是Apache的虚拟主机转为Nginx里面的虚拟主机的时候参数怎么解决呢?
今天福哥就将Nginx配置文件里面的location、root、alias的使用技巧给大家做一个讲解!
2. location
Nginx里面的location对应Aapache里面的Directory,可以针对一个特殊的URI路径进行单独的设置。
location / {
root /tongfu.net/web/static;
}
在location块里面可以单独设置映射目录、重写逻辑、默认文档等等。
location / {
root /tongfu.net/web/download;
index index.htm;
}
location ~ ^\/download\/.*\.(zip|rar|tgz|gz)nbsp;{
rewrite ^\/download\/(.*)nbsp; /downloadValidation.php?$1;
}
3. root
Nginx里面的root参数用来指定映射根目录,末尾不加“/”。
3.1 主机默认目录
直接在server里面设置root就是设置主机的根目录。
server {
root /tongfu.net/web/static;
}
3.2 匹配URI目录
在location里面设置root就是设置匹配URI的根目录。
下面的例子里如果访问 http://localhost/icon/abc.png 网址,映射到的服务器路径是 /tongfu.net/web/icons/icon/abc.png。
location /icon/ {
root /tongfu.net/web/icons;
}

4. alias
Nginx里面的root参数用来指定映射目录,末尾需要加“/”。
下面的例子里如果访问 http://localhost/icon/abc.png 网址,映射到的服务器路径是 /tongfu.net/web/icons/abc.png。
location /icon/ {
alias /tongfu.net/web/icons/;
}

5. 总结
今天福哥给童鞋们讲解了关于Nginx配置文件里面的location、root和alias的使用技巧和区别,大家今后在配置Nginx主机的时候就会得心应手了~~