【问题标题】:Nginx - another root for a specific locationNginx - 特定位置的另一个根
【发布时间】:2023-03-03 22:05:01
【问题描述】:

我在下面有一些静态 html 文件:

/var/www/project1 

这个项目的 Nginx 配置是:

server_name             www.project1.com project1.com;
root                    /var/www/project1;

location / {
    index           index.html;
}

我的目标是使用 nginx,这样当用户输入这个 url 时:

www.project1.com/project2

Nginx 使用另一个root,我试过了:

 location /project2 {
     root /var/www/project2;
     index index.html;
}

但这不起作用。关于如何实现这一点的任何想法?

【问题讨论】:

  • project2 的实际路径是什么?是/var/log/project2吗?您应该在 project2 的位置使用 root /var/www,否则您将在 /var/www/project2/project2 下查找文件
  • @Lution,谢谢,解决了这个问题。其实project2的路径是/var/www/project2。你能回答这个问题,我会接受

标签: nginx nginx-location


【解决方案1】:

根据你对project2的配置

location /project2 {
    root /var/www/project2;
    index index.html;
}

Nginx 将在路径 /var/www/project2/project2/ 下查找文件,以满足您对 project2 的请求。所以如果你的project2在/var/www/project2下,正确的配置应该是

location /project2 {
    root /var/www;
    index index.html;
}

另一种选择是使用alias 而不是root

你的情况是alias /var/www/project2,检查here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-17
    • 2020-05-23
    • 2014-08-28
    • 2018-11-16
    • 2018-12-02
    相关资源
    最近更新 更多