Question

I'm having an issue setting up Nginx to work with Puma server for a Rails 4 application.

The problem seems to be in my Nginx configuration since I keep getting 502 Bad Gateway error and error log states the following:

*1 connect() to unix:///srv/vhosts/rumysitename/www/mysitename/tmp/mysitename.sock failed 
(2: No such file or directory) while connecting to upstream, client: XX.XXX.XX.XXX, 
server: mysitename.com, request: "GET /favicon.ico HTTP/1.1", upstream: 
"http://unix:///srv/vhosts/rumysitename/www/mysitename/tmp/mysitename.sock:/favicon.ico", 
host: "mysitename.com"

Here is the Nginx site configuration I'm using:

upstream mysitename {
  server unix:///srv/vhosts/rumysitename/www/mysitename/tmp/mysitename.sock;
}

server {
  listen 80;
  server_name mysitename.com;
  root /srv/vhosts/rumysitename/www/mysitename/public;

  location / {
    proxy_pass http://mysitename;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }

  location ~* ^/assets/ {
    # Per RFC2616 - 1 year maximum expiry
    expires 1y;
    add_header Cache-Control public;

    # Some browsers still send conditional-GET requests if there's a
    # Last-Modified header or an ETag header even if they haven't
    # reached the expiry date sent in the Expires header.
    add_header Last-Modified "";
    add_header ETag "";
    break;
  }
}

Needless to say that Puma can't connect to that sock because it isn't there:

rails s -e production -b unix:///srv/vhosts/rumysitename/www/mysitename/tmp/mysitename.sock=> Booting Puma
=> Rails 4.1.0 application starting in production on http://unix:///srv/vhosts/rumysitename/www/mysitename/tmp/mysitename.sock:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Puma 2.8.2 starting...
* Min threads: 0, max threads: 16
* Environment: production
* Listening on tcp://unix:///srv/vhosts/rumysitename/www/mysitename/tmp/mysitename.sock:3000
Exiting
/srv/vhosts/rumysitename/.rvm/gems/ruby-2.1.0/gems/puma-2.8.2/lib/puma/binder.rb:195:in `initialize': getaddrinfo: Name or service not known (SocketError)

I am no system admin and have absolutely no experience with Nginx so excuse me if I'm missing something obvious.

Was it helpful?

Solution

It seems that starting rails server and asking it to bind to a unix socket doesn't work. The -b option with rails server doesn't behave the same was as with the puma command. Basically it wants to bind to an IP address:

rails server --help
  Usage: rails server [mongrel, thin, etc] [options]
    -p, --port=port                  Runs Rails on the specified port.
                                     Default: 3000
    -b, --binding=ip                 Binds Rails to the specified ip.
                                     Default: 0.0.0.0

You can run puma directly:

puma -e production -b unix:///srv/vhosts/rumysitename/www/mysitename/tmp/mysitename.sock

The socket will be created by running puma like this. It doesn't need to exist already. You'll need to have permission to create it in the location specified but you'll get a different error if that is not the case.

Another alternative is to create a config/puma.rb file which can include the binding:

config/puma.rb:

bind 'unix:///srv/vhosts/rumysitename/www/mysitename/tmp/mysitename.sock'

and then run puma referencing that:

puma -C config/puma.rb -e production

You can put a lot more in the config file than just the sock. The puma example config file is a good starting point.

OTHER TIPS

Try following steps.

  • Stop the puma server.
  • Delete the .sock file used by puma.
  • Start the puma server.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top