码迷,mamicode.com
首页 > 其他好文 > 详细

[转] nginx关于root与alias的区别

时间:2021-06-24 17:47:20      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:name   无效   方便   之一   title   需要   audio   cti   comment   

结论

配置demo:

location xxx {
    root yyy
}

浏览器访问 xxx,实际访问的是 yyy/xxx
浏览器访问 xxx/abc.html,实际访问的是 yyy/xxx/abc.html
浏览器访问 xxx/ccc/abc.html,实际访问的是 yyy/xxx/ccc/abc.html

结论: root属性,会把root的值(这里是yyy)加入到访问路径(locaition)之前

配置demo:

locaiton xxx {
    # alias必须以 / 结束,否则无效
    alias yyy/ 
}

浏览器访问 xxx,实际访问的是 yyy
浏览器访问 xxx/abc.html,实际访问的是 yyy/abc.html
浏览器访问 xxx/ccc/abc.html,实际访问的是 yyy/ccc/abc.html

结论:alias属性,会把alias的值(这里是yyy)替代访问路径匹配的部分(这里是xxx)

示例

nginx的目录结构如下:

nginx/
    -html/
        -index.html
    -logs/
        - access.log
    -conf/
        -nginx.conf

1) 这种配置,http://localhost:8086/access.log,能看到 nginx/logs/access.log,但就别指望能访问 html目录下的文档了

server {
    listen       8086;
    server_name  localhost;
    location / {
        root   logs;
    }
}

2) 这种配置,访问 http://localhost:8086/log/access.log,能看到 nginx/logs/access.log;
访问 http://localhost:8086/, 能看到 nginx/html/index.html

server {
    listen       8086;
    server_name  localhost;
    location / {
        root   html;
        index  index.html index.htm;
    }
    # 配置成 location /log/ 或 location /log 都可以
    location /log/ {
        # 不能写成logs, 必须已 / 结束
        alias logs/;
        # 以下配置没用也可以,只是方便你输入 localhost:8086/log/ 后能,看到nginx/logs/目录下的所有文件
        autoindex on;
    }
}

3) 这种配置,访问 http://localhost:8086/logs/access.log,能看到 nginx/logs/access.log;
访问 http://localhost:8086/, 能看到 nginx/html/index.html

server {
    listen       8086;
    server_name  localhost;
    # http://localhost:8086/ 访问的是
    # nginx/html/  (然后会自动显示 index.html 或 index.htm,如果存在这两个文件之一)
    # 啰嗦的注释: nginx/html(html是root的值)/(/是location的值)
    location / {
        root   html;
        index  index.html index.htm;
    }
    # http://localhost:8086/logs/ 访问的是
    # nginx/./logs/
    # .是root的值,logs是location的值
    # 请与第4种错误配置进行比较,深入理解root属性
    location /logs/ {
        # 写成./也可以
        root .;
    }
}

4) 错误的配置

server {
    listen       8086;
    server_name  localhost;
    location / {
        root   html;
        index  index.html index.htm;
    }
    # 这样子配置是错的, 请与第三种配置比较一下
    # 关键点:root属性会把root的值加入到最终路径之前
    # 即: http://localhost:8086/logs/access.log访问的是:
    # nginx/logs/logs/access.log
    # 因为: nginx/logs(root的值)/logs(locaition的值)/access.log,
    location /logs/ {
        root /logs/;
    }
}

节选:https://www.cnblogs.com/zhang... 这段话:
root属性指定的值是要加入到最终路径的,所以访问的位置变成了 root的值/locaiton的值。而我不想把访问的URI加入到路径中。所以就需要使用alias属性,其会抛弃URI,直接访问alias指定的位置

参考:
https://www.cnblogs.com/zhang...
https://www.cnblogs.com/kevin...

[转] nginx关于root与alias的区别

标签:name   无效   方便   之一   title   需要   audio   cti   comment   

原文地址:https://www.cnblogs.com/chris-oil/p/14924372.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!