Nginx之坑:完全理解location中的index,配置网站初始页

4,503 阅读3分钟

index指令的作用

  • 在前后端分离的基础上,通过Nginx配置,指定网站初始页。

index指令详解

基本内容(中文文档和官方文档都可见):

  • 该指令后面可以跟多个文件,用空格隔开;
  • 如果包括多个文件,Nginx会根据文件的枚举顺序来检查,直到查找的文件存在;
  • 文件可以是相对路径也可以是绝对路径,绝对路径需要放在最后;
  • 文件可以使用变量$来命名;
index  index.$geo.html  index.0.html  /index.html;
  • 该指令拥有默认值,index index.html ,即,如果没有给出index,默认初始页为index.html

核心内容(中文文档没有或一笔带过,而官方文档作详细解释):

  • Nginx给了三种方式来选择初始页,三种方式按照顺序来执行:

    • ngx_http_random_index_module 模块,从给定的目录中随机选择一个文件作为初始页,而且这个动作发生在 ngx_http_index_module 之前,注意:这个模块默认情况下没有被安装,需要在安装时提供配置参数 --with-http_random_index_module
    • ngx_http_index_module 模块,根据index指令规则来选择初始页;
    • ngx_http_autoindex_module 模块,可以使用指定方式,根据给定目录中的文件列表自动生成初始页,这个动作发生在 ngx_http_index_module之后,即只有通过index指令无法确认初始页,此时启用后的自动生成模块才会被使用。
  • 切记,index指令并不是查到文件之后,就直接拿来用了。它的实际工作方式是:

  • 如果文件存在,则使用文件作为路径,发起内部重定向。直观上看上去就像再一次从客户端发起请求,Nginx再一次搜索location一样。

  • 既然是内部重定向,域名+端口不发生变化,所以只会在同一个server下搜索。

  • 同样,如果内部重定向发生在proxy_pass反向代理后,那么重定向只会发生在代理配置中的同一个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.orgwww.example.org直接发起请求,那么首先会访问到“/”location,结合rootindex指令,会先判断/data/www/index.html是否存在,如果不,则接着查看 /data/www/index.php ,如果存在,则使用/index.php发起内部重定向,就像从客户端再一次发起请求一样,Nginx会再一次搜索location,毫无疑问匹配到第二个~ \.php$,从而访问到/data/www/test/index.php


Nginx中文文档

index


syntax: index file [file...] default: index index.html context: http, server, location Directive determines the file(s) which will be used as the index. It's possible to use variables in the name of file. The presence of the files is checked in the order of their enumeration. A file with an absolute path can be put at the end. Example using a variable:

index  index.$geo.html  index.0.html  /index.html;

If you want to automatically generate an index from a directory listing, useautoindex on.

详情可见:《HttpIndex模块》《Nginx中文文档》


Nginx官方文档

The ngx_http_index_module module processes requests ending with the slash character (‘/’). Such requests can also be processed by the ngx_http_autoindex_module and ngx_http_random_index_module modules.

Example Configuration

location / {
    index index.$geo.html index.html;
}

Directives

Syntax: index file ...; Default: index index.html; Context: http,server, location

Defines files that will be used as an index. The file name can contain variables. Files are checked in the specified order. The last element of the list can be a file with an absolute path. Example:

index index.$geo.html index.0.html /index.html;

It should be noted that using an index file causes an internal redirect, and the request can be processed in a different location. For example, with the following configuration:

location = / {
    index index.html;
}

location / {
    ...
}

a “/” request will actually be processed in the second location as “/index.html”.

详情可见:《Nginx官方文档》


吐槽

看到这里你难道还不想吐槽吗?

  • 《Nginx中文文档》结果内容不是中文,很滑稽但还能理解;
  • 《HttpIndex模块》基本内容抄出来了,核心内容省略了?但这难道不是误人子弟,不可避免的要踩这个坑,而且浪费了不少时间!!

所以,搞IT,如果你已经入门,请认准《官方文档》《新框架(新工具,语言)从入门到精通的正确姿势》

微信公众号:流花鬼的博客

在这里插入图片描述