nginx配置中,同一个server下配置多个location
今天新接触了layui,想试试她数据表格的渲染,所以就简单配置了一下nginx。因为之前有一个citrus的location配置项,我觉得nginx可以配置多个,所以直接配置了下面这个代码。
server {
listen 80;
server_name localhost;
location /{
root D:/XM/citrus;
index index.html index.htm;
}
location /layui{
root D:/XM/layui;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location /citrus{
proxy_pass http://192.168.1.2:7778/; #本地后端端口
}
}
但是在我访问:127.0.0.1/layui后,nginx直接给我了个403错误:
403 Forbidden
查看日志文件:
[error] : *24 directory index of "D:/XM/layui/layui/" is forbidden, client: 127.0.0.1, server: localhost, request: "GET /layui/ HTTP/1.1", host: "127.0.0.1"
发现这个路径有问题,它多了一个layui。
经过多方查找,找到了错误原因,在我配置layui的路径时,不应该使用root,而是要用alias,所以将layui的配置改成如下:
location /layui{
alias D:/XM/layui;
index index.html index.htm;
}
补充说明:
root:root路径+location路径
alias:alias路径替换location路径