location中的Index

  • 该指令拥有默认值,index index.html ,即,如果没有给出index,默认初始页为index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
server {
listen 80;
server_name example.org www.example.org;

location / {
root /data/www;
index index.html index.php;
}

location ~ \.php$ {
root /data/www/test;
}
}

上面的例子中,

  1. 如果你使用example.org或www.example.org直接发起请求,那么首先会访问到“/”的location,
  2. 结合root与index指令,会先判断/data/www/index.html是否存在,
    • 如果不存在,则接着查看/data/www/index.php ,
    • 如果存在,则使用/index.php发起内部重定向,就像从客户端再一次发起请求一样,Nginx会再一次搜索location,毫无疑问匹配到第二个~ .php$,从而访问到/data/www/test/index.php。
1
2
3
4
5
location / {
# alias /screct_user/;
index /screct_user/index.html;
autoindex on;
}

在没有alias或者root的情况下 , 访问的/就会映射到目录/usr/html/ ,所以会指向/usr/html/screct_user/index.html ,文件不存在 , 返回404

1
2
3
4
5
location / {
# alias /screct_user/;
# index /screct_user/index.html;
# autoindex on;
}

在没有alias或者root的情况下 , 访问的/就会映射到目录/usr/html/ , index的默认值是index.html , 所以会使用/usr/html/index.html , 即返回nginx默认的初始页.

1
2
3
4
5
location / {
alias /screct_user/;
# index /screct_user/index.html;
# autoindex on;
}

有alias , 访问的/就会映射到目录/screct_user/ , index的默认值是index.html , 所以会使用/screct_user/index.html

总结:

  • index的默认值是index.html ,
  • index使用的是相对路径 , 如果没有alias或者root , 那么就是/usr/html , 如果有alias或者root , 那么index就是基于该路径的相对路径

测试指定的Nginx配置文件

使用-c参数指定配置文件

1
nginx -t -c /etc/nginx/nginx. conf