簡體   English   中英

使用 -d(守護進程)運行 Rails(Puma)時的 Nginx 502

[英]Nginx 502 when running Rails (Puma) with -d (daemon)

紅寶石 2.5.1,Rails 5.2.2.1

我試圖讓 nginx 通過 puma 套接字進入上游。

當我運行rails s -e production一切都很好。

當我運行rails s -e production -d Nginx 返回502 Bad Gateway

配置/美洲獅.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"

(PS從原來的域名改成mydomain.com)有什么區別? 如何解決? 請解釋和幫助

更新

似乎使用守護進程標志運行,它不會在 /home/user/myapp/tmp/sockets 中創建puma.sock 為什么?它在哪里?

不知道為什么,但是如果運行puma(不是rails服務器),它將起作用

RAILS_ENV=production bundle exec puma -C config/puma.rb -d

給定的答案不再起作用。 不推薦使用守護程序選項-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 文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM