【问题标题】:Nginx getting Permission denied when connecting to UnicornNginx 在连接到 Unicorn 时获得权限被拒绝
【发布时间】:2014-04-07 22:54:09
【问题描述】:

我在网上尝试了很多建议,但都没有奏效。我有 unicorn 和 nginx 在 CentOS 6.5 服务器上部署 ruby​​ on rails 应用程序。它以前工作过,但现在当我尝试启动站点时出现错误的网关错误。我正在使用的两个主要文件是 unicorn.rb 配置文件和 nginx 的 default.conf 文件。这些文件的位置是:/home/myuser/myApp/config/unicorn.rb 和 /etc/nginx/conf.d/default.conf。以下是它们包含的内容:

独角兽.rb

Set the working application directory
# working_directory "/path/to/your/app"
working_directory "/home/myuser/myApp"

# Unicorn PID file location
# pid "/path/to/pids/unicorn.pid"
pid "/home/myuser/myApp/pids/unicorn.pid"

# Path to logs
# stderr_path "/path/to/log/unicorn.log"
# stdout_path "/path/to/log/unicorn.log"
stderr_path "/home/myuser/myApp/log/unicorn.log"
stdout_path "/home/myuser/myApp/log/unicorn.log"

# Unicorn socket
# listen "/tmp/unicorn.[app name].sock"
listen "/home/myuser/myApp/tmp/unicorn.myApp.sock"

# Number of processes
# worker_processes 4
worker_processes 2

# Time-out
timeout 30

default.conf

upstream app {
        # Path to Unicorn SOCK file, as defined previously
        server unix:/home/myuser/myApp/tmp/unicorn.myApp.sock fail_timeout=0;
}

server {


    listen 80;
    server_name localhost;

    # Application root, as defined previously
    root /root/myApp/public;

    try_files $uri/index.html $uri @app;

    location @app {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app;
    }

    error_page 500 502 503 504 /500.html;
    client_max_body_size 4G;
    keepalive_timeout 10;
}

我正在尝试从 myuser 运行这些东西,所以我不想移动 unicorn 文件或 myApp。为了进行设置,我遵循了https://www.digitalocean.com/community/articles/how-to-deploy-rails-apps-using-unicorn-and-nginx-on-centos-6-5,但更改了某些步骤的目录。我知道本教程在我不更改目录时有效,但我真的需要它在我的用户主目录中运行。任何帮助表示赞赏。

看起来问题出在 nginx 上。查看 nginx error.log 时,我得到了这个:

2014/03/05 14:53:04 [crit] 5756#0: *1 stat() "/home/myuser/myApp/public/tasks/index.html" failed (13: Permission d$
2014/03/05 14:53:04 [crit] 5756#0: *1 stat() "/home/myuser/myApp/public/tasks" failed (13: Permission denied), cli$
2014/03/05 14:53:04 [crit] 5756#0: *1 connect() to unix:/home/myuser/myApp/tmp/unicorn.myApp.sock failed (13: Per$
2014/03/05 14:53:04 [crit] 5756#0: *1 stat() "/home/myuser/myApp/public/500.html/index.html" failed (13: Permissio$
2014/03/05 14:53:04 [crit] 5756#0: *1 stat() "/home/myuser/myApp/public/500.html" failed (13: Permission denied), $
2014/03/05 14:53:04 [crit] 5756#0: *1 connect() to unix:/home/myuser/myApp/tmp/unicorn.myApp.sock failed (13: Per$
2014/03/05 14:53:04 [crit] 5756#0: *1 stat() "/home/myuser/myApp/public/favicon.ico/index.html" failed (13: Permis$
2014/03/05 14:53:04 [crit] 5756#0: *1 stat() "/home/myuser/myApp/public/favicon.ico" failed (13: Permission denied$
2014/03/05 14:53:04 [crit] 5756#0: *1 connect() to unix:/home/myuser/myApp/tmp/unicorn.myApp.sock failed (13: Per$
2014/03/05 14:53:04 [crit] 5756#0: *1 stat() "/home/myuser/myApp/public/500.html/index.html" failed (13: Permissio$
2014/03/05 14:53:04 [crit] 5756#0: *1 stat() "/home/myuser/myApp/public/500.html" failed (13: Permission denied), $
2014/03/05 14:53:04 [crit] 5756#0: *1 connect() to unix:/home/myuser/myApp/tmp/unicorn.myApp.sock failed (13: Per$

我在网上找到的解决方案之一是禁用 SELinux。我试过了,但没有帮助。这些文件都是 777 的权限,所以这不应该发生。有什么想法吗?

【问题讨论】:

  • 确保设置了正确的权限。正确的用户和正确的权限。 unicorn 需要运行并且应用程序用户可以读取。此外,您应该能够通过查看日志“/home/myuser/myApp/log/unicorn.log”获得更多信息,他们说什么?
  • @lsaffie 这带来了我应该提到的一些事情......独角兽工作正常。我能够毫无问题地启动守护进程。我将日志添加到帖子中。
  • 所以应用程序抛出 500x 错误。应用程序日志文件说明了什么? /home/myuser/myApp/log/production.log
  • @lsaffie 我在开发中运行它,所以我发布了 development.log(production.log 为空)。看起来 /show 路线不起作用,这是问题吗?我现在会调查一下
  • 是的,这就是问题所在,或者至少是第一个问题。尝试在 Welcome 控制器中执行 show 操作。 - 这实际上表明 unicorn/nginx 正在到达您的应用程序位置,问题出在实际应用程序上

标签: ruby-on-rails ruby ruby-on-rails-3 nginx


【解决方案1】:

我发现了问题。 unicorn.myapp.sock 的路径必须转到根目录下的 /tmp 目录,而不是 myuser 目录。此外,我的 nginx default.conf 文件中的根路径需要位于 root/myApp/public 而不是我所拥有的。我不知道最后一部分是什么意思,但它有效,我很高兴。感谢所有帮助我到达这里的人。

【讨论】:

  • +1 关于根路径的事情......我花了几个小时才发现我的根路径中有错字,但我得到的错误只是关于权限错误套接字文件...
  • 在我的情况下,我必须将 o+x 添加到我的主目录(之前是 700)
【解决方案2】:

我昨天在我的 Ubuntu 12.10 和 nginx 和 unicorn 上遇到了 502 Bad Gateway 问题。这些类型的错误太笼统了,所以我能做的最好的就是帮助您找到有关潜在错误的更多详细信息。就我而言,我可以通过查看我的独角兽日志文件的结尾来确定原因。

tail -n 100 /home/unicorn/log/unicorn.log

您的独角兽日志可能位于其他地方,我在安装了 rails 应用程序的 Digital Ocean 服务器上。但我确实认为你会在你的独角兽日志文件中找到你收到的错误的原因。如果没有,请检查您的 nginx 错误日志。

在我的情况下,我的系统 gem 和我的应用程序之间的 gem 版本存在冲突(unicorn 没有在 bundle exec 下运行,在我的待办事项上),但您的问题可能是其他问题。这些日志将帮助您找出 502 Bad Gateway 的原因。

【讨论】:

    【解决方案3】:

    我最近遇到了这个问题。 Unicorn 正在正确执行,并且它的错误日志很好。 Nginx 的错误日志显示“失败 13:权限被拒绝”消息。和许多其他用户一样,我检查了 unicorn.sock 文件的权限,它们是正确的。

    问题在于不仅 unicorn.sock 文件需要正确的权限;独角兽文件的整个路径也需要具有可读权限。

    chmod o+r
    

    更改这些文件夹的权限后,神奇的事情发生了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-13
      • 2014-07-19
      • 2022-01-03
      • 2019-10-22
      • 2015-07-04
      • 1970-01-01
      • 2016-05-12
      相关资源
      最近更新 更多