首页 > 解决方案 > 在为 NGINX 中的某个位置使用不同的根时需要帮助

问题描述

我有一个站点配置指向域的 /var/www ( https://somedomain.com/ ),但我希望将位置 /clientfolder 重定向到 /home/clientfolder/website/

我的问题是每当有请求进来时,例如https://somedomain.com/clientfolder/index.html守护进程正在 /home/clientfolder/website/clientfolder/index.html 中查找文件

所以我尝试使用重写指令:

location /clientfolder {
    rewrite ^/clientfolder/(.*)$ /$1 last;
    root /home/clientfolder/website
}

但是现在https://somedomain.com/clientfolder/index.html在 /var/www/index.html 加载文件

如果 /clientfolder 位置匹配,是否还有其他指令而不是 rewrite 允许我从文件路径中删除“clientfolder”?


更新:我设法让它工作,但感觉完全错误:

location /clientfolder/website {
    root /home;
}
location /clientfolder {
    rewrite ^/clientfolder/(.*)$ /clientfolder/website/$1;
}

标签: nginxdirective

解决方案


如果以 开头的 URI/clientfolder位于 中/home/clientfolder/website,则应使用alias指令。

例如:

location /clientfolder {
    alias /home/clientfolder/website;
}

有关详细信息,请参阅此文档


推荐阅读