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的区别详解的更多相关文章

  1. nginx中root和alias的区别

    nginx中root和alias的区别    

  2. 配置 nginx 访问资源目录,nginx配置 root 与 alias 的区别

    比如说想要把 /home/source 目录作为资源目录,那么需要如下配置: location /source/ { #识别url路径后,nginx会到/home/文件路径下,去匹配/source r ...

  3. Web服务器之Nginx详解(操作部分)

    大纲 一.前言 二.Nginx 安装与配置 三.Nginx 配置文件详解 四.Nginx 命令参数 五.配置Nginx提供Web服务 六.配置Nginx的虚拟主机 七.配置Nginx的用户认证 八.配 ...

  4. nginx代理配置 配置中的静态资源配置,root 和 alias的区别。启动注意事项

    这篇主要内容是:nginx代理配置 配置中的静态资源配置,root 和 alias的区别.启动注意事项! 为什么会在window上配置了nginx呢?最近我们的项目是静态资源单独放在一个工程里面,后端 ...

  5. nginx代理配置 配置中的静态资源配置,root 和 alias的区别

    这篇主要内容是:nginx代理配置 配置中的静态资源配置,root 和 alias的区别.启动注意事项! 为什么会在window上配置了nginx呢?最近我们的项目是静态资源单独放在一个工程里面,后端 ...

  6. Nginx中root与alias的用法及区别:

    Nginx中root与alias都是定义location {}块中虚拟目录访问的文件位置: 先看看两者在用法上的区别: location /img/ { alias /var/www/image/; ...

  7. nginx中root与alias关键字的区别

    前言 近段时间秋招上岸了,于是每天疯狂补各种分布式基础,每天都在痛苦与快乐中度过. 在学习 nginx 的时候,遇到配置上的问题:root 与 alias 的区别,卡了大概三个小时,记录下来警醒自己不 ...

  8. nginx 中 root和alias

    根本区别 一个请求的url= http://ip:port/path 在location中配置root和alias的区别: root是在location的正则之前拼接了路径 alias是在locati ...

  9. web缓存服务器varnish-4.1.6的部署及配置详解

    web缓存服务器varnish-4.1.6的部署及配置详解 1.安装varnish4.1.6安装依赖 yum install -y autoconf automake jemalloc-devel l ...

  10. [转帖]Nginx服务器的六种负载均衡策略详解

    Nginx服务器的六种负载均衡策略详解 咔咔侃技术 2019-09-11 17:40:12 一.关于Nginx的负载均衡 在服务器集群中,Nginx起到一个代理服务器的角色(即反向代理),为了避免单独 ...

随机推荐

  1. 修改debian apt搜索的软件包颜色(原本是绿色)

    sudo nano /etc/apt/apt.conf 加入以下内容 apt::color::highlight "#"; 再搜索软件包会变成白色 不足之处是包的前面会加上#号

  2. Android开发 活动activity

    一.关于Activity 关于Activity必须要了解的内容有:Activity的生命周期.android任务栈.Activity启动模式.scheme跳转协议. 1.1  什么是Activity ...

  3. 5W1H聊开源之Why——为什么要参与开源?

    中国开源的发展速度发展加快,个人和组织对于为开源作贡献有着前所未有的激情.据<2020年IT行业项目管理调查报告>,约四成受访者以自己开发开源项目.为他人提交项目代码.作为成员开发维护项目 ...

  4. 5 JavaScript变量提升

    5 变量提升 看以下代码, 或多或少会有些问题的. function fn(){ console.log(name); var name = '大马猴'; } fn() 发现问题了么. 这么写代码, ...

  5. #根号分治,树形dp#CF1039D You Are Given a Tree

    题目 给定一棵树,对于 \(k\in [1,n]\) 问最多可以分成多少段长度为 \(k\) 的不交路径 分析 首先考虑对于单个 \(k\) 怎么做. 设 \(dp[x]\) 表示点 \(x\) 往下 ...

  6. #动态规划,组合计数,树状数组,前缀和#F 简单计数题&K 最简单的题

    先膜两位出题人 F 简单计数题 题目 有\(n\)个活动,预约期有\(k\)天,第\(j\)天YC可以获得\(a_j(1\leq a_j\leq n)\)张预约券, 他会在\(n\)个活动中等概率选择 ...

  7. 在typescript中,Omit是什么意思

    在TypeScript中,Omit<Type, Keys> 是一个工具类型(utility type),它用于创建一个新的类型,这个新类型是从现有类型(Type)中排除了某些指定的属性(K ...

  8. 你知道什么叫做API、SDK吗?

    链接:https://www.zhihu.com/question/21691705/answer/770586138 API.SDK是什么......... 讲个小故事: 研发人员A开发了软件A,研 ...

  9. ArkUI新能力,助力应用开发更便捷

    原文链接:https://mp.weixin.qq.com/s/TAuq1WC6435ebn6L61rZAA,点击链接查看更多技术内容:    ArkUI是一套构建分布式应用的声明式UI开发框架.它具 ...

  10. 手把手教你基于gin从零搭建一个属于你自己的go项目(一)

    一.为什么写这个,适合什么人看 原因 因为自己想写点小玩意,本来是打算用egg.js来写服务端的,后来发现了个更好玩的midway,但是后来发现自己手上的服务器都是一核2g的小水管,用node有点难顶 ...