关于 nginx 中 location 下的 alias 配置的问题?-灵析社区

MastFancy

如果配置是 server { listen 8002; location /about/ { alias html; index index.html; } } 访问 [http://localhost:8002/about/](https://link.segmentfault.com/?enc=a2Oo%2FArFInFpEKu7KClCjg%3D%3D.cqiVGB2KY8sEgQizzMKK%2FWWDthPm%2FtrGTuu%2Fpv1GqP8%3D) 会返回 403 禁止 server { listen 8002; location /about/ { alias html/; index index.html; } } 访问 [http://localhost:8002/about/](https://link.segmentfault.com/?enc=gxQ4hj4zQqAgRs9%2Fk83YzA%3D%3D.uHQ3fU8DLTHSkGnb9DY1hnQla1RTnKAp26Os9tWdXcU%3D) 会返回 html 目录下的 index.html 文件,这是符合预期的 server { listen 8002; location ~ /about/ { alias html/; index index.html; } } 访问 [http://localhost:8002/about/](https://link.segmentfault.com/?enc=kEa3KzQgy922eFTP70Byjw%3D%3D.TJWUnfwrOahiF2NhbcRuW2aaFFZASk53Xz6Icx%2FyIfU%3D) 会不断进行重定向生成 [http://localhost:8002/about/index.html/index.html/...../index...](https://link.segmentfault.com/?enc=5f%2Fck4P79D%2Fr8uCip26Y1Q%3D%3D.1qEPXiUzEcgr3KPFJmJprdi%2FyWTTy%2BuBj3Lxl75dS4ZbOz76xs0WEkGH7Z3oPzOkPAimwxhADehhmE6ZLdvN9WyAtneG9dVZIEvz8LlhyGI%3D) 直到长度超过限制而访问出错。 为什么会出现第一和第三种的情况?

阅读量:211

点赞量:0

问AI
第一种,当访问 /about 时,他会去找文件 "htmlindex.html" 而不是预期的 "html/index.html"。 (很多文章都说要加上尾斜杠,实际也确实如此,但是 nginx 的官方手册中似乎没有提及。不过给出的示例里面都是有的) 第三种,按照手册上的说法,你应该使用捕获组。 «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; }»» * "Module ngx_http_core_module" (https://link.segmentfault.com/?enc=dy6my4JwLll9s3GWDbUvBA%3D%3D.HmaSd6lXg9wiCjBPD3lvTe2rAxe7qAdUq4umQz4AJ4UArvl0dSEdGiXUzuR83zwn7UvhyjondUlrI2kpo2e35w%3D%3D)