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

Ade*_*Ade 8 rewrite nginx

我希望我的站点根目录中的目录索引文件位于我希望它index.html的子目录/foobar.html

IE

  • /index.html
  • /foo/bar.html

此外,我想请求/foo/foo//foo/:anything以决心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;
  }
}
Run Code Online (Sandbox Code Playgroud)

编辑:我还在那个服务器块的底部有以下内容。

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;
}
Run Code Online (Sandbox Code Playgroud)

Ale*_*Ten 7

bar.html作为索引文件使用:

location /foo {
    index bar.html;
}
Run Code Online (Sandbox Code Playgroud)

如果您希望任何请求/foo/anything最终/foo/bar.html使用此:

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

location /foo/ {
    try_files /foo/bar.html =404;
}
Run Code Online (Sandbox Code Playgroud)

在您中,location您有错误的root指令。

在您的配置中,nginx 最终会查找该文件 /usr/share/nginx/mysite/public/foo/foo/bar.html