index指令作用
通过nginx配置文件,指定网站初始页。
index指令详解
index指令后可以跟多个文件,用空格隔开。nginx会根据文件的枚举顺序来查找文件,直到文件存在。- 文件可以使用变量
$来命名。
index index.$geo.html index.0.html /index.html;
index指令执行过程
index指令在查找到文件之后,会使用文件作为路径,发起内部重定向。类似再一次从客户端发起请求,nginx再一次搜索location一样。既然是内部重定向,域名和端口不会发生变化,所以只会在同一个server下搜索。
示例
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;
}
}
上面的例子中,如果你使用example.org或www.example.org直接发起请求,那么首先会访问到“/”的location,结合root与index指令,会先判断/data/www/index.html是否存在,如果不,则接着查看 /data/www/index.php ,如果存在,则使用/index.php发起内部重定向,就像从客户端再一次发起请求一样,Nginx会再一次搜索location,毫无疑问匹配到第二个~ .php$,从而访问到/data/www/test/index.php。