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

查看:22
本文介绍了使用子域上不同根文件夹的多个位置配置 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 指令:

You need to use the alias directive for location /static:

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 比我更能解释 root 和别名之间的区别:

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天全站免登陆