【问题标题】:Nginx root locationNginx 根目录
【发布时间】:2016-01-17 14:35:44
【问题描述】:

在我们的服务器上,/var/www/root/html/web/ 目录包含所有代码,/var/www/root/html/web/front/ 包含我们的静态前端代码。

我们的前端仅通过具有 /api/ 前缀的 REST API 调用与代码通信,因此所有调用都可以通过 ourdomain.com/api/products/ourdomain.com/api/products/45 等方式访问。我们还有一个管理员在那里运行,ourdomain.com/admin

当我们想看到实际的前端时,我们必须在浏览器中访问ourdomain.com/front,这当然不是我们想要的。

除其他外,我们的配置中有这个:

root /var/www/html/web;
index index.php index.html;

location /front {
        # some magic to make sure the /front folder will not be parsed
        index nothing_will_match;
        autoindex on;
    }

但是,我们希望的是,如果您转到 ourdomain.com,它将以 root 身份加载 /var/www/html/web/front/ 文件夹,如果您转到 ourdomain.com/api/*ourdomain.com/admin/*,它将以 root 身份加载 /var/www/html/web/。那可能吗?

注意:/var/www/html/web/front/ 文件夹可以根据需要移动到其他位置,例如 /var/www/html/front/

【问题讨论】:

    标签: nginx configuration location vhosts


    【解决方案1】:

    您需要的是 alias 指令(而不是 root)来重写 URI:

    root /var/www/html/web/front;
    index index.php index.html;
    
    location / {
    }
    
    location /api {
        alias /var/www/html/web/;
    }
    location /admin {
        alias /var/www/html/web/;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-29
      • 2014-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-17
      • 2021-07-12
      相关资源
      最近更新 更多