我正在开发一个网站(html/css/js,不涉及php)。由于需求,我使用了一个 docker 容器,里面有 nginx 镜像。
我有两个相同的 HTML 文件。一个应该以 root 身份打开(例如
www.example.com
),另一个应该在 subdir 子目录被点击时打开(例如 www.example.com/subdir
)。
由于显而易见的原因,我不想多次复制同一文件。
在nginx中,如何使
www.example.com
自动重定向到www.example.com/subdir
或反之亦然(www.example.com/subdir
重定向到www.example.com
)?
我尝试了各种方法,但我不断收到 403 禁止。
好的,所以以下解决方案应该适合您:
location = / { # Serving that html.file when requesting / (root)
index /subdir/index.html;
}
location /subdir { # Serve that subdirectory how you want, e.g. like that
root /var/www/html;
index index.html;
}
}
这有帮助吗?