使用 -d(守护进程)运行 Rails(Puma)时的 Nginx 502
Posted
技术标签:
【中文标题】使用 -d(守护进程)运行 Rails(Puma)时的 Nginx 502【英文标题】:Nginx 502 when running Rails (Puma) with -d (daemon) 【发布时间】:2019-11-17 04:05:22 【问题描述】:Ruby 2.5.1,Rails 5.2.2.1
我正在尝试让 nginx 通过 puma 套接字进入上游。
当我运行rails s -e production
时,一切都很好。
当我运行rails s -e production -d
Nginx 返回502 Bad Gateway
config/puma.rb
...
app_dir = "/home/user/myapp"
tmp_dir = "#app_dir/tmp"
# Set up socket location
bind "unix://#tmp_dir/sockets/puma.sock"
# Logging
stdout_redirect "#app_dir/log/puma.stdout.log", "#app_dir/log/puma.stderr.log", true
...
etc/nginx/sites-enabled/mydomain.com
upstream app
# Path to Puma SOCK file, as defined previously
server unix:/home/user/myapp/tmp/sockets/puma.sock fail_timeout=0;
server
listen 80;
server_name mydomain.com;
root /home/user/myapp/public;
try_files $uri/index.html $uri @app;
location @app
proxy_pass http://app;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
var/log/nginx/error.log
2019/07/07 13:45:09 [error] 21609#21609: *11391 connect() to
unix:/home/user/myapp/tmp/sockets/puma.sock failed (111: Connection
refused) while connecting to upstream, client: 172.68.11.91, server:
mydomain.com, request: "GET /pages/one HTTP/1.1", upstream:
"http://unix:/home/user/myapp/tmp/sockets/puma.sock:/pages/one", host: "mydomain.com"
(P.S. 从原始域更改为 mydomain.com) 有什么区别?如何解决?请解释和帮助
更新
似乎正在使用守护程序标志运行,它不会在 /home/user/myapp/tmp/sockets 中创建 puma.sock
。为什么以及在哪里?
【问题讨论】:
【参考方案1】:解决方案
不知道为什么,但是运行 puma(不是 rails server)就可以了
RAILS_ENV=production bundle exec puma -C config/puma.rb -d
【讨论】:
【参考方案2】:earlier answer 不再起作用。守护进程选项-d
已弃用。
您可以使用 systemd 服务:
sudo nano /etc/systemd/system/puma.service
将此复制到文件并填写您的 YOUR_APP_PATH 和 FULLPATH:
[Unit]
Description=Puma HTTP Server
After=network.target
# Uncomment for socket activation (see below)
# Requires=puma.socket
[Service]
# Puma supports systemd's `Type=notify` and watchdog service
# monitoring, if the [sd_notify](https://github.com/agis/ruby-sdnotify) gem is installed,
# as of Puma 5.1 or later.
# On earlier versions of Puma or JRuby, change this to `Type=simple` and remove
# the `WatchdogSec` line.
Type=notify
# If your Puma process locks up, systemd's watchdog will restart it within seconds.
WatchdogSec=10
# Preferably configure a non-privileged user
# User=
# The path to your application code root directory.
# Also replace the "<YOUR_APP_PATH>" placeholders below with this path.
# Example /home/username/myapp
WorkingDirectory=<YOUR_APP_PATH>
# Helpful for debugging socket activation, etc.
# Environment=PUMA_DEBUG=1
# SystemD will not run puma even if it is in your path. You must specify
# an absolute URL to puma. For example /usr/local/bin/puma
# Alternatively, create a binstub with `bundle binstubs puma --path ./sbin` in the WorkingDirectory
ExecStart=/<FULLPATH>/bin/puma -C <YOUR_APP_PATH>/puma.rb
# Variant: Rails start.
# ExecStart=/<FULLPATH>/bin/puma -C <YOUR_APP_PATH>/config/puma.rb ../config.ru
# Variant: Use `bundle exec --keep-file-descriptors puma` instead of binstub
# Variant: Specify directives inline.
# ExecStart=/<FULLPATH>/puma -b tcp://0.0.0.0:9292 -b ssl://0.0.0.0:9293?key=key.pem&cert=cert.pem
Restart=always
[Install]
WantedBy=multi-user.target
运行 systemctl daemon-reload
以重新加载您的服务。
然后就可以使用sudo systemctl restart puma
来重启/启动/停止服务了
有关详细信息,请参阅puma docs。
【讨论】:
以上是关于使用 -d(守护进程)运行 Rails(Puma)时的 Nginx 502的主要内容,如果未能解决你的问题,请参考以下文章
将 Rails + Puma + Postgres 应用程序部署到 Elastic beanstalk 的正确方法?
带有 Rails(Puma)的 Websockets - WebSocket 握手期间出错:意外的响应代码:200
ruby rails中如何配置puma服务监听指定的IP地址
Rails 应用程序保持这么多空闲的 Puma 和 Postgres 连接是不是正常?