加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > Nginx > 正文

nginx – 为子目录指定不同的索引文件名

发布时间:2020-12-13 21:07:58 所属栏目:Nginx 来源:网络整理
导读:我希望我的站点根目录中的目录索引文件是index.html但在子目录/ foo中我希望它是bar.html 即 /index.html /foo/bar.html 此外,我想要/ foo或/ foo /或/ foo /:请求解决bar.html的任何请求 为什么我的位置阻止/ foo不起作用? 如何为/ foo指定位置块? 这是

我希望我的站点根目录中的目录索引文件是index.html但在子目录/ foo中我希望它是bar.html

> /index.html
> /foo/bar.html

此外,我想要/ foo或/ foo /或/ foo /:请求解决bar.html的任何请求

>为什么我的位置阻止/ foo不起作用?
>如何为/ foo指定位置块?

这是服务器定义:

server {
  listen  80;
  server_name  domain.tld;
  port_in_redirect off;

  location / {
    # ssl is terminated at our load bal
    if ($http_x_forwarded_proto != 'https') {
      rewrite ^ https://$host$request_uri? permanent;
    }

    root   /usr/share/nginx/mysite/public;
    index  index.html;
    add_header Cache-Control "public max-age=600";
    add_header "X-UA-Compatible" "IE=Edge,chrome=1";
    charset UTF-8;
  }

  # this location doesn't work  <<<<<<<<<<<<<<
  location /foo {
    root  /usr/share/nginx/mysite/public/foo;
    index  bar.html;
  }

  # set expiration of assets to 30d for caching
  location ~* .(ico|css|js|gif|jpe?g|png)(?[0-9]+)?${
    root   /usr/share/nginx/mysite/public;
    expires 30d;
    add_header Cache-Control "public";
    log_not_found off;
  }
}

编辑:我在该服务器块的底部也有以下内容.

error_page  404              /404.html;
location = /404.html {
  root   /usr/share/nginx/www;
}

# redirect server error pages to the static page /50x.html
error_page   500 502 503 504  /50x.html;
location = /50x.html {
  root   /usr/share/nginx/www;
}
最佳答案
要将bar.html用作索引文件,请使用:

location /foo {
    index bar.html;
}

如果您希望/ foo / bar.html中的/ foo / what请求最终使用:

location = /foo {
    try_files /foo/bar.html =404;
}

location /foo/ {
    try_files /foo/bar.html =404;
}

在您的位置,您有错误的根指令.

在你的配置nginx最终寻找文件/usr/share/nginx/mysite/public/foo/foo/bar.html

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读