Nginx 虚拟目录(root 与 alias)的区别
文章目录
Nginx 虚拟目录(root 与 alias)的区别
出现场景
有如下前端目录结构
front
-common
-dist
-doc
-node_moduels
-package.json
-READDE.md
-src
真正的web目录应为dist目录
url访问路径:http://localhost/front/#module
之前错误的配置如下
location /front
{
index index.html;
root /home/www/front/dist/;
}
按照如上的配置,nginx可以匹配到/front配置节,但是我的目录是/home/www/front/dist/,访问http://localhost/front/#module,nginx会去/home/www/front/dist/下找front目录,都报404 error,这个是在预料之中的。
解决方法
1.改名(此方法比较粗暴狂野)
location /front
{
index index.html;
root /home/www/front/;
}
mv /home/www/front/dist /home/www/front/front
2.使用nginx alias别名
location /front/
{
index index.html;
alias /home/www/front/dist/;
# proxy_pass http://http_front;
}
使用alias配置后 访问 http://localhost/front/nginx会去/home/www/front/dist/下去找index.html而不会去/home/www/front/dist/front去找文件了
希望对你有帮助
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论