使用子域上不同的根文件夹在多个位置配置Nginx [英] Configure nginx with multiple locations with different root folders on subdomain

查看:70
本文介绍了使用子域上不同的根文件夹在多个位置配置Nginx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望将子域的根URL和子域的目录提供给服务器上的两个不同文件夹.这是我已有但不起作用的简单设置...

I'm looking to serve the root url of a subdomain and directory of a subdomain to two different folders on my server. Here is the simple set-up that I have and is not working...

server {

    index index.html index.htm;
    server_name test.example.com;

    location / {
            root /web/test.example.com/www;
    }

    location /static {
            root /web/test.example.com/static;
    }
}

在此示例中,转到test.example.com/会将索引文件放入/web/test.example.com/www

In this example going to test.example.com/ would bring the index file in /web/test.example.com/www

并转到test.example.com/static会将索引文件放入/web/test.example.com/static

and going to test.example.com/static would bring the index file in /web/test.example.com/static

推荐答案

您需要对location /static使用alias指令:

server {

  index index.html;
  server_name test.example.com;

  root /web/test.example.com/www;

  location /static/ {
    alias /web/test.example.com/static/;
  }

}

nginx Wiki 比我能更好地解释根目录和别名之间的区别:

The nginx wiki explains the difference between root and alias better than I can:

请注意,乍一看,它看起来可能类似于root指令,但是文档根目录并没有改变,只是用于请求的文件系统路径.在Nginx问题中删除了请求的位置部分.

Note that it may look similar to the root directive at first sight, but the document root doesn't change, just the file system path used for the request. The location part of the request is dropped in the request Nginx issues.

请注意,rootalias处理尾随斜杠的方式不同.

Note that root and alias handle trailing slashes differently.

这篇关于使用子域上不同的根文件夹在多个位置配置Nginx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆