如果配置是
server {
listen 8002;
location /about/ {
alias html;
index index.html;
}
}
访问 http://localhost:8002/about/ 会返回 403 禁止
server {
listen 8002;
location /about/ {
alias html/;
index index.html;
}
}
访问 http://localhost:8002/about/ 会返回 html 目录下的 index.html 文件,这是符合预期的
server {
listen 8002;
location ~ /about/ {
alias html/;
index index.html;
}
}
访问 http://localhost:8002/about/ 会不断进行重定向生成 http://localhost:8002/about/index.html/index.html/...../index... 直到长度超过限制而访问出错。
为什么会出现第一和第三种的情况?
第一种,当访问 /about 时,他会去找文件
htmlindex.html
而不是预期的html/index.html
。(很多文章都说要加上尾斜杠,实际也确实如此,但是 nginx 的官方手册中似乎没有提及。不过给出的示例里面都是有的)
第三种,按照手册上的说法,你应该使用捕获组。