zoukankan      html  css  js  c++  java
  • [转] nginx关于root与alias的区别

    结论

    配置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...

  • 相关阅读:
    函数式编程(三元运算、文件操作、函数、装饰器)
    开发基础(练习题)
    开发基础(字符串操作、元祖、元组、Hash、字典、集合、字符编码转换)
    开发基础(字符编码、列表操作)
    开发基础 (变量、数据类型、格式化输出、运算符、流程控制、while循环)
    [LeetCode] 127. 单词接龙
    [LeetCode] 126. 单词接龙 II
    [LeetCode] 122. 买卖股票的最佳时机 II
    [LeetCode] 124. 二叉树中的最大路径和
    [LeetCode] 125. 验证回文串
  • 原文地址:https://www.cnblogs.com/chris-oil/p/14924372.html
Copyright © 2011-2022 走看看