Does Nginx require an index file for the root directory?

josh asked:

I’m developing a Rails application, and I’m now making the production environment default. I created the directive

server {
    listen 127.0.0.1:80;
    server_name example.com;
    root /web/applications/dashboard/public;
    index index.html;

    location / {
        try_files $uri $uri/ @app;
    }
    location @app {
        proxy_set_header host $Host;
        proxy_redirect off;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_pass http://dashboard;
    }
}

This gives me a 403 Forbidden error when I attempt to visit the URL of my app. I don’t have an index.html in the public directory.

If I add the directive

location ~ ^/(assets)/  {
  gzip_static on; # to serve pre-gzipped version
  expires max;
  add_header Cache-Control public;
}

and change the @app directive to /, everything works perfectly.

Do I need an index.html defined in the root directory? I would ideally not like this to happen and just go to the application directly.

Edit: Here’s a relevant entry from the error.log:

2013/11/29 07:38:08 [error] 30796#0: *1570 directory index of "/web/applications/dashboard/public/" is forbidden, client: '', server: example.com, request: "GET / HTTP/1.1", host: "example.com"

The permissions on that folder are:

...
drwxr-xr-x  3 joshua joshua 4096 Nov 29 07:15 public
...

My answer:


I would remove $uri/ from try_files. Since you aren’t going to be serving any index.html files directly, there is no need for it to be present, and it is the immediate cause of this error, if not the root cause. Leaving you with:

try_files $uri @app;

Oh, and make sure you set up a route in config/routes.rb


View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.