# index

指令<mark style="color:blue;">**index**</mark>用于<mark style="color:blue;">**设置网站的默认首页**</mark>，它一般可以有两个作用：

* 一是，用户在发出请求访问网站时，请求地址可以不写首页名称；
* 二是，可以对一个请求，根据其请求内容而设置不同的首页。

该指令的语法结构为：

```nginx
index file ...;
```

其中，<mark style="color:blue;">**file**</mark>变量可以包括多个文件名，其间使用空格分隔，也可以包含其他变量。此变量默认为 “**index.html**”。

{% hint style="success" %}

```nginx
location ~^/data/(.+)/web/ $
{
    index index.$1.html index.my1.html index.html;
}
```

当location块接收到“**/data/locationtest/web/**”时，匹配成功，它首先将预置变&#x91CF;**$1**置为“**locationtest**”，然后&#x5728;**/data/locationtest/web/**&#x8DEF;径下按照index的配置次序**依次**寻找**index.locationtest.html**页、**index.my1.html**页和**index.html**页，首先找到哪个页面，就使用哪个页面响应请求。
{% endhint %}
