Nginx 默认的location index设置网站的默认首页

/斜杠代表location定位的路径,路径当中最重要的字段就是root。

root默认值就是html,这个就是nginx安装路径下面的html文件夹作为root的路径。默认不配置就是root下面的内容,index指定了主页的内容。

bash 复制代码
[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是不生效的。这个主页你可以自己配置。

bash 复制代码
location / {
    root /usr/local/nginx/html;
    index index.html index.htm;
}

Nginx index:首页处理


HTTP 请求经过一系列的请求流程处理后,最终将读取数据并把数据内容返回给用户。当用户请求没有明确指定请求的文件名称时,Nginx 会根据设定返回默认数据,实现这一功能包含 ngx_http_index_module、ngx_http_random_index_module、ngx_http_autoindex_module 这 3 个模块。

常用的首页处理配置指令如下面表格中所示。

配置样例如下:

bash 复制代码
location / {
    index index.$geo.html index.html;
}

指令值为多个文件时,会按照从左到右的顺序依次查找,找到对应文件后将结束查找。

相关推荐
雨落Liy3 天前
Nginx 从入门到进阶:反向代理、负载均衡与高性能实战指南
运维·nginx·负载均衡
Yyyy4823 天前
Nginx负载均衡集群实验步骤
运维·nginx·负载均衡
qq_264220893 天前
Nginx优化与 SSL/TLS配置
运维·nginx
matlab的学徒3 天前
Web与Nginx网站服务(改)
linux·运维·前端·nginx·tomcat
邂逅星河浪漫4 天前
【Docker+Nginx+Ollama】前后端分离式项目部署(传统打包方式)
java·nginx·docker·部署
IT成长日记4 天前
【Nginx开荒攻略】Nginx主配置文件结构与核心模块详解:从0到1掌握nginx.conf:
linux·运维·nginx·配置文件
ZzzZZzzzZZZzzzz…4 天前
Nginx_Tomcat综合案例
运维·nginx·tomcat·负载均衡·rhce·反向代理·https加密服务
VueVirtuoso4 天前
SaaS 建站从 0 到 1 教程:Vue 动态域名 + 后端子域名管理 + Nginx 配置
前端·vue.js·nginx
007php0075 天前
Redis高级面试题解析:深入理解Redis的工作原理与优化策略
java·开发语言·redis·nginx·缓存·面试·职场和发展
Xxtaoaooo5 天前
Nginx 502 网关错误:upstream 超时配置的踩坑与优化
运维·nginx·负载均衡·502错误·upstream超时