nginx 权限拒绝访问确实存在于正确位置的 puma 套接字

nginx permission denied accessing puma socket that does exist in the correct location

在 Digital Ocean droplet 运行ning Ubuntu 21.10 impish 我正在将一个简单的 Rails 7.0.0.alpha2 应用程序部署到生产环境中。我正在将 nginx 设置为反向代理服务器以与充当 Rails 服务器的 Puma 进行通信。

我希望 运行 puma 作为使用 systemctl 的服务而无需 sudo root 权限。为此,我在位于 ~/.config/systemd/user 的用户主文件夹中设置了一个 puma 服务,该服务已启用并且 运行s 正如我所期望的 运行.

systemctl status --user puma_master_cms_production

报告以下内容

● puma_master_cms_production.service - Puma HTTP Server for master_cms (production)
     Loaded: loaded (/home/comtechmaster/.config/systemd/user/puma_master_cms_production.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2021-11-18 22:31:02 UTC; 1h 18min ago
   Main PID: 1577 (ruby)
      Tasks: 10 (limit: 2338)
     Memory: 125.1M
        CPU: 2.873s
     CGroup: /user.slice/user-1000.slice/user@1000.service/app.slice/puma_master_cms_production.service
             └─1577 puma 5.5.2 (unix:///home/comtechmaster/apps/master_cms/shared/tmp/sockets/puma_master_cms_production.sock)

Nov 18 22:31:02 master-cms systemd[749]: Started Puma HTTP Server for master_cms (production).

rails production.log 为空。 puma 错误日志显示如下

cat log/puma_error.log 
=== puma startup: 2021-11-18 22:31:05 +0000 ===

pid 文件存在于应用程序根 shared/tmp/pids 文件夹中

ls tmp/pids
puma.pid  puma.state

并且nginx需要但由于权限被拒绝而无法连接的套接字存在

ls -l ~/apps/master_cms/shared/tmp/sockets/
total 0
srwxrwxrwx 1 comtechmaster comtechmaster 0 Nov 18 22:31 puma_master_cms_production.sock

nginx 已启动,运行正在运行并提供

502 bad gateway

响应。 nginx错误日志报如下错误

2021/11/18 23:18:43 [crit] 1500#1500: *25 connect() to unix:/home/comtechmaster/apps/master_cms/shared/tmp/sockets/puma_master_cms_production.sock failed (13: Permission denied) while connecting to upstream, client: 86.160.191.54, server: 159.65.50.229, request: "GET / HTTP/2.0", upstream: "http://unix:/home/comtechmaster/apps/master_cms/shared/tmp/sockets/puma_master_cms_production.sock:/500.html"

sudo nginx -t 报告如下

sudo nginx -t
nginx: [warn] could not build optimal proxy_headers_hash, you should increase either proxy_headers_hash_max_size: 512 or proxy_headers_hash_bucket_size: 64; ignoring proxy_headers_hash_bucket_size
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successfu

只是为了迂腐 lssudo ls 到错误中报告的路径显示

ls /home/comtechmaster/apps/master_cms/shared/tmp/sockets/
puma_master_cms_production.sock

正如预期的那样,所以我很难理解为什么 nginx 运行 以 root 身份使用 sudo service nginx start 被拒绝访问存在的套接字,该套接字由本地用户而不是 root 拥有。

我希望解决方案非常明显,但我看不出是什么

这个问题最终与用户主文件夹的文件夹权限有关,特别是 Ububntu 20.10 设置权限的方式与以前版本的 ubuntu 不同,或者至少在方式上有所不同DigitalOcean 安装脚本的行为。 这是通过 /home 中针对相关用户文件夹的简单命令行 chmod o=rx 解决的,例如

cd /home
chmod o=rx the_home_folder_for_user

感谢@jamesc 这解决了我的问题 ubuntu 22.04

我作为 ubuntu 用户做了

cd /home

sudo chmod o=rx deploy

以下 nginx 上的先前错误,权限被拒绝:

==> /var/log/nginx/error.log <==
2022/05/02 12:48:55 [crit] 10524#10524: *2 stat() 
"/home/deploy/production/current/public/favicon.ico/index.html" failed 
(13: Permission denied), client: xx.xx.xx, , request: "GET /favicon.ico 
HTTP/1.1", host: "technologies.co.uk", referrer: 
"http://technologies.co.uk/"