nginx 动态读取多文件夹下index.html

大家好,我要配一个nginx 静态路径,把/app文件夹设定为root,在app文件夹下有很多服务,1,2,3,4,5.... 每个服务下都有一个index.html 我希望访问每个服务的文件夹就可以自动读取index.html,我当前的配置无法做到,只能自己输入index.html,如 xxx.com/1/index.html,(我希望xxx.com/1就能自动读取index.html)。我的配置如下:
server {

      listen 80;
      location ~ \.(js|css|png|jpg|gif|html|woff|ttf) {
        root /app;
        index index.html;
      }
    }

求如何实现,谢谢

阅读 7.5k
3 个回答
server {
  listen 80;
  root /app;
  location / {
    index index.html;
  }
}
server {
  listen 80;
  root */app;
  location ~ \.(js|css|png|jpg|gif|html|woff|ttf) {
    rewrite ^/(\w+)$ /$1/index.html;
  }
}

root 修改为对应绝对路径。根据需要修改正则匹配规则即可。

使用try_files自动尝试, index放到上一级

server {
    listen 80;
    root /app;
    index index.html default.html;
    location / {
        try_files $uri $uri/ $uri/index.html =404;
    }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题