nginx权限被拒绝错误

我运行nginx独angular兽服务于Rails应用程序。
独angular兽和Rails应用程序工作正常,但nginx无法打开rails公共目录与权限错误,并返回502错误的网关。

curl http://localhost/ <html> <head><title>502 Bad Gateway</title></head> <body bgcolor="white"> <center><h1>502 Bad Gateway</h1></center> <hr><center>nginx/1.6.1</center> </body> </html> 

无功/日志/ nginx的/ error.log中

 014/09/18 18:16:16 [crit] 16619#0: *1 stat() "/home/rails/public_html/rails_app/current/public//index.html" failed (13: Permission denied), client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "localhost" 2014/09/18 18:16:16 [crit] 16619#0: *1 stat() "/home/rails/public_html/rails_app/current/public/" failed (13: Permission denied), client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "localhost" 2014/09/18 18:16:16 [crit] 16619#0: *1 connect() to unix:/tmp/unicorn.rails_app.sock failed (2: No such file or directory) while connecting to upstream, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", upstream: "http://unix:/tmp/unicorn.rails_app.sock:/", host: "localhost" 2014/09/18 18:16:16 [crit] 16619#0: *1 stat() "/home/rails/public_html/rails_app/current/public/500.html/index.html" failed (13: Permission denied), client: 127.0.0.1, server: , request: "GET / HTTP/1.1", upstream: "http://unix:/tmp/unicorn.rails_app.sock/", host: "localhost" 2014/09/18 18:16:16 [crit] 16619#0: *1 stat() "/home/rails/public_html/rails_app/current/public/500.html" failed (13: Permission denied), client: 127.0.0.1, server: , request: "GET / HTTP/1.1", upstream: "http://unix:/tmp/unicorn.rails_app.sock/", host: "localhost" 2014/09/18 18:16:16 [crit] 16619#0: *1 connect() to unix:/tmp/unicorn.rails_app.sock failed (2: No such file or directory) while connecting to upstream, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", upstream: "http://unix:/tmp/unicorn.rails_app.sock:/500.html", host: "localhost" 

nginx.conf

 user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; # increase if you have lots of clients # accept_mutex off; # "on" if nginx worker_processes > 1 } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; # tcp_nopush on; # tcp_nodelay off; upstream unicorn { server unix:/tmp/unicorn.rails_app.sock fail_timeout=0; } server { listen 80 default deferred; # server_name example.com; root /home/rails/public_html/rails_app/current/public; try_files $uri/index.html $uri @unicorn; location @unicorn { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off; proxy_pass http://unicorn; } error_page 500 502 503 504 /500.html; client_max_body_size 4G; keepalive_timeout 10; } } 

Rails公共目录的权限是775或775。
nginx由'nginx'用户执行,rails公用目录由'rails'用户拥有。
我试图通过'rails'用户和root用户执行nginx,但是发生了相同的错误。
我不知道这个错误有什么问题。
如果我将根目录设置为/ usr / share / nginx / html /,nginx可以正常工作。 我的环境是在Vagrant的Centos7和轨道2.2.2与独angular兽gem4.8.3。

[注意]我知道/tmp/unicorn.rails_app.sock需要放在这个问题的另一个目录,但我想这不是问题。

nginx作为你的configuration状态作为nginx用户运行。 你说你的目录属于root:root

确保从根到试图被nginx访问的文件的每一个目录都有正确的执行权(也许读取权限)。 在你的情况下, 其他权限组( chmod o+(r)x <recursive list of dirs> )。

如果这还不够,请尝试在套接字文件上为其他人添加读取权限。

通常现在nginx将能够stat()这些文件。

nginx root设置为/usr/share/nginx/html一切运行正常的事实是,这些文件已由nginx包与用户/组/权限的充分混合创build…

现在,我又看到了另一个(也是更大的)问题:你尝试使用nginx nginx:nginx作为nginx:nginx和由root:root拥有的文件,对于nginx没有权限。 任何由nginx写入套接字的尝试都将最终失败

你应该真的重新考虑你的整个build设。 使nginx用户/组和目录/文件以某种方式匹配,以便nginx可以打开需要打开的内容(在目录上执行权限),读取它需要读取和写入的内容,而不依赖于其他权限组这基本上允许任何用户做同样的事情。

编辑我的nginx.conf并将用户从nginx更改为部署者(我的部署用户是capistrano和wheel组的成员),现在一切正常。

我设法解决了这个问题。
问题是nginx无法读取/tmp/unicorn.rails_app.sock,因为PrivateTmp = true在/etc/systemd//system/multi-user.target.wants/nginx.service

这就是我所知道的。
创build了独angular兽套接字的目录

 mkdir /var/run/unicorn chown rails:rails /var/run/unicorn chmod 755 /var/run/unicorn 

修改了nginx.conf

 upstream unicorn { server unix:/var/run/unicorn/unicorn.rails_app.sock fail_timeout=0; } 

禁用SElinux

 sudo setenforce 0 

永久禁用
的/ etc / SYSCONFIG / selinux的

 SELINUX=permissive SELINUXTYPE=targeted 

现在工作正常。