• Robert小七
    2019-02-14
    为什么我访问/alias 不是"/alias/"时,会返回301永久重定向

    作者回复: 如果你的URI与root/alias映射后对应的是目录,但是URI却没有以/结尾,就会返回301重定向,并在Location中把URI加上/结尾

    
     4
  • here_always
    2019-10-15
    老师请问docker nginx镜像怎么添加静态模块啊?要自己写dockerfile 自己编译进去吗?官方nginx的镜像文档什么都没说...

    作者回复: 如果要定制模块,建议不要用docker镜像,docker镜像只保持运行必要的软件,而编译需要依赖许多软件,安装非常麻烦。

    
    
  • leitiannet
    2019-01-31
    无法在留言上继续回复...
    对于包含正则表达式部分还是有点没明白
    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;
    }
    如果location不使用匹配组,或者alias不引用匹配组会有什么效果?是否会报错?
    展开

    作者回复: 你是指这个例子吗?例子中的配置反推其业务场景,可以得到:
    1、这个location只能匹配/user/,要做前缀匹配URI。
    2、前缀匹配完成后还不行,可能除了图片还有其他文件,这里只想对图片做处理。
    3、需要从URI里取出图片的文件名,作为映射磁盘文件的规则。
    针对以上三点,使用正则表达式可以很好的完成第2、3条(参见第54课),使用alias可以很好的忽略掉/users/这部分URI。

    
    
  • leitiannet
    2019-01-30
    问题1:root中指定根目录时加不加/都可以,alias指定目录时必须要加/吗?
    location /i/ {
        root /data/w3;
        #root /data/w3/;

    }

    location /i/ {
        alias /data/w3/images/;
    }

    问题2:alias指令的说明
    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;
    }

    1,alias中使用变量一般什么情况会使用?
    2,如果location不使用匹配组,或者alias不引用匹配组会有什么效果?是否会报错?


    展开

    作者回复: 1、alias通常需要加/的原因是,location /i/这种配置最后带了/,所以拼接文件路径时已经把URL中/i/给移除了,少了一个/。如果你配置的是location /i就不需要。
    2、当你的访问路径是由不同的请求内容决定的,例如HTTP头部、URL参数等,可以用变量。

    
    
  • Mr.zhang
    2019-01-21
    老师,我这四个location的特殊之处是:
    1 有的加了斜杠/,有的没有加(包括alias指令后面跟的路径)
    2 四个location都用的是 ~ 正则匹配,没有用前缀匹配
    然后是,我访问的是同一个url。

    作者回复: 这次看明白了:-)
    1、
    location ~ /download/ {
    alias /usr/local/mywork/test/html;
    }
    有明显问题,例如访问/download/web/1.html 实际会访问/usr/local/mywork/test/htmlweb/1.html,肯定不存在。
    2、2和4无限重定向的原因,可能是你的访问URL,在1.html后还有一个/所致。你把完整的配置,以及error.log日志发我邮件看看:russelltao@foxmail.com

    
    
  • Mr.zhang
    2019-01-15
    老师,我遇到一个很奇怪的问题:
        location ~ /download/ {
            alias /usr/local/mywork/test/html;
        }
        location ~ /download/ {
            alias /usr/local/mywork/test/html/;
        }
        location ~ /download {
            alias /usr/local/mywork/test/html;
        }
        location ~ /download {
            alias /usr/local/mywork/test/html/;
        }
    访问:
    1 www.test.com/download/web/1.html
    2 www.test.com/download/web/1.html
    3 www.test.com/download/web/1.html
    4 www.test.com/download/web/1.html
    得到的结果分别是:
    1和3: 403错误:日志显示 directory index of "/usr/local/mywork/test/html" is forbidden,request: "GET /download5/zhangbao/1.html/ HTTP/1.1"
    2和4: 301无限重定向:浏览器地址栏目是:http://test.com/download/web/1.html/index.html/index.html/index.html/index.html/index.html/index.html/index.html/index.html/index.html/index.html/index.html/index.html/index.html/index.html/index.html/index.html/index.html/index.html/

    而在测试测试location /download/ {}这种类型的是没有这种现象的。
    展开

    作者回复: 我反复对比你列出的4个location和4个访问url,居然是完全一样的,是不是复制粘贴时出错啦??

    
    
  • 我爱北京天安门
    2019-01-09
    老师好给力啊 更新好多 追追追!

    作者回复: :-)

    
    
  • Panda
    2018-12-30
    root 会带上 root 后面的目录 alias 不会带上 alias后面的目录 对么

    作者回复: 不是,主要是对所属location后url的处理上。

    
    
我们在线,来聊聊吧