当我尝试使用NGINX和Puma时,我会得到以下错误:
[error] 13416#13416: *3 connect() to unix:///home/deploy/app/tmp/pids/puma.sock failed (111: Connection refused) while connecting to upstream, client: ip.address.redacted, server: myapp.com, request: "GET / HTTP/1.1", upstream: "http://unix:///home/deploy/app/tmp/pids/puma.sock:/", host: "ip.address.redacted"
以下是我为设置这个服务器所做的快速分解:
- 我正在使用RBENV
- 我使用的是Ruby 3.1.0和Rails 6.1
- 我没有使用Capistrano
- 该服务器设置为禁用root访问,而是使用名为deploy的sudo特权用户
当我运行sudo service puma status
时,我得到以下
● puma.service - Puma HTTP Server
Loaded: loaded (/etc/systemd/system/puma.service; disabled; vendor preset: enabled)
Active: activating (start) since Thu 2022-01-27 23:59:45 UTC; 28s ago
Main PID: 1365 (bundle)
Tasks: 12 (limit: 2274)
Memory: 155.1M
CGroup: /system.slice/puma.service
└─1365 puma 4.3.10 (tcp://0.0.0.0:3000,unix:///home/deploy/app/tmp/pids/puma.sock) [app]
Jan 27 23:59:45 localhost systemd[1]: Starting Puma HTTP Server...
Jan 27 23:59:48 localhost rbenv[1365]: Puma starting in single mode...
Jan 27 23:59:48 localhost rbenv[1365]: * Version 4.3.10 (ruby 3.1.0-p0), codename: Mysterious Traveller
Jan 27 23:59:48 localhost rbenv[1365]: * Min threads: 5, max threads: 5
Jan 27 23:59:48 localhost rbenv[1365]: * Environment: production
Jan 27 23:59:51 localhost rbenv[1365]: * Listening on tcp://0.0.0.0:3000
Jan 27 23:59:51 localhost rbenv[1365]: * Listening on unix:///home/deploy/app/tmp/pids/puma.sock
Jan 27 23:59:51 localhost rbenv[1365]: Use Ctrl-C to stop
ls -l /home/deploy/app/tmp/pids/puma.sock
产生:srwxrwxrwx 1 deploy users 0 Jan 28 00:05 /home/deploy/app/tmp/pids/puma.sock
以下是Nginx配置文件,位于:/etc/Nginx/sites-enabled/default
upstream myapp {
server unix:///home/deploy/app/tmp/pids/puma.sock;
}
server {
listen 80;
# server_name myapp.com;
# ~2 seconds is often enough for most folks to parse HTML/CSS and
# retrieve needed images/icons/frames, connections are cheap in
# nginx so increasing this is generally safe...
keepalive_timeout 5;
# path for static files
root /home/deploy/app/public;
access_log /home/deploy/app/log/nginx.access.log;
error_log /home/deploy/app/log/nginx.error.log info;
# this rewrites all the requests to the maintenance.html
# page if it exists in the doc root. This is for capistrano's
# disable web task
if (-f $document_root/maintenance.html) {
rewrite ^(.*)$ /maintenance.html last;
break;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
# If the file exists as a static file serve it directly without
# running all the other rewrite tests on it
if (-f $request_filename) {
break;
}
# check for index.html for directory index
# if it's there on the filesystem then rewrite
# the url to add /index.html to the end of it
# and then break to send it to the next config rules.
if (-f $request_filename/index.html) {
rewrite (.*) $1/index.html break;
}
# this is the meat of the rack page caching config
# it adds .html to the end of the url and then checks
# the filesystem for that file. If it exists, then we
# rewrite the url to have explicit .html on the end
# and then send it on its way to the next config rule.
# if there is no file on the fs then it sets all the
# necessary headers and proxies to our upstream pumas
if (-f $request_filename.html) {
rewrite (.*) $1.html break;
}
if (!-f $request_filename) {
proxy_pass http://myapp;
break;
}
}
# Now this supposedly should work as it gets the filenames with querystrings that Rails provides.
# BUT there's a chance it could break the ajax calls.
location ~* .(ico|css|gif|jpe?g|png|js)(?[0-9]+)?$ {
expires max;
break;
}
# Error pages
# error_page 500 502 503 504 /500.html;
location = /500.html {
root /home/deploy/app/public;
}
我在nginx.conf中添加了一个用户指令,认为这将尝试以部署用户的身份运行nginx连接。然而,这并没有任何效果。这是我的Nginx.conf文件的前几行
user deploy;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
在这一点上,我不知道该怎么办。我对彪马没有任何经验,所以我不确定我是否在配置上做了什么错误。据我所知,pum.sock文件是自动生成的,我不确定如何更改sock文件的权限/所有者,或者这是否是正确的做法。
以前有人遇到过这种情况吗?我在这里犯了什么错误?
我认为上游配置条目中有太多/。它正在尝试连接,就好像它是一个HTTP url一样。
尝试更改:
upstream myapp {
server unix:///home/deploy/app/tmp/pids/puma.sock;
}
至
upstream myapp {
server unix:/home/deploy/app/tmp/pids/puma.sock;
}
来源:nginx ngx_http_upstream_module文档