Почему nginx не может получить доступ к сокету Puma на CentOS 7?

4471
Pori

Итак, у меня есть приложение Ruby on Rails, /var/www/принадлежащее пользователю nginxс 755разрешениями. Указанное приложение предназначено для развертывания через Puma.

Вот так:

rvmsudo -u nginx bundle exec puma -e production -d -b unix:///var/www/my_app/tmp/sockets/my_app.socket 

Разрешения для сокета:

srwxrwxrwx. 1 nginx nginx 0 Nov 6 09:43 tmp/sockets/my_app.sock 

Этот процесс, конечно же, принадлежит nginx:

nginx 7335 0.0 8.8 536744 90388 ? Sl 09:43 0:00 puma 2.9.2 (unix:///var/www/my_app/tmp/sockets/my_app.sock) 

Моя nginxконфигурация конфигурации выглядит следующим образом:

upstream my_app { server unix:///var/www/my_app/tmp/sockets/my_app.sock; }  server { listen 80; server_name www.example.com example.com; root /var/www/my_app/public;  location / { proxy_pass http://my_app; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } 

Все это и мое приложение по-прежнему отказано в разрешениях.

connect() to unix:///var/www/my_app/tmp/sockets/my_app.sock failed (13: Permission denied) while connecting to upstream, 

Я пробовал все это как пользователь root, а также. Но это все равно не работает.

Кто-нибудь знает, что я делаю не так?

6
Мне то же самое. Вы нашли решение? Kamil Lelonek 9 лет назад 0
Да, я сделал! Смотрите ответ ниже. Pori 9 лет назад 0

1 ответ на вопрос

9
Pori

Hallelujah! This all turned out to be an SELinux policy issue specifically pertaining to nginx. After hours of digging, I discovered such denials by running:

sudo grep nginx /var/log/audit/audit.log 

The messages looked like so:

type=AVC msg=audit(1415283617.227:1386): avc: denied { write } for pid=1683 comm="nginx" name="my_app.sock" dev="tmpfs" ino=20657 scontext=system_u:system_r:httpd_t:s0 tcontext=unconfined_u:object_r:var_run_t:s0 tclass=sock_file 

In order to fix this, I found a wonderful article by Axilleas.

To create the policy containing the necessary permissions, I had to install audit2allow and run:

grep nginx /var/log/audit/audit.log | audit2allow -M nginx 

Once done, I finalized the policy with:

semodule -i nginx.pp 

Unfortunately, I had to run this process twice before being able to access my application because further policies were needed. Nonetheless, here was the solution.

Also, there is another nice article by Sergiy Krylkov.

Moral of the story: learn SELinux.

Спасибо Большое!!! Я провел десятки часов, копая весь интернет. Это работает сейчас! AntonAL 8 лет назад 0
Нет проблем! Рад, что мой опыт может помочь кому-то еще. Pori 8 лет назад 0

Похожие вопросы