首页 > 解决方案 > Nginx 目录列表仅用于子路径

问题描述

我试图将我的子路径作为仅在 Nginx 中列出的目录,但是它返回 404。

假设我有以下配置http://file.example.com/test

server {
    listen        80;
    server_name  file.example.com;

    location /test/ {
        root    /var/www/abc/def/;
        autoindex on;
        autoindex_exact_size off;
        autoindex_format html;
        autoindex_localtime on;
    }
}

我没有指定/部分,是不是上面导致的问题不起作用?我错过了什么?

标签: nginxnginx-locationnginx-config

解决方案


将位置更改为location /test

将所有文件复制到/var/www/abc/def/test文件夹,因为您正在使用 location 指令和 root 指令。在您的情况下,当 Nginx 搜索静态文件时,它将使用根路径/var/www/abc/def并附加位置路径/test

或者按照@RichardSmith 的建议使用别名。


推荐阅读