$
来命名;index index.$geo.html index.0.html /index.html;
复制代码
index index.html
,即,若是没有给出index,默认初始页为index.htmlNginx给了三种方式来选择初始页,三种方式按照顺序来执行:php
--with-http_random_index_module
;index
指令规则来选择初始页;index
指令没法确认初始页,此时启用后
的自动生成模块才会被使用。切记,index
指令并非查到文件以后,就直接拿来用了。它的实际工做方式是:html
若是文件存在,则使用文件做为路径
,发起内部重定向。直观上看上去就像再一次从客户端发起请求,Nginx再一次搜索location
同样。nginx
既然是内部重定向
,域名+端口不发生变化,因此只会在同一个server
下搜索。git
一样,若是内部重定向
发生在proxy_pass
反向代理后,那么重定向只会发生在代理配置中的同一个server
。github
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
。后端
indexbash
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中文文档》前后端分离
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官方文档》
看到这里你难道还不想吐槽吗?
因此,搞IT,若是你已经入门,请认准《官方文档》《新框架(新工具,语言)从入门到精通的正确姿式》