Ruby 在 rails 应用程序 return 中使用独角兽和 nginx 在 ubuntu 中部署时出现 403 错误?

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

我在 ubuntu 中部署了一个 ruby on rails 应用程序。我用 RAILS_ENV=production rails s 测试了我的应用程序,一切正常。但是对于 unicornnginx,我得到了 403 错误。

这是错误日志:

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*

这是我的一部分 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;
}

通过 Unix 套接字查看 this Nginx configuration example as consistent and fully functional. This config describes Nginx proxying of Rails app runing on Unicorn。现在您可以修复您的配置了!:-)

为了使用上述示例中的配置,将 Unicorn 设置为使用套接字(快一点)或 HTTP 端口。并确保 Unicorn 正确启动。只有在那之后你才能继续。

请确保 @hello_app 是您配置中的正确上游名称。目前它的定义在您的代码中不存在:

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
}

无论如何,当代理背后的一切正常时,调试代理会容易得多。

以防万一,请确保您已预编译资产;-)

$ RAILS_ENV=production bundle exec rake assets:precompile