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

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

20200103193054943.png

官网说明

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



a135e09422514fa992f7eda09f2a5d75.png


点击 Module ngx_http_core_module


ae54939e4abb4ccb83c21f9dbf9cab81.png


a8b29bcc4c9c491aaa94e9c9bfdb8c5f.png


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中


location ^~ /abc/ {
  alias /www/artisan/html/new_abc/;
}

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

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

location ^~ /abc/ {
     root /www/artisan/html/;
}

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


alias (用alias场景居多)

2de68c34e9294492bde0e6f5e30e29b5.png

语法

Syntax: alias path;
Default:  —
Context:  location


Demo

Defines a replacement for the specified location.

For example, with the following configuration

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:

location ~ ^/users/(.+\.(?:gif|jpe?g|png))$ {
    alias /data/w3/images/$1;
}

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

location /images/ {
    alias /data/w3/images/;
}


it is better to use the root directive instead:

location /images/ {
    root /data/w3;
}


root


cc9d2009063a46c4b7beb6dfcaf8682b.png


语法


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


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.


实操


基本信息


环境:CentOS7

位置:/root/ng/artisan_ng

版本: nginx/1.22.0


需求

需求: 搞个图片or文档,能通过浏览器访问


步骤

我们假定把 这些资源放到 ng的 安装目录下的 document下


[root@VM-0-7-centos artisan_ng]# pwd
/root/ng/artisan_ng
# 新建document目录 
[root@VM-0-7-centos artisan_ng]# mkdir document
[root@VM-0-7-centos artisan_ng]#
[root@VM-0-7-centos artisan_ng]# cd document/
# 上传资源 
[root@VM-0-7-centos document]# ll
total 2876
-rw-r--r-- 1 root root 1912566 Oct  3 10:40  1.gif
-rw-r--r-- 1 root root  166244 Oct  3 01:25  1.jpg
-rw-r--r-- 1 root root  607643 Oct  3 01:26  1.pdf
-rwxrwxrwx 1 root root  246643 Oct  3 01:01  a.html
drwxrwxrwx 2 root root    4096 Oct  3 01:01 'Module ngx_http_core_module_files'
[root@VM-0-7-centos document]#

接下来我们去修改ng的配置

[root@VM-0-7-centos document]# vim ../conf/nginx.conf


配置如下

25258c6eb367409eb797109fd700bf55.png


alias后面的path ,也可以配置绝对路径 。 如果配置的是相对路径的话,则以ng的安装目录为准。

重载nginx

[root@VM-0-7-centos document]# ../sbin/nginx -s reload
[root@VM-0-7-centos document]#
[root@VM-0-7-centos document]#


访问 http://ip:9999/a.html


11f2a33e63cf4e1b8ce7bafc7c12fd10.png



问题

11f2a33e63cf4e1b8ce7bafc7c12fd10.png


这有啥办法优化么 ?

相关文章
|
4月前
|
缓存 负载均衡 JavaScript
Nginx:高性能Web服务器与反向代理利器
Nginx:高性能Web服务器与反向代理利器
286 110
|
4月前
|
负载均衡 Cloud Native 前端开发
Nginx:高性能的Web服务器与反向代理利器
Nginx:高性能的Web服务器与反向代理利器
211 100
|
4月前
|
缓存 负载均衡 前端开发
Nginx:高性能Web服务器的核心力量
Nginx:高性能Web服务器的核心力量
238 100
|
4月前
|
缓存 负载均衡 前端开发
Nginx:高性能的Web服务器与反向代理利器
Nginx:高性能的Web服务器与反向代理利器
260 99
|
4月前
|
负载均衡 前端开发 安全
Nginx:高性能的Web服务器与反向代理利器
Nginx:高性能的Web服务器与反向代理利器
241 98
|
4月前
|
缓存 负载均衡 前端开发
Nginx:高性能Web服务器的核心引擎
Nginx:高性能Web服务器的核心引擎
242 99
|
4月前
|
缓存 负载均衡 前端开发
Nginx:高性能Web服务器的核心引擎
Nginx:高性能Web服务器的核心引擎
180 47
|
3月前
|
Ubuntu 安全 应用服务中间件
详细指南:配置Nginx服务器在Ubuntu平台上
以上步骤涵盖了基本流程:从软件包管理器获取 Ngnix, 设置系统服务, 调整UFW规则, 创建并激活服务器块(也称作虚拟主机), 并进行了初步优化与加固措施。这些操作都是建立在命令行界面上,并假设用户具有必要权限(通常是root用户)来执行这些命令。每个操作都有其特定原因:例如,设置开机启动确保了即使重启后也能自动运行 Ngnix;而编辑server block则定义了如何处理进入特定域名请求等等。
272 18
|
3月前
|
Ubuntu 安全 应用服务中间件
详细指南:配置Nginx服务器在Ubuntu平台上
以上步骤涵盖了基本流程:从软件包管理器获取 Ngnix, 设置系统服务, 调整UFW规则, 创建并激活服务器块(也称作虚拟主机), 并进行了初步优化与加固措施。这些操作都是建立在命令行界面上,并假设用户具有必要权限(通常是root用户)来执行这些命令。每个操作都有其特定原因:例如,设置开机启动确保了即使重启后也能自动运行 Ngnix;而编辑server block则定义了如何处理进入特定域名请求等等。
339 17
|
9月前
|
移动开发 数据挖掘 开发者
服务器发送事件(SSE)在现代Web开发中的关键作用
服务器发送事件(SSE)是HTML5标准协议,用于服务器主动向客户端推送实时数据,适合单向通信场景。相比WebSocket,SSE更简洁高效,基于HTTP协议,具备自动重连、事件驱动等特性。常见应用场景包括实时通知、新闻推送、数据分析等。通过Apipost等工具可轻松调试SSE,助力开发者构建高效实时Web应用。示例中,电商平台利用SSE实现秒杀活动通知,显著减少延迟并简化架构。掌握SSE技术,能大幅提升用户体验与开发效率。