Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

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:

index index.php index.htm index.html

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 try index.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):

location /foobar/ {
    index foobar.html;
}

Don't go putting an index directive in every location block if one outside the location blocks will do.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.