Nginx默認location?index設(shè)置網(wǎng)站的默認首頁方法詳解

/斜杠代表location定位的路徑,路徑當(dāng)中最重要的字段就是root。
root默認值就是html,這個就是nginx安裝路徑下面的html文件夾作為root的路徑。默認不配置就是root下面的內(nèi)容,index指定了主頁的內(nèi)容。
[root@jenkins html]# echo test > test.html
[root@jenkins html]# ls
50x.html dist test.html
root@jenkins html]# pwd
/usr/local/nginx/html
[root@jenkins html]# ls
50x.html dist test.html
[root@jenkins html]# cat test.html
test
[root@jenkins ~]# vim /usr/local/nginx/conf/nginx.conf
location / {
root html;
index test.html;
}location斜杠表示服務(wù)的根目錄,這里index指令是去指定首頁。

root你這里可以更改為自己想要設(shè)置的目錄,并且它的相對路徑是在nginx的安裝路徑下面。
如果你需要寫絕對路徑,那么前面加入斜杠/nginx/。
還有一點就是index后面其實默認只有一個配置index.html,另外一個Index.htm是不生效的。這個主頁你可以自己配置。
location / {
root /usr/local/nginx/html;
index index.html index.htm;
}實際當(dāng)中的配置

[www@12-116-efx-simulation estage-front]$ cat /application/nginx/conf/conf.d/estage-front.conf
server {
listen 7080;
server_name _;
location / {
root /data/app/estage-front/;
index login.html;
}
}
[root@12-116-efx-simulation ~]# ls -l /data/app/estage-front/
總用量 56
-rw-r--r--. 1 www www 5480 9月 16 11:09 app.html
drwxr-xr-x. 3 www www 184 9月 16 11:09 bigdata
drwxr-xr-x. 2 www www 4096 9月 16 11:09 defs
drwxr-xr-x. 2 www www 191 9月 16 11:09 extention
-rw-r--r--. 1 www www 7806 9月 16 11:09 get.html
-rw-r--r--. 1 www www 5033 9月 16 11:09 getRiskPDF.html
drwxr-xr-x. 23 www www 8192 9月 16 11:09 iframe
-rw-r--r--. 1 www www 7738 9月 16 11:09 login.html
-rw-r--r--. 1 www www 4460 9月 16 11:09 modifyPwd.html
drwxr-xr-x. 2 www www 24 9月 16 11:09 proxy
drwxr-xr-x. 8 www www 76 9月 16 11:09 static
[www@12-116-efx-simulation estage-front]$ curl 192.168.12.116:7080/login.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content ="IE=edge,chrome=1"/>
<title></title>
<script>
// 如果當(dāng)前瀏覽器已登錄賬戶,需自動跳轉(zhuǎn)到主頁
if(document.cookie && document.cookie.indexOf('_bank_token') != -1) {
window.location.href = 'app.html?v=ebba';Nginx index:首頁處理
HTTP 請求經(jīng)過一系列的請求流程處理后,最終將讀取數(shù)據(jù)并把數(shù)據(jù)內(nèi)容返回給用戶。當(dāng)用戶請求沒有明確指定請求的文件名稱時,Nginx 會根據(jù)設(shè)定返回默認數(shù)據(jù),實現(xiàn)這一功能包含 ngx_http_index_module、ngx_http_random_index_module、ngx_http_autoindex_module 這 3 個模塊。
常用的首頁處理配置指令如下面表格中所示。

配置樣例如下:
location / {
index index.$geo.html index.html;
}指令值為多個文件時,會按照從左到右的順序依次查找,找到對應(yīng)文件后將結(jié)束查找。
總結(jié)
到此這篇關(guān)于Nginx默認location index設(shè)置網(wǎng)站的默認首頁的文章就介紹到這了,更多相關(guān)Nginx index網(wǎng)站默認首頁內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
PHP的Symfony和CodeIgniter框架的Nginx重寫規(guī)則配置
這篇文章主要介紹了PHP的Symfony和CodeIgniter框架的Nginx重寫規(guī)則配置,文中截取配置中關(guān)鍵的一些rewrite寫法進行講解,需要的朋友可以參考下2016-01-01
記一次nginx配置不當(dāng)引發(fā)的499與failover 機制失效問題
近期在非高峰期也存在499超過告警閾值的偶發(fā)情況,多的時候一天幾次,少的時候則幾天一次,持續(xù)一般也就數(shù)分鐘,經(jīng)過和小伙伴的共同探究,最后發(fā)現(xiàn)之前對于499是客戶端主動斷開因而和服務(wù)端關(guān)系不大的想當(dāng)然認知是錯誤的,這里記錄一下2023-05-05
使用Nginx實現(xiàn)端口轉(zhuǎn)發(fā)TCP代理的實現(xiàn)示例
本文主要介紹了使用Nginx實現(xiàn)端口轉(zhuǎn)發(fā)TCP代理的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12
Nginx報:Nginx?-?504?Gateway?Time-out問題解決辦法
這篇文章主要給大家介紹了關(guān)于Nginx報:Nginx?-?504?Gateway?Time-out問題的解決辦法,一般是由于程序執(zhí)行時間過長導(dǎo)致響應(yīng)超時,例如程序需要執(zhí)行90秒,而nginx最大響應(yīng)等待時間為30秒,這樣就會出現(xiàn)超時,需要的朋友可以參考下2024-01-01
nginx http響應(yīng)限速的具體實現(xiàn)
本文主要介紹了nginx http響應(yīng)限速的具體實現(xiàn),可以使用limite_rate和limit_rate_after來限制HTTP響應(yīng)的速度,具有一定的參考價值,感興趣的可以了解一下2024-05-05

