nginx静态多目录配置

一 nginx静态多目录配置

1.默认配置为

1
2
3
4
5
6
7
server {
listen port;
location / {
root /var/www/root_path;
index index.html index.htm;
}
}

其中root表明路径,在nginx中有两个指令用来设置项目路径:root & alias
主要区别:

root与alias主要区别在于nginx如何解释location后面的uri,这会使两者分别以不同的方式将请求映射到服务器文件上。

  1. root的处理结果是:root路径+location路径。alias的处理结果是:使用alias路径替换location路径
  2. alias是一个目录别名的定义,root则是最上层目录的定义。
  3. alias后面必须要用“/”结束,否则会找不到文件的。。。而root则可有可无~~

2.多目录配置

1
2
3
4
5
6
7
8
9
10
11
12
server {
listen port;
location / {
root path;
index index.html index.htm;
}

location /blog/ {
alias path;
index index.html index.htm;
}
}

注意在上面代码中如果第二个location中实用root,则项目发布后,URL中索引文件目录为path/blog/ + url中的路径