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;
}

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

相关推荐
linweidong4 小时前
顺丰运维面试题及参考答案
运维·nginx·容器·ansible·运维开发·防火墙·python面试
MonkeyKing_sunyuhua5 小时前
ubuntu22.04安装nginx
运维·windows·nginx
无极小卒10 小时前
Nginx服务器无法访问图片的问题解决
运维·服务器·nginx
眠りたいです11 小时前
Docker:认识Docker镜像仓库并进行拉取推送
运维·nginx·docker·容器·busybox
kong790692811 小时前
环境搭建-运行前端工程(Nginx)
前端·nginx·前端工程
苹果醋312 小时前
java设计模式之责任链模式
java·运维·spring boot·mysql·nginx
卡布叻_星星1 天前
Docker之Nginx前端部署(Windows版-x86_64(AMD64)-离线)
前端·windows·nginx
用户54277848515401 天前
Nginx 为什么能进行静态资源托管
nginx
草莓熊Lotso1 天前
C++11 核心精髓:类新功能、lambda与包装器实战
开发语言·c++·人工智能·经验分享·后端·nginx·asp.net
元气满满-樱1 天前
Rewrite重写
linux·nginx