1

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
...
3
  • Is your app running? Did the app send the 403 error? What Rails web server are you using? 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
4

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

3
  • 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. Nov 29 '13 at 7:07
  • This was very helpful. Thank you. Oct 14 '20 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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