前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >重识Nginx - 06 搭建静态资源Web服务器(alias VS root)

重识Nginx - 06 搭建静态资源Web服务器(alias VS root)

作者头像
小小工匠
发布2022-10-04 17:25:23
1.4K0
发布2022-10-04 17:25:23
举报
文章被收录于专栏:小工匠聊架构

文章目录

在这里插入图片描述
在这里插入图片描述

官网说明

https://nginx.org/en/docs/

在这里插入图片描述
在这里插入图片描述

点击 Module ngx_http_core_module

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

root vs alias

root与alias主要区别在于nginx如何解释location后面的uri, 分别以不同的方式将请求映射到服务器文件上。

  • alias是一个目录别名的定义(仅能用于location上下文)
  • root则是最上层目录的定义。
  • root的处理结果是:root路径+location路径 ; alias的处理结果是:使用alias路径替换location路径
  • alias后面必须要用“/”结束,否则会找不到文件, root则可有可无~~
  • 使用alias时,目录名后面一定要加"/"
  • alias在使用正则匹配时,必须捕捉要匹配的内容并在指定的内容处使用。
  • alias只能位于location块中, root可以不放在location中
在这里插入图片描述
在这里插入图片描述

代码语言:javascript
复制
location ^~ /abc/ {
	alias /www/artisan/html/new_abc/;
}

如果一个请求的URI是/abc/a.html时, 将会返回服务器的/www/artisan/html/new_abc/a.html

注意这里是new_abc, alias会把location后面配置的路径丢弃掉,把当前匹配到的目录指向到指定的目录


代码语言:javascript
复制
location ^~ /abc/ {
     root /www/artisan/html/;
}

如果一个请求的URI是/abc/a.html时,web服务器将会返回服务器上的/www/artisan/html/abc/a.html的文件。 root会把location配置的路径进行追加----> root路径+location路径

在这里插入图片描述
在这里插入图片描述

alias (用alias场景居多)

在这里插入图片描述
在这里插入图片描述

语法

代码语言:javascript
复制
Syntax:	alias path;
Default:	—
Context:	location

Demo

Defines a replacement for the specified location.

For example, with the following configuration

代码语言:javascript
复制
location /i/ {
    alias /data/w3/images/;
}

on request of “/i/top.gif”, the file /data/w3/images/top.gif will be sent.

The path value can contain variables, except document_root and realpath_root.

If alias is used inside a location defined with a regular expression then such regular expression should contain captures and alias should refer to these captures (0.7.40), for example:

代码语言:javascript
复制
location ~ ^/users/(.+\.(?:gif|jpe?g|png))$ {
    alias /data/w3/images/$1;
}

When location matches the last part of the directive’s value:

代码语言:javascript
复制
location /images/ {
    alias /data/w3/images/;
}

it is better to use the root directive instead:

代码语言:javascript
复制
location /images/ {
    root /data/w3;
}

root

在这里插入图片描述
在这里插入图片描述

语法

代码语言:javascript
复制
Syntax:	root path;
Default:	
root html;
Context:	http, server, location, if in location

注意Contex的范围


Demo

Sets the root directory for requests.

For example, with the following configuration

代码语言:javascript
复制
location /i/ {
    root /data/w3;
}

The /data/w3/i/top.gif file will be sent in response to the “/i/top.gif” request.

The path value can contain variables, except document_root and realpath_root.

A path to the file is constructed by merely adding a URI to the value of the root directive. If a URI has to be modified, the alias directive should be used.

在这里插入图片描述
在这里插入图片描述
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-10-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 官网说明
  • root vs alias
  • alias (用alias场景居多)
    • 语法
      • Demo
      • root
        • 语法
          • Demo
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档