如何让nginx中多个位置打开同一个index.html文件?

问题描述 投票:0回答:1

我正在开发一个网站(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 禁止。

docker nginx nginx-config nginx-location
1个回答
0
投票

好的,所以以下解决方案应该适合您:

    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;
    }
  }    

这有帮助吗?

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.