LoginSignup
7

More than 5 years have passed since last update.

nginx が socket に書き込めない問題

Last updated at Posted at 2016-07-22

SELinuxが有効になっていると nginx が puma.sock に書き込めず以下のようなエラーが /var/log/nginx/error.log に出てた

2016/07/22 18:46:25 [crit] 27059#27059: *63 connect() to unix:/home/ec2-user/hoge/tmp/sockets/puma.sock failed (13: Permission denied) while connecting to upstream, client: xxx.xxx.xxx.xxx, server: localhost, request: "GET /admin HTTP/1.1", upstream: "http://unix:/home/ec2-user/hoge/tmp/sockets/puma.sock:/admin", host: "xxx.xxx.xxx.xxx"

audit.log に以下のようなメッセージが出てる

type=AVC msg=audit(1469180785.073:858): avc:  denied  { write } for  pid=27059 comm="nginx" name="puma.sock" dev="xvda2" ino=25826156 scontext=system_u:system_r:httpd_t:s0 tcontext=unconfined_u:object_r:user_home_t:s0 tclass=sock_file
type=SYSCALL msg=audit(1469180785.073:858): arch=c000003e syscall=42 success=no exit=-13 a0=11 a1=246d778 a2=6e a3=7fffdd4341e0 items=0 ppid=27058 pid=27059 auid=4294967295 uid=1000 gid=1000 euid=1000 suid=1000 fsuid=1000 egid=1000 sgid=1000 fsgid=1000 tty=(none) ses=4294967295 comm="nginx" exe="/usr/sbin/nginx" subj=system_u:system_r:httpd_t:s0 key=(null)

SELinux の有効無効を確認

$ sudo getenforce
Enforcing

有効になってる。
SELinux を無効化すれば解決するが、無効にせず socket の問題を解決したい。

audit2allow -mコマンドでポリシーモジュールを作成する。

$ sudo cat /var/log/audit/audit.log | grep nginx | audit2allow -m nginx

module nginx 1.0;

require {
        type unconfined_t;
        type httpd_t;
        type user_home_t;
        class sock_file write;
        class unix_stream_socket connectto;
}

#============= httpd_t ==============
allow httpd_t unconfined_t:unix_stream_socket connectto;
allow httpd_t user_home_t:sock_file write;

上記で問題なさそうなら audit2allow -M でモジュールパッケージ(バイナリファイル)を作成。

$ sudo cat /var/log/audit/audit.log | grep nginx | audit2allow -M nginx
******************** IMPORTANT ***********************
To make this policy package active, execute:

semodule -i nginx.pp

semodule -i で作成したモジュールパッケージを適用

$ sudo semodule -i nginx.pp

これで nginx が動くようになった。

参考

SELinux + Nginx ポリシー設定 - URAGAMI

追記

そもそも、ユーザのディレクトリにあるファイルにアクセスさせるような配置方法が問題ある気がする、、、

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7