Nginx getting Permission denied when connecting to

2019-02-25 02:22发布

问题:

I have tried many suggestions online but nothing has worked. I have unicorn and nginx working to deploy a ruby on rails app on a CentOS 6.5 server. It has worked before but now I'm getting the bad gateway error when I try to pull up the site. The two main files I'm working with are the unicorn.rb config file and nginx's default.conf file. These files' locations are: /home/myuser/myApp/config/unicorn.rb and /etc/nginx/conf.d/default.conf. Here is what they contain:

unicorn.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;
}

I am trying to run this stuff from myuser so I dont want to move the unicorn file or myApp. To set this up I followed https://www.digitalocean.com/community/articles/how-to-deploy-rails-apps-using-unicorn-and-nginx-on-centos-6-5 but changed the directories for some of the steps. I know the tutorial works when I dont change the directories but I really need it to run in my user home. Any help is appreciated.

It looks like the problem is in nginx. When looking at the nginx error.log, I get this :

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$

One of the solutions I've found online is to disable SELinux. I tried and it did not help. These files are all 777 on permissions so this shouldn't be happening. Any ideas?

回答1:

I found the problem. The path for unicorn.myapp.sock had to go to the /tmp directory on root rather than in myuser directory. Also, the root path in my nginx default.conf file needed to be at root/myApp/public rather than what I had. I have no idea what that last part means but it works and I'm happy. Thanks to everyone that helped me get here.



回答2:

I faced a 502 Bad Gateway issue just yesterday on my Ubuntu 12.10 and nginx and unicorn. These type of errors are too generic so the best I can do is help you find more details about underlying error. In my case, I was able to determine the cause by viewing the end of my unicorn log file.

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

Your unicorn log may be located somewhere else, I'm on a Digital Ocean server with the rails app setup. But I do think you will find the cause of the error you are receiving in your unicorn log file. If not, check your nginx error log.

In my case, I had conflicted gem versions between system gems and my app (unicorn is not running under bundle exec, on my todo) but your issue may be something else. The logs will help you figure out the cause of the 502 Bad Gateway.



回答3:

I had this problem recently. Unicorn was being executed correctly and its error's log was fine. Nginx's error log was showing "failed 13: Permission denied" messages. As many other users, I checked the permissions of the unicorn.sock file and they were correct.

The problem was that not only the unicorn.sock file needs the correct permissions; also the whole path to the unicorn file needs to have readable permissions.

chmod o+r

After changing the permissions on those folders, the magic happened.