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
...
  • Is your app running? Did the app send the 403 error? What Rails web server are you using? – Michael Hampton Nov 29 '13 at 6:51
  • No the app is running - I can go to other directories like /users and it shows up. It's just the root page. I'll update the question with some more info from the error log. – josh Nov 29 '13 at 6:53
  • Sorry, meant the app did not send the 403 error. – josh Nov 29 '13 at 7:00
up vote 3 down vote accepted

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...

  • That fixed it! Thanks. I thought that you should always have $uri $uri/ in the directive... What exactly does $uri/ do that causes this error? – josh Nov 29 '13 at 7:06
  • 1
    It causes nginx to try to load the files specified in the index directive, or if none exist, to do a directory index. – Michael Hampton Nov 29 '13 at 7:07

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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