Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

Unicorn x Nginx x AWS で、*11 connect() to unix:/home/ec2-user/app/current/tmp/sockets/unicorn.sock failed (13: Permission denied) while connecting to upstream のエラーの回避方法

More than 5 years have passed since last update.

ブラウザからNginx経由でUnicornに接続しようとした際に、502 Bad Gatewayが返却されてあれれってなったので調べました。

nginxのエラーログ

/var/log/nginx/app_error.log
*11 connect() to unix:/home/ec2-user/app/current/tmp/sockets/unicorn.sock failed (13: Permission denied) while connecting to upstream

どうもパーミッションがないとのことで、nginxを実行しているユーザを調べた所、

[ec2-user@ip-xxx.xxxx.xxx.xxx]$ ps aux | grep nginx
root      2089  0.0  0.0 109476  2124 ?        Ss   02:05   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 2091  0.0  0.0 109888  3248 ?        S    02:05   0:00 nginx: worker process

こんな感じでデフォルトだとnginxになっちゃうみたいですね。
以下の設定ファイルを書き換えて実行ユーザをec2-user(もしくはアプリケーションの実行ユーザ)に変更してあげれば解決できます。

/etc/nginx/nginx.conf
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

#user  nginx;
user  ec2-user;
worker_processes  1;
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away