Search code examples
ruby-on-railsrubynginxunicornhttp-status-code-403

Ruby on rails application return 403 error deploy in ubuntu with unicorn and nginx?


I deploy a ruby on rails application in ubuntu. I have test my application with RAILS_ENV=production rails s, everything is ok. But with unicorn and nginx, I got 403 error.

here is the error log:

2015/01/21 16:04:48 [error] 12432#0: *1 directory index of "/home/roger/ruby_workspace/hello_app/public/" is forbidden, client: 192.168.44.1, server: , request: "GET / HTTP/1.1", host: "192.168.44.131"

ll /home/roger/ruby_workspace/hello_app/public/ return

drwxrwxr-x  2 roger roger 4096  1月 13 14:55 ./
drwxrwxr-x 14 roger roger 4096  1月 19 22:30 ../
-rwxrwxr-x  1 roger roger 1564  1月 13 14:55 404.html*
-rwxrwxr-x  1 roger roger 1547  1月 13 14:55 422.html*
-rwxrwxr-x  1 roger roger 1477  1月 13 14:55 500.html*
-rwxrwxr-x  1 roger roger    0  1月 13 14:55 favicon.ico*
-rwxrwxr-x  1 roger roger  202  1月 13 14:55 robots.txt*

this is part of my nginx.conf:

server {
    listen 80 default deferred;
    root /home/roger/ruby_workspace/hello_app/public/;
    try_files $uri/index.html $uri @hello_app;
    client_max_body_size 128M;
    keepalive_timeout 5;

    access_log logs/host.access.log main;

    location @hello_app {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header HOST $http_host;
        proxy_redirect off;
        #proxy_pass http://hello_app;
    }

    error_page 500 502 503 504 /500.html;
}

Solution

  • Take a look on this Nginx configuration example as consistent and fully functional. This config describes Nginx proxying of Rails app runing on Unicorn via Unix socket. Now you ready to fix your config!:-)

    In order to use this config from example above, setup Unicorn to use socket (bit faster) or HTTP port. And make sure Unicorn starts correctly. Only after that you may continue.

    Please be sure @hello_app is correct upstream name in your config. Currently it's definition is absent in your code:

    upstream hello_app{
      # Update xxx.socket below:
      server unix:/home/roger/ruby_workspace/hello_app/tmp/sockets/xxx.socket fail_timeout=0;
      # Or use HTTP port here e.g.
      # server http://hello_app
    }
    

    Anyway it's much more easy to debug proxying, when everything behind it is working.

    Just in case, make sure you have assets precompilled;-)

    $ RAILS_ENV=production bundle exec rake assets:precompile