我想在我的网站的根目录索引文件是index.html但在一个子目录/foo我希望它是bar.html
即
另外我想请求/foo或/foo/或/foo/:anything要parsing到bar.html /foo/:anything
这是服务器定义:
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/anything以/foo/bar.html结尾的/foo/bar.html使用这个:
location = /foo { try_files /foo/bar.html =404; } location /foo/ { try_files /foo/bar.html =404; }
在你的location你有错误的root指令。
在你的configurationnginx最终寻找文件/usr/share/nginx/mysite/public/foo/foo/bar.html