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起到一个代理服务器的角色(即反向代理),为了避免单独 ...
随机推荐
- 双向循环链表(DoubleLoopLinkList)
双向循环链表 关于双向循环链表可以先阅读这篇文章这里就不再赘述:双向链表(DoubleLinkList) Node template<typename T> class Node { pu ...
- #矩阵乘法#洛谷 5343 【XR-1】分块
题目 分析 考虑dp,\(dp[i]=\sum dp[i-j]\) 既然\(j\)很小,那么这显然可以用矩阵乘法优化 代码 #include <cstdio> #include <c ...
- Go 中的格式化字符串`fmt.Sprintf()` 和 `fmt.Printf()`
在 Go 中,可以使用 fmt.Sprintf() 和 fmt.Printf() 函数来格式化字符串,这两个函数类似于 C 语言中的 scanf 和 printf 函数. fmt.Sprintf() ...
- Python 列表操作指南2
将元组的元素添加到列表中: thislist = ["apple", "banana", "cherry"] thistuple = (&q ...
- 使用IDEA直接连接数据库报错:Server returns invalid timezone. Go to 'Advanced' tab and set 'serverTimezone' property manually.
错误详情:使用IDEA直接连接数据库报错:Server returns invalid timezone. Go to 'Advanced' tab and set 'serverTimezone' ...
- HTMLTestRunner测试报告中点击 view 按钮没反应
背景 HTMLTestRunner 生成测试报告后,发现点击 view 这个按钮一直没有反应 通过 F12 开发人员工具检查,发现是 jQuery 文件没有加载出来 解决方法 我采用的解决方法是直接 ...
- Spring的事务管理方式编程式和声名式
spring的事务管理方式: 一.声名式 二.编程式 事务:查询不会影响数据的改变,对于增删改必须进行事务的管理.如果没有事务管理spring也提供了默认传播方式REQUIRED 一.声名式事务管理( ...
- leetcode-数组中两元素的最大乘积
题目描述 给你一个整数数组 nums,请你选择数组的两个不同下标 i 和 j,使 (nums[i]-1)*(nums[j]-1) 取得最大值. 请你计算并返回该式的最大值. 示例 1: 输入:nums ...
- 玩转HarmonyOS专项测试,轻松上架“五星”高品质应用
作者:David,华为测试服务专家 随着信息技术的高速发展,移动应用与人们生活日益紧密,面向各类场景的应用层出不穷,什么样的应用更受用户青睐呢?在满足用户功能需求之上,一个好的应用要能运行稳定.流 ...
- CentOS6.4中yum命令安装php5.2.17[转载未亲测]
最近给公司部署服务器的时候发现他们提供的服务器是centos6.4系统的,装好系统和相关服务httpd,mysql,php,一跑代码,发现php5.3中的zend加密不能用,安装Zend Guard ...