重识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


这有啥办法优化么 ?

相关文章
|
10月前
|
中间件 Java 应用服务中间件
Windows部署web应用服务器Jboss中间件
如何在Windows系统上部署JBoss 7.1作为Web应用服务器,包括配置环境变量、自动部署WAR包、访问JBoss控制台、设置管理员账户以及修改端口和绑定地址等操作。
299 1
|
4月前
|
应用服务中间件 PHP nginx
当你的nginx服务器和php服务器不在一起的时候,这个nginx 的root目录问题
两个服务器的网站代码目录需要对齐,docker容器里面也是一样
|
10月前
|
缓存 NoSQL 数据库
高性能Web服务器架构设计
【8月更文第28天】在当今互联网时代,网站的响应速度直接影响用户体验和业务成功率。因此,构建一个高性能的Web服务器架构至关重要。本文将从硬件配置、软件架构以及网络设置三个方面探讨如何提高Web服务器的性能,并提供一些实际的代码示例。
521 0
|
7月前
|
XML 前端开发 JavaScript
PHP与Ajax在Web开发中的交互技术。PHP作为服务器端脚本语言,处理数据和业务逻辑
本文深入探讨了PHP与Ajax在Web开发中的交互技术。PHP作为服务器端脚本语言,处理数据和业务逻辑;Ajax则通过异步请求实现页面无刷新更新。文中详细介绍了两者的工作原理、数据传输格式选择、具体实现方法及实际应用案例,如实时数据更新、表单验证与提交、动态加载内容等。同时,针对跨域问题、数据安全与性能优化提出了建议。总结指出,PHP与Ajax的结合能显著提升Web应用的效率和用户体验。
159 3
|
7月前
|
应用服务中间件 nginx
Nginx里的root和alias的区别是什么?
Nginx里的root和alias的区别是什么?
611 2
|
8月前
|
Java PHP
PHP作为广受青睐的服务器端脚本语言,在Web开发中占据重要地位。理解其垃圾回收机制有助于开发高效稳定的PHP应用。
【10月更文挑战第1天】PHP作为广受青睐的服务器端脚本语言,在Web开发中占据重要地位。其垃圾回收机制包括引用计数与循环垃圾回收,对提升应用性能和稳定性至关重要。本文通过具体案例分析,详细探讨PHP垃圾回收机制的工作原理,特别是如何解决循环引用问题。在PHP 8中,垃圾回收机制得到进一步优化,提高了效率和准确性。理解这些机制有助于开发高效稳定的PHP应用。
93 3
|
7月前
|
JavaScript 前端开发 Java
SpringBoot_web开发-webjars&静态资源映射规则
https://www.91chuli.com/ 举例:jquery前端框架
100 0
|
10月前
|
JavaScript 搜索推荐 前端开发
从零搭建到部署:Angular与Angular Universal手把手教你实现服务器端渲染(SSR),全面解析及实战指南助你提升Web应用性能与SEO优化效果
【8月更文挑战第31天】服务器端渲染(SSR)是现代Web开发的关键技术,能显著提升SEO效果及首屏加载速度,改善用户体验。Angular Universal作为官方SSR解决方案,允许在服务器端生成静态HTML文件。本文通过具体示例详细介绍如何使用Angular Universal实现SSR,并分享最佳实践。首先需安装Node.js和npm。
325 1
|
10月前
|
Ubuntu 应用服务中间件 Linux
在Linux中,如何配置Web服务器(如Apache或Nginx)?
在Linux中,如何配置Web服务器(如Apache或Nginx)?
|
10月前
|
API C# 开发框架
WPF与Web服务集成大揭秘:手把手教你调用RESTful API,客户端与服务器端优劣对比全解析!
【8月更文挑战第31天】在现代软件开发中,WPF 和 Web 服务各具特色。WPF 以其出色的界面展示能力受到欢迎,而 Web 服务则凭借跨平台和易维护性在互联网应用中占有一席之地。本文探讨了 WPF 如何通过 HttpClient 类调用 RESTful API,并展示了基于 ASP.NET Core 的 Web 服务如何实现同样的功能。通过对比分析,揭示了两者各自的优缺点:WPF 客户端直接处理数据,减轻服务器负担,但需处理网络异常;Web 服务则能利用服务器端功能如缓存和权限验证,但可能增加服务器负载。希望本文能帮助开发者根据具体需求选择合适的技术方案。
510 0