Nginx之坑:徹底理解location中的index,配置網站初始頁

index指令的做用

  • 在先後端分離的基礎上,經過Nginx配置,指定網站初始頁。

index指令詳解

基本內容(中文文檔和官方文檔均可見):

  • 該指令後面能夠跟多個文件,用空格隔開;
  • 若是包括多個文件,Nginx會根據文件的枚舉順序來檢查,直到查找的文件存在;
  • 文件能夠是相對路徑也能夠是絕對路徑,絕對路徑須要放在最後;
  • 文件可使用變量$來命名;
index  index.$geo.html  index.0.html  /index.html;
複製代碼
  • 該指令擁有默認值,index index.html ,即,若是沒有給出index,默認初始頁爲index.html

核心內容(中文文檔沒有或一筆帶過,而官方文檔做詳細解釋):

  • Nginx給了三種方式來選擇初始頁,三種方式按照順序來執行:php

    • 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指令並非查到文件以後,就直接拿來用了。它的實際工做方式是:html

  • 若是文件存在,則使用文件做爲路徑,發起內部重定向。直觀上看上去就像再一次從客戶端發起請求,Nginx再一次搜索location同樣。nginx

  • 既然是內部重定向,域名+端口不發生變化,因此只會在同一個server下搜索。git

  • 一樣,若是內部重定向發生在proxy_pass反向代理後,那麼重定向只會發生在代理配置中的同一個servergithub

實例

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中文文檔

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中文文檔》前後端分離


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

微信公衆號:流花鬼的博客

在這裏插入圖片描述
相關文章
相關標籤/搜索