I want to make nginx serve an HTML file for /foobar/
. I tried 2 configurations and neither worked.
In Apache you do it like this:
AliasMatch ^/foobar/$ /home/admin/foobar/foobar.html
My first attempt:
location /foobar/ {
alias /home/admin/foobar/foobar.html;
}
Going to /foobar/
shows a 500 error page. The error log says:
2011/08/05 04:12:35 [alert] 32465#0: *1 "/home/admin/foobar/foobar.htmlindex.html" is not a directory, ...
This is weird. Why did it append "index.html" to the end of the file path?
My next attempt:
location ~ ^/foobar$ {
alias /home/admin/foobar/foobar.html;
}
Going to /foobar
makes my browser download the file. This is weird. Why did that happen?
I was able to get it to work with this:
location /foobar.html {
alias /home/admin/foobar/foobar.html;
}
This works, but it has the wrong URL. It has /foobar.html
instead of /foobar/
.
How do I do this?
The reason that the 'index.html' is appended is because of the index directive (probably in your main nginx.conf file)
It typically reads something similar to:
which tells nginx what to do when it is asked to serve a directory (in the above case, try to serve
index.php
, if that doesn't exist tryindex.htm
, etc.)What you can do, is to add an additional index directive within the location block if this is a special case, or if you always want to serve
foobar.html
for any directory, change your main index directive.e.g. (to override the index for just one specific location):
Don't go putting an index directive in every location block if one outside the location blocks will do.