1

Ruby 2.5.1, Rails 5.2.2.1

I'm trying to make nginx get upstream through puma socket.

When I run rails s -e production all is good.

When I run rails s -e production -d Nginx returns 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. change from original domain to mydomain.com) What difference? How to fix it? Please explain and help

UPDATE

Seems to be running with daemon flag it doesn't create puma.sock in /home/user/myapp/tmp/sockets. Why and where is it?

2 Answers 2

1

The earlier answer does not work any more. The deamon option -d is deprecated.

You could use a systemd service:

sudo nano /etc/systemd/system/puma.service

Copy this to the file and fill in your YOUR_APP_PATH and 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

Run systemctl daemon-reload to reload your services.

Then you can use sudo systemctl restart puma to restart/start/stop the service

Refer to the puma docs for more information.

0

Solution

Dont know why, but It works if run puma (not rails server)

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.