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,如果你已经入门,请认准《官方文档》《新框架(新工具,语言)从入门到精通的正确姿势》

目录目录完全理解index指令Nginx中文文档Nginx官方文档吐槽完全理解index指令该指令后面可以跟多个文件,用空格隔开;文件可以是相对路径也可以是绝对路径;文件可以使用变量$来命名;该指令拥有默认值,index index.html ,即,如果没有给出index,默认初始页为indexNginx中文文档index...
需要进入nginx的安装目录的(也可以配置环境变量,在任何目录都可以执行下面的命令),里面有一个nginx脚本文件 2、Nginx配置文件(nginx.conf) 2.1概述 nginx.conf主要由三部分组成 2.2全局块 就是配置文件从头开始到events块之间的内容,比如worker_process, 值越大,可以支持的并发处理量也越多,但是还是和服务器的硬件相关events 块涉及的指令主要影响 Nginx 服务器与用户的网络连接,常用的设置包括是否开启对多 work process下的网络连
1.1 本案例以 centos7 系统作为演示,配置文件在 /etc/nginx/conf.d/test.conf ,内容如下 server { listen 8000; server_name 127.0.0.1; root /home/cookcyq/web/; error_page 404 /40x.html; error_page 500 502 503 504 /50x.ht
上一篇文章写的nginx的基础安装,以及nginx配置文件的整体结构。这篇文章将会一起来看一下nginx的一个重要属性locationlocation是放在server里面的,用于匹配访问的域名后的path路径。location匹配的规则很复杂,内容也是非常多,这里只做简单的介绍。 location的匹配规则 location的三种匹配方式 location的匹配规则有三种,分别是精准匹配、普通匹配和正则匹配。下面对三种匹配做一个说明。 http://blog.itcrud.com/2018/12/12
今天继续给大家介绍Linux运维的相关知识,本文主要内容是Nginx配置文件详解。 一、Nginx location模块简介 二、Nginx location匹配符与匹配优先级 三、Nginx location匹配示例
Nginxlocation配置是通过在nginx配置文件定义location块来实现的。在location,您可以配置该路径下的各种请求处理方式。 语法如下: location [ = | ~ | ~* | ^~ ] uri { - `=` 号表示精确匹配,即请求路径与location后的uri完全一致时才会使用该location配置。 - `~` 号表示正则匹配,用于匹配大小写敏感的正则表达式。 - `~*` 号表示正则匹配,用于匹配大小写不敏感的正则表达式。 - `^~` 号表示前缀匹配,如果请求路径以location后的uri为前缀,则使用该location配置,并且不再继续匹配其他location。 例如,下面的配置将请求路径为/static/的请求转发到/data/www目录下: location /static/ { root /data/www; 更多信息,您可以参考nginx官方文档:http://nginx.org/en/docs/http/ngx_http_core_module.html#location
root cause org.apache.ibatis.ognl.OgnlException: source is null for getProperty(null, "XXX") 45048 解决方案(二)— 将 "http://apache.org/xml/features/disallow-doctype-decl" 设置为“true”时, 不允许使用 DOCTYPE m0_68032227: 能不能手动设置成false Nginx之坑:完全理解location中的index,配置网站初始页 沧海一声笑·rush: 原来如止!。多谢 阿里云服务器使用SSH工具连接不上,并且ping不通,显示连接超时 w1215435751: 我的是A服务器telnet不了B的22端口,超时,其他端口都可以通。用C服务器telnet的了B服务器的22端口,能通,不知道A服务器什么情况 mysqld_safe Directory ‘/var/lib/mysql‘ for UNIX socket file don‘t exists. weixin_52510780: 无法定位程序输入点ucrtbase.terminate于动态链接库api-ms-win-crt-runtime-|1-1-0.dll m0_50458308: 十分感谢,问题解决