Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This is what I'm trying to achieve:

PHYSICAL PATH: /subfolder/index.html


OUTPUT: whatever the contents of /subfolder/index.html are.

I read that this should work automatically, but evidently it doesn't work on Nginx. (I've tested it on Apache, and it works there automatically.)

What is the best way to replicate this behaviour? Do I need to use rewrites, or can I make a simple change in a configuration file?

EDIT: Here is a link to a copy of my configuration file. I know that it has index mentioned twice, and that the try_files section is a bit weird. Both those things came from my experimentation with different settings.

share|improve this question
"I have a site which I run as a hobby using Nginx" – Better don't mention something like that when asking on serverfault... 8-) – Hauke Laging Apr 28 '13 at 10:53
I will keep that in mind. – daviewales Apr 28 '13 at 11:06
What does "it doesn't work" mean? – Michael Hampton Apr 28 '13 at 16:08
It doesn't automatically serve an index.html file if you request its enclosing folder. I discovered I had a section in my config that automatically redirected you to the main page if you didn't type the exact url. After I removed that, it worked as expected. – daviewales Apr 29 '13 at 0:40
OK. But please put answers in an Answer, not in the Question. – Michael Hampton Apr 29 '13 at 0:58

You're missing an index statement:

server {

    location / {
        index  index.html index.htm;
share|improve this answer
That didn't fix it. (I already had that line in the server section, but adding it to the location section had no effect.) Here is a link to my config file. – daviewales Apr 28 '13 at 11:34
Try without lines 8-15. If that works, add lines back until it breaks again. Then you know what's broken :) – Dennis Kaarsemaker Apr 28 '13 at 11:36
That worked. The offending lines were if (!-f $request_filename) { rewrite ^ /index.html last;} – daviewales Apr 28 '13 at 11:47
I'm not quite sure what the etiquette is here. Should I create an answer stating that the removal of the rewrite fixed it, or should I let you create an answer stating the same thing so I can accept it? – daviewales Apr 28 '13 at 11:51
That line made Nginx check if the exact file existed, and if not, redirected to the main page. I suspect the same functionality could be better implemented using try_files. – daviewales Apr 28 '13 at 11:56
up vote 0 down vote accepted

The solution was to remove the following lines from my config.

if (!-f $request_filename) {
    rewrite ^ /index.html last;    

Thanks to @Dennis Kaarsemaker who suggested deleting lines from my config to see what the problem was.

share|improve this answer
@Dennis Kaarsemaker, if you want to make your own answer, I'll accept it instead, and delete this one. – daviewales Apr 30 '13 at 11:07

Your Answer


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.