ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

nginx中的alias和root辨析

2020-04-20 16:04:29  阅读:300  来源: 互联网

标签:http gif nginx alias w3 location root


Syntax:	root path;
Default:	
root html;
Context:	http, server, location, if in location
Sets the root directory for requests. For example, with the following configuration

访问 http://localhost:80/i/top.gif,nginx匹配的文件路径是/data/w3/i/top.gif

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.


Syntax:	alias path;
Default:	—
Context:	location
Defines a replacement for the specified location. For example, with the following configuration

访问http://localhost:80/i/top.gif,文件路径为/data/w3/images/top.gif
这里有个细节,nginx在解析root时会自动加上/,因此root可以省略斜杠,但alias路径结尾需要加上斜杠才能正确解析。

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;
}

nginx的官方文档:
http://nginx.org/en/docs/http/ngx_http_core_module.html#alias
http://nginx.org/en/docs/http/ngx_http_core_module.html#root

标签:http,gif,nginx,alias,w3,location,root
来源: https://www.cnblogs.com/fullee/p/12738323.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有