以nginx的forms提供URL以斜线结尾的页面

我想让nginx/foobar/提供一个HTML文件。 我尝试了2个configuration,都没有工作。

在Apache中,你可以这样做:

 AliasMatch ^/foobar/$ /home/admin/foobar/foobar.html 

我第一次尝试:

 location /foobar/ { alias /home/admin/foobar/foobar.html; } 

/foobar/显示一个500错误页面。 错误日志说:

 2011/08/05 04:12:35 [alert] 32465#0: *1 "/home/admin/foobar/foobar.htmlindex.html" is not a directory, ... 

这很奇怪 为什么它会在文件path的末尾添加“index.html”?

我下一次尝试:

 location ~ ^/foobar$ { alias /home/admin/foobar/foobar.html; } 

/foobar使我的浏览器下载文件。 这很奇怪 为什么会发生?

我能够得到它与这个工作:

 location /foobar.html { alias /home/admin/foobar/foobar.html; } 

这工作,但它有错误的url。 它有/foobar/ /foobar.html而不是/foobar/

我该怎么做呢?

index.html附加的原因是因为index指令(可能在你的主要nginx.conf文件中)

它通常会读取类似于:

 index index.php index.htm index.html 

它告诉nginx要求服务一个目录时要做什么(在上面的例子中,尝试提供index.php ,如果不存在,试试index.htm等)

你可以做的是在位置块中添加一个额外的索引指令,如果这是一个特殊情况,或者你总是想为任何目录提供foobar.html ,请改变你的主索引指令。

例如(为了只覆盖一个特定位置的索引):

 location /foobar/ { index foobar.html; } 

如果位置块之外的位置块将会执行,则不要在每个位置块中放置索引指令。