欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Nginx root&alias文件路径配置解析

程序员文章站 2024-02-21 10:49:40
...

Nginx root&alias文件路径配置解析

  • nginx在指定文件路径有两种方式root和alias,这两种的主要区别在于nginx如何解析location后面的uri,这会使两者分别以不同的方式请求映射到服务器的文件上。

1.root语法的使用

【root】
语法: root path
默认值: root html
配置段: http/server/location/if
例子:

location ^~/chen/ {
    root /data/www/www.chen.com;
    autoindex on;
    auto_basic "Restricted";
    auto_basic_user_file passwd/chen;
}

例子解析:
  如果请求的uri是/chen/httplogs/www.chen.com-access.log时,web服务器将会返回服务器上的“/data/www/www.chen.com”(root的path)+“/chen/httplogs/www.chen.com-access.log”的文件。也就是说root路径配置会根据完整的uri请求来映射,也就是/path/uri

2.alias语法的使用

【alias】
语法:alias path
配置段:location
例子:

location ^~ /binapp/ {
    limit_conn limit 4;
    limit_rate 200k;
    internal;
    alias /data/statics/bin/apps/;
}

例子解析:
  alias会把location后面配置的路径丢弃,把当前匹配到的目录指向到指定的目录。如果一个请求的uri是/binapp/a.chen.com/favicon时,web服务器将会返回服务器上的“/data/statics/bin/apps/”+“a.chen.com/favicon.html”的文件。

  alias使用总结:

  • 使用alias时,目录名后面一定要加“/”
  • alias可以指定任何名称
  • alias在使用正则表达式时,必须捕捉要匹配到的内容并在指定的内容处使用
  • alias只能位于location块中。