Nginx搭建靜態資源服務配置詳解

2021-09-05 12:24:18 字數 1886 閱讀 1146

第一種:單個server,root配置根目錄

nginx配置檔案中,server模組預設配置了location,如下:

server 

}

上述location的配置是「/」,當我們訪問192.168.121.140:8088/ 或 192.168.121.140:8088,nginx會去html(root 配置項)目錄下去找index.html或者index.htm(index配置項)

1、如果我們修改root的值為

location /
那麼訪問192.168.121.140:8088/ 或 192.168.121.140:8088,nginx會到html/main目錄下找index.html或index.htm。

2、如果我們修改location為

location /index
那麼訪問路徑改為192.168.121.140:8088/index 或 192.168.121.140:8088/index/,nginx會到html/main/index目錄下找index.html或index.htm。

3、如果在目錄html/main/index下還有乙個資料夾index2,該資料夾下有index2.html。那麼採用下面的配置

location /index
如果我們訪問到index2.html,那麼我們的訪問位址為192.168.121.140:8088/index/index2/index2.html。

4、對於下面的配置

location /
5、對於精確匹配的理解:如果有兩個location配置如下

//第一種 全域性預設匹配

location /

//第二種 匹配index

location /index

(1)當訪問位址為192.168.121.140:8088/index,匹配到第二種location

(2)當訪問位址為192.168.121.140:8088/,匹配到第一種location

(3)當訪問位址為192.168.121.140:8088/main,匹配到第乙個location,因為沒有location是main的。

第二種:單個server,alias配置根目錄

除了上面的root配置檔案的目錄,還有個alias配置,其和root的區別在於nginx搜尋檔案時是否需要加上location目錄。

//nginx搜尋檔案的目錄為html/main/index/index.html

location /index

//nginx搜尋檔案的目錄為html/main/index.html

location /index

第三種:多個server,root或alias配置根目錄

在nginx配置中,乙個http模組可以配置多個server,也就是說我們可以配置多個服務。

server 

}server

}

上面的兩個server配置,只有埠和index配置不一樣,當訪問位址的埠為8088,請求到第乙個server。當訪問位址的埠為8000,請求到第二個server。

**小結:**如果乙個server模組,有多個location模組,nginx的匹配原則是精確匹配優先。對於靜態檔案,如果使用root配置檔案根目錄,nginx搜尋檔案的路徑為root + /location + /index。如果使用alias配置檔案根目錄,nginx搜尋檔案的路徑為alias+/index。

搭建nginx靜態資源伺服器

1.windows下nginx作為靜態資源伺服器使用小結 2.nginx常用命令 3.直接在命令列中使用nginx命令會出現bash nginx.exe command not found,可以這樣寫.nginx s reload 4.檢視本地ip位址 在cmd命令面板中輸入ipconfig就可以檢...

nginx搭建靜態資源伺服器

修改nginx.conf配置檔案 修改服務節點server server 啟動nginx即可 其中在location節點下有指點靜態檔案路徑有兩個指令 root和alias root 使用root如上述形式,則對映的訪問路徑根目錄 location指定的訪問目錄,即e tools show 訪問結果...

nginx配置靜態資源

http請求 server location project1 error page 500 502 503 504 50x.html location 50x.html 此時訪問www.test.com 進入location 訪問www.test.com project1 進入location p...