Puma 和 NGINX 上游超时(110:连接超时)

Puma 和 NGINX 上游超时(110:连接超时)

访问使用 Nginx 代理后面的 systemd 服务运行的 Puma 应用程序会导致 nginx error.log 中出现以下错误:

 *6 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 1.2.3.4, server: mydomain.com, request: "GET / HTTP/1.1", upstream: "http://unix:/home/deploy/opt/app/shared/sockets/puma.app.sock/", host: "mydomain.com"

Puma 服务日志没有错误。

不过,在本地手动运行 Puma 应用程序(在 nginx 之前)不会产生任何错误。同样,在 http 代理而不是 puma unix 套接字上运行相同的应用程序也可以正常工作,允许 Nginx 正确处理请求。所以看起来问题不在我的后端 ruby​​ 中。

  • 美洲豹 4.3.3
  • 羅達 3.30.0
  • Rbenv Ruby 2.7.0
  • Nginx 1.14.0

/etc/nginx/sites_available/mydomain.com

upstream app {
  server unix:/home/deploy/opt/app/shared/sockets/puma.app.sock;
}

server {
  listen 80;
  server_name mydomain.com;
  root /home/deploy/opt/app/public;

  location / {
    try_files $uri @puma;
  }

  location @puma {
    include proxy_params;

    proxy_pass http://app;
  }
}

/etc/systemd/system/puma-app.service

[Unit]
Description=Puma HTTP Server
After=network.target

[Service]
# Foreground process (do not use --daemon in ExecStart or config.rb)
Type=simple

# Preferably configure a non-privileged user
User=deploy
Group=sudo

# Specify the path to your puma application root
WorkingDirectory=/home/deploy/opt/app

# Helpful for debugging socket activation, etc.
Environment=DEBUG=1
EnvironmentFile=/home/deploy/opt/app/.env

# The command to start Puma
ExecStart=/home/deploy/.rbenv/shims/bundle exec puma -C /home/deploy/opt/app/config/puma.rb

TimeoutSec = 15
Restart=always

[Install]
WantedBy=multi-user.target

/home/deploy/opt/app/config/puma.rb

# Change to match your CPU core count
workers ENV.fetch("PUMA_WORKERS") { 1 }

# Min and Max threads per worker
threads ENV.fetch("PUMA_MIN_THREADS") { 1 }, ENV.fetch("PUMA_MAX_THREADS") { 10 }

app_dir = File.expand_path("../..", __FILE__)
shared_dir = "#{app_dir}/shared"

# Set up socket location
bind "unix://#{shared_dir}/sockets/puma.app.sock"

# Redirect STDOUT to log files
stdout_redirect "#{app_dir}/log/puma.stdout.log", "#{app_dir}/log/puma.stderr.log", true

# Set master PID and state locations
pidfile "#{shared_dir}/pids/puma.app.pid"
state_path "#{shared_dir}/pids/puma.app.state"
rackup app_dir

activate_control_app

答案1

看起来是权限问题。

移动/home/deploy/opt/app/shared/sockets/puma.app.sock/run/app/puma.app.sock(+ .pid 和 .state 文件)解决了这个问题。

另外,我必须将用户分配deploy给 /run/app/

sudo chown -R deploy:sudo /run/app

相关内容