点击 Module ngx_http_core_module
root与alias主要区别在于nginx如何解释location后面的uri, 分别以不同的方式将请求映射到服务器文件上。
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路径
Syntax: alias path;
Default: —
Context: location
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;
}
Syntax: root path;
Default:
root html;
Context: http, server, location, if in location
注意Contex的范围
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.