Nginx默认location index设置网站的默认首页方法详解

/斜杠代表location定位的路径,路径当中最重要的字段就是root。
root默认值就是html,这个就是nginx安装路径下面的html文件夹作为root的路径。默认不配置就是root下面的内容,index指定了主页的内容。
[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斜杠表示服务的根目录,这里index指令是去指定首页。

root你这里可以更改为自己想要设置的目录,并且它的相对路径是在nginx的安装路径下面。
如果你需要写绝对路径,那么前面加入斜杠/nginx/。
还有一点就是index后面其实默认只有一个配置index.html,另外一个Index.htm是不生效的。这个主页你可以自己配置。
location / {
root /usr/local/nginx/html;
index index.html index.htm;
}实际当中的配置

[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>
// 如果当前浏览器已登录账户,需自动跳转到主页
if(document.cookie && document.cookie.indexOf('_bank_token') != -1) {
window.location.href = 'app.html?v=ebba';Nginx index:首页处理
HTTP 请求经过一系列的请求流程处理后,最终将读取数据并把数据内容返回给用户。当用户请求没有明确指定请求的文件名称时,Nginx 会根据设定返回默认数据,实现这一功能包含 ngx_http_index_module、ngx_http_random_index_module、ngx_http_autoindex_module 这 3 个模块。
常用的首页处理配置指令如下面表格中所示。

配置样例如下:
location / {
index index.$geo.html index.html;
}指令值为多个文件时,会按照从左到右的顺序依次查找,找到对应文件后将结束查找。
总结
到此这篇关于Nginx默认location index设置网站的默认首页的文章就介绍到这了,更多相关Nginx index网站默认首页内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
PHP的Symfony和CodeIgniter框架的Nginx重写规则配置
这篇文章主要介绍了PHP的Symfony和CodeIgniter框架的Nginx重写规则配置,文中截取配置中关键的一些rewrite写法进行讲解,需要的朋友可以参考下2016-01-01
记一次nginx配置不当引发的499与failover 机制失效问题
近期在非高峰期也存在499超过告警阈值的偶发情况,多的时候一天几次,少的时候则几天一次,持续一般也就数分钟,经过和小伙伴的共同探究,最后发现之前对于499是客户端主动断开因而和服务端关系不大的想当然认知是错误的,这里记录一下2023-05-05
Nginx报:Nginx - 504 Gateway Time-out问题解决办法
这篇文章主要给大家介绍了关于Nginx报:Nginx - 504 Gateway Time-out问题的解决办法,一般是由于程序执行时间过长导致响应超时,例如程序需要执行90秒,而nginx最大响应等待时间为30秒,这样就会出现超时,需要的朋友可以参考下2024-01-01


最新评论