Nginx failed (13: Permission denied) when initial rails with unicorn
The rails app is running on the server with Unicorn
and Nginx
, but after setting up Nginx and running it, I got the error:
2015/08/03 15:43:44 [crit] 13951#0: *1 stat() "/home/ec2-user/apps/mybest/current/public//index.html" failed (13: Permission denied), client: 123.185.144.80, server: 52.74.148.194, request: "GET / HTTP/1.1", host: "52.74.148.194"
2015/08/03 15:43:44 [crit] 13951#0: *1 stat() "/home/ec2-user/apps/mybest/current/public/.html" failed (13: Permission denied), client: 123.185.144.80, server: 52.74.148.194, request: "GET / HTTP/1.1", host: "52.74.148.194"
2015/08/03 15:43:44 [crit] 13951#0: *1 stat() "/home/ec2-user/apps/mybest/current/public/" failed (13: Permission denied), client: 123.185.144.80, server: 52.74.148.194, request: "GET / HTTP/1.1", host: "52.74.148.194"
here is nginx_myapp.conf
upstream unicorn {
server unix:/tmp/unicorn.myapp.sock fail_timeout=0;
}
server {
listen 80 default deferred;
server_name 52.74.148.194;
root /home/ec2-user/apps/mybest/current/public;
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
try_files $uri/index.html $uri.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;
keepalive_timeout 10;
}
I've googled for hours but still can't seem to work it out. I'm guessing if any way of typos, and not sure why the error message is with " public//index.html
" and not " public/index.html
" Any hint? thank!
Nginx works with user ' Nginx
' as shown below:
EDIT:
$ groups nginx
nginx : nginx ec2-user
groups ec2-user
ec2-user : ec2-user wheel
Path Resolutions: (current)
current]$ ls -l
total 76
drwxrwxr-x 8 ec2-user ec2-user 4096 Aug 3 14:06 app
drwxrwxr-x 2 ec2-user ec2-user 4096 Aug 3 14:08 assets_manifest_backup
drwxrwxr-x 2 ec2-user ec2-user 4096 Aug 3 14:26 bin
-rw-rw-r-- 1 ec2-user ec2-user 830 Aug 3 14:06 Capfile
drwxrwxr-x 6 ec2-user ec2-user 4096 Aug 3 14:08 config
-rw-rw-r-- 1 ec2-user ec2-user 153 Aug 3 14:06 config.ru
drwxrwxr-x 2 ec2-user ec2-user 4096 Aug 3 14:08 db
-rw-rw-r-- 1 ec2-user ec2-user 1720 Aug 3 14:06 Gemfile
-rw-rw-r-- 1 ec2-user ec2-user 5262 Aug 3 14:06 Gemfile.lock
drwxrwxr-x 4 ec2-user ec2-user 4096 Aug 3 14:06 lib
drwxrwxr-x 2 ec2-user ec2-user 4096 Aug 3 14:35 log
drwxrwxr-x 2 ec2-user ec2-user 4096 Aug 3 14:08 public
-rw-rw-r-- 1 ec2-user ec2-user 249 Aug 3 14:06 Rakefile
-rw-rw-r-- 1 ec2-user ec2-user 478 Aug 3 14:06 README.rdoc
-rw-rw-r-- 1 ec2-user ec2-user 8 Aug 3 14:07 REVISION
drwxrwxr-x 8 ec2-user ec2-user 4096 Aug 3 14:06 test
drwxrwxr-x 4 ec2-user ec2-user 4096 Aug 3 14:35 tmp
drwxrwxr-x 3 ec2-user ec2-user 4096 Aug 3 14:06 vendor
public: (I changed to 777)
ls -l
total 16
-rwxrwxrwx 1 ec2-user ec2-user 1564 Aug 3 14:06 404.html
-rwxrwxrwx 1 ec2-user ec2-user 1547 Aug 3 14:06 422.html
-rwxrwxrwx 1 ec2-user ec2-user 1477 Aug 3 14:06 500.html
lrwxrwxrwx 1 ec2-user ec2-user 47 Aug 3 14:08 assets -> /home/ec2-user/apps/mybest/shared/public/assets
-rwxrwxrwx 1 ec2-user ec2-user 0 Aug 3 14:06 favicon.ico
-rwxrwxrwx 1 ec2-user ec2-user 202 Aug 3 14:06 robots.txt
Change the nginx user in nginx.conf from 'nginx' to 'ec2-user' resolving it.
source to share
Make sure nginx is running under the correct user (directive user ...
in the main nginx config file) and then make sure the files /home/ec2-user/apps/mybest/current/public/*
are available for that user (i.e. they belong to the same group as the user and they have read permission ).
You also need to have + x permission for each directory in your path. You could see the permissions with ls -l
in your terminal and then just do something like this if they are missing:
chmod g+x apps
cd apps
chmod g+x mybest
cd mybest
chmod g+x current
cd current
chmod g+x public
cd public
chmod g+r *
UPD . As clarified in the comments, nginx works fine under ec2-user
username ( user ec2-user
in config). Most likely there are restrictive permissions (no "+ x" / "+ r" for group in directories) for /home
and / or /home/ec2-user
. Personally, I don't see anything wrong with nginx running under a ec2-user
username. Or you can move your Rails app to, for example /var/www/my_app
, set permissions for a user nginx
and run it from there.
source to share