$
來命名;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,若是你已經入門,請認準《官方文檔》《新框架(新工具,語言)從入門到精通的正確姿式》