WEB服务与NGINX(5)- root和alias的区别详解
root和alias的区别
root:指定站点家目录,给定的路径对应于location中的/uri 左侧的/,文件的绝对路径为root+location。
支持环境:http, server, location, if in location
#1.nginx的配置文件如下:
[root@nginx01 ~]# vim /etc/nginx/conf.d/virtualhost.conf
server {
listen 80;
server_name www.nginx01.com;
location / {
root /data/nginx/html/web1;
index index.html;
}
location /images {
root /data/nginx/html/web1/pictures;
index index.html;
}
} #2.nginx的目录结构:
[root@nginx01 ~]# cd /data/nginx/html/web1/
[root@nginx01 web1]# mkdir pictures
[root@nginx01 web1]# echo "jpg" > pictures/test.html #3.重启nginx
[root@nginx01 web1]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@nginx01 web1]# systemctl reload nginx.service #4.此时客户端请求下面的资源时会报404错误,因为在web1下没有images目录
[root@xuzhichao ~]# curl http://www.nginx01.com/images/test.html
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html> #5.如果在pictures下新建一个images目录,把test.html放在images目录下,则可以正常访问
[root@nginx01 web1]# mkdir pictures/images
[root@nginx01 web1]# mv pictures/test.html pictures/images/
[root@nginx01 web1]# tree pictures/
pictures/
└── images
└── test.html [root@xuzhichao ~]# curl http://www.nginx01.com/images/test.html
jpg
alias:定义路径别名,会把访问的路径重新定义到期指定的路径
注意:在使用alias的时候,location后的uri后面如果加了斜杠,则下面alias定义的路径也必选加斜杠,否则会提示403
仅能用于location上下文
#1.nginx的配置如下
[root@nginx01 web1]# vim /etc/nginx/conf.d/virtualhost.conf
server {
listen 80;
server_name www.nginx01.com;
location / {
root /data/nginx/html/web1;
index index.html;
}
location /images {
alias /data/nginx/html/web1/pictures;
index index.html;
}
} #2.重启nginx进程
[root@nginx01 web1]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@nginx01 web1]# systemctl reload nginx.service #3.web1下的目录文件如下:
[root@nginx01 web1]# tree pictures/
pictures/
└── images
└── test.html #4.此时客户端请求下面的资源时会报404错误,因为在web1下没有images目录
[root@xuzhichao ~]# curl http://www.nginx01.com/images/test.html
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html> #5.如把web1的目录结构改成下面的,则客户端可以正常访问
[root@nginx01 web1]# tree .
.
├── index.html
└── pictures
└── test.html [root@xuzhichao ~]# curl http://www.nginx01.com/images/test.html
jpg
WEB服务与NGINX(5)- root和alias的区别详解的更多相关文章
- nginx中root和alias的区别
nginx中root和alias的区别
- 配置 nginx 访问资源目录,nginx配置 root 与 alias 的区别
比如说想要把 /home/source 目录作为资源目录,那么需要如下配置: location /source/ { #识别url路径后,nginx会到/home/文件路径下,去匹配/source r ...
- Web服务器之Nginx详解(操作部分)
大纲 一.前言 二.Nginx 安装与配置 三.Nginx 配置文件详解 四.Nginx 命令参数 五.配置Nginx提供Web服务 六.配置Nginx的虚拟主机 七.配置Nginx的用户认证 八.配 ...
- nginx代理配置 配置中的静态资源配置,root 和 alias的区别。启动注意事项
这篇主要内容是:nginx代理配置 配置中的静态资源配置,root 和 alias的区别.启动注意事项! 为什么会在window上配置了nginx呢?最近我们的项目是静态资源单独放在一个工程里面,后端 ...
- nginx代理配置 配置中的静态资源配置,root 和 alias的区别
这篇主要内容是:nginx代理配置 配置中的静态资源配置,root 和 alias的区别.启动注意事项! 为什么会在window上配置了nginx呢?最近我们的项目是静态资源单独放在一个工程里面,后端 ...
- Nginx中root与alias的用法及区别:
Nginx中root与alias都是定义location {}块中虚拟目录访问的文件位置: 先看看两者在用法上的区别: location /img/ { alias /var/www/image/; ...
- nginx中root与alias关键字的区别
前言 近段时间秋招上岸了,于是每天疯狂补各种分布式基础,每天都在痛苦与快乐中度过. 在学习 nginx 的时候,遇到配置上的问题:root 与 alias 的区别,卡了大概三个小时,记录下来警醒自己不 ...
- nginx 中 root和alias
根本区别 一个请求的url= http://ip:port/path 在location中配置root和alias的区别: root是在location的正则之前拼接了路径 alias是在locati ...
- web缓存服务器varnish-4.1.6的部署及配置详解
web缓存服务器varnish-4.1.6的部署及配置详解 1.安装varnish4.1.6安装依赖 yum install -y autoconf automake jemalloc-devel l ...
- [转帖]Nginx服务器的六种负载均衡策略详解
Nginx服务器的六种负载均衡策略详解 咔咔侃技术 2019-09-11 17:40:12 一.关于Nginx的负载均衡 在服务器集群中,Nginx起到一个代理服务器的角色(即反向代理),为了避免单独 ...
随机推荐
- 【已解决】ERROR: but there is no HDFS_NAMENODE_USER defined. Aborting operation. Starting datanodes
export HDFS_NAMENODE_USER=rootexport HDFS_DATANODE_USER=rootexport HDFS_SECONDARYNAMENODE_USER=roote ...
- #树链剖分,线段树#洛谷 2146 [NOI2015]软件包管理器
题目传送门 分析 安装时1到\(x\)路径上都变为1,删除时\(x\)的子树都变为0, 显然可以用树链剖分+线段树实现 代码 #include <cstdio> #include < ...
- #分治,决策单调性dp#CF868F Yet Another Minimization Problem
题目 给定一个序列 \(a\),要把它分成 \(k\) 个子段.(\(n\leq 10^5,k\leq 20\)) 每个子段的费用是其中相同元素的对数.求所有子段的费用之和的最小值. 分析 有一个很明 ...
- Python 集合(Sets)1
集合 集合用于在单个变量中存储多个项.集合是 Python 中的 4 种内置数据类型之一,用于存储数据集合,其他 3 种是列表(List).元组(Tuple)和字典(Dictionary),它们都具有 ...
- 【FAQ】HarmonyOS SDK 闭源开放能力 —IAP Kit
1.问题描述 根据https://developer.huawei.com/consumer/cn/doc/harmonyos-references/iap-data-model-0000001736 ...
- 财务人提高竞争力必备的技能,怎么能少了ta!
从近年来大数据技术的发展趋势和相关产业飞速发展的状态,可以看出当前"数据分析"的热度可以说是有增无减,而且从市场上对数据分析人才的需求缺口也可以看出企业对数据分析的重视程度.未来随 ...
- 机器学习&深度学习 操作tips
1. 在运行程序时,报错如下: usage: run.py [-h] --model MODEL [--embedding EMBEDDING] [--word WORD] run.py: error ...
- nginx重新整理——————编译nginx[二]
前言 简单编译一下nginx. 正文 为什么我们要去编译nginx. 系统安装,比如yum安装,会把nginx 模块直接编译进来. 这意味着,我们无法使用第三方的包.如果我们需要使用第三方包,那么需要 ...
- 前端之多线程 ---webworker
一.啥是workerJavaScript为单线程,worker则为JavaScript创建多线程环境.使用场景如:计算文件hash,计算大于1G的文件hash过程是很慢的,但由于要将hash传给后端, ...
- Django框架——cookie与session简介、django操作cookie与session、django中间件
cookie与session简介 """ 回忆:HTTP协议四大特性 1.基于请求响应 2.基于TCP.IP作用于应用层之上的协议 3.无状态 不保存客户端的状态 4.无 ...