Nginx – trying to diagnose weird nginx 110 connection timeout error somehow related to redirection

nginxruby-on-rails

I'm trying to diagnose server timeouts on ubuntu 16.04. I've seen this site http://blogs.candoerz.com/question/161218/cannot-figure-out-nginx–puma-timeout-110-connection-timed-out.aspx
and suggestions given there didn't work. However the site does not address the problem I'm concerned with:

one part of the error log looks particularly strange

upstream: "http://unix:///home/chris/Rails/chrisbim2ree/shared/tmp/sockets/chrisbim2ree-puma.sock/contact_messages"

As far as I understand sockets are files, not directories. Here the log seems to indicate that it is a directory. The other website I mention here has similar pattern. On my system *puma.sock is not a directory. What's wrong here? Problem happens on a part of the site responsible for redirection after submitting the message.

tmp/sockets$ ls -l
total 0
srwxrwxrwx 1 chris chris 0 Jun 21 20:08 chrisbim2ree-puma.sock

my error log.
$ tail -n 2 ./log/nginx.error.log

2016/06/21 20:10:30 [error] 1416#1416: *5 upstream timed out (110: Connection timed out) while reading response header from upstream,

client: 86.22.165.132, server: chrisbeard-images.com, request: "POST
/contact_messages HTTP/1.1", upstream:
"http://unix:///home/chris/Rails/chrisbim2ree/shared/tmp/sockets/chrisbim2ree-puma.sock/contact_messages",
host: "chrisbeard-images.com", referrer:
"http://chrisbeard-images.com/contact"
2016/06/21 20:13:41 [info] 1416#1416: *14 client closed connection while waiting for request, client: 86.22.165.132, server: 0.0.0.0:80

nginx config files

$ sudo cat /etc/nginx/sites-enabled/default 
[sudo] password for chris: 

upstream puma {
  server unix:///home/chris/Rails/chrisbim2ree/shared/tmp/sockets/chrisbim2ree-puma.sock;
}

server {
  listen 80 default_server deferred;
  server_name chrisbeard-images.com www.chrisbeard-images.com;

  root /home/chris/Rails/chrisbim2ree/current/public;
  access_log /home/chris/Rails/chrisbim2ree/current/log/nginx.access.log;
  error_log /home/chris/Rails/chrisbim2ree/current/log/nginx.error.log info;

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri @puma;

  location @puma {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    proxy_pass http://puma;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 70s;
}

another relevant config file

$ sudo cat /etc/nginx/nginx.conf
user www-data;
worker_processes 1;
pid /var/run/nginx.pid;

events { worker_connections 1024; }

http {
        client_max_body_size 2048M;
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 70;
    types_hash_max_size 2048;
    server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    gzip on;
    gzip_disable "msie6";
    gzip_types text/plain text/xml text/css text/comma-separated-values;
        upstream app_server { server 127.0.0.1:8080 fail_timeout=0; }

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

puma config

$ cat ./puma.rb
#!/usr/bin/env puma

directory '/home/chris/Rails/chrisbim2ree/current'
rackup "/home/chris/Rails/chrisbim2ree/current/config.ru"
environment 'production'

pidfile "/home/chris/Rails/chrisbim2ree/shared/tmp/pids/puma.pid"
state_path "/home/chris/Rails/chrisbim2ree/shared/tmp/pids/puma.state"
stdout_redirect '/home/chris/Rails/chrisbim2ree/current/log/puma.error.log', '/home/chris/Rails/chrisbim2ree/current/log/puma.access.log', true


threads 4,16

bind 'unix:///home/chris/Rails/chrisbim2ree/shared/tmp/sockets/chrisbim2ree-puma.sock'

workers 4



preload_app!


on_restart do
  puts 'Refreshing Gemfile'
  ENV["BUNDLE_GEMFILE"] = "/home/chris/Rails/chrisbim2ree/current/Gemfile"
end


on_worker_boot do
  ActiveSupport.on_load(:active_record) do
    ActiveRecord::Base.establish_connection
  end
end

Best Answer

Try this

upstream puma {
  server unix:/home/chris/Rails/chrisbim2ree/shared/tmp/sockets/chrisbim2ree-puma.sock;
}

Here's one I have defined as an example

upstream php-fpm {
  server unix:/var/run/php-fpm/php-fpm.sock;
}

I've never used @ in locations, but I had a quick read about it, nothing obvious seemed off. I'm not sure what you're trying to do with that part, can you explain further by editing your question? It looks a little suspect, but that may just be because I've never used it myself.

I'd try the upstream fix first then see what happens. If it doesn't work edit your original question to add behavior and log entries, and comment below tagging me so I get notified.