I'm runnning nginx and unicorn to serve Rails application.  

Unicorn and Rails app works fine, but nginx can't open rails public directory with permission error and return 502 Bad Gateway.  

```
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>

```

**var/log/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;
        }
}

```

The permissions for rails public directories are 775 or 775.  

nginx is executed by 'nginx' user and rails public directories are owned by 'rails' user.  

I've tried to execute nginx by 'rails' user and root user but the same errors occurred.  

I have no idea what's wrong with this errors.  

nginx works fine if I set root directory to /usr/share/nginx/html/.
My environment is Centos7 on Vagrant and rails 2.2.2 with unicorn gem 4.8.3.  

[note]I know that /tmp/unicorn.rails\_app.sock need to be placed another directory by [this question](https://stackoverflow.com/questions/22272943/nginx-cannot-find-unix-socket-file-with-unicorn-no-such-file-or-directory), but I guess it's not the problem.