Nginx – 502 Bad Gateway Error Nginx connect() to unix:/tmp/unicorn.sock failed

nginxopenbsdunix

This is my first question on Server Fault. I could not find a solution to this while searching the web.

I am working on an OpenBSD server running Nginx, which I did not setup, that is throwing a 502 Bad Gateway error in the browser. The Nginx error log shows this error:

014/04/29 09:43:49 [error] 5236#0: *263 connect() to unix:/tmp/unicorn.sock failed (61: Connection refused) while connecting to upstream, client: ###.###.###.###, server: , request: "GET / HTTP/1.1", upstream: "http://unix:/tmp/unicorn.sock:/", host: "sub.domain.com"

Here is the contents of the nginx.conf

user _nginx;
worker_processes 2;
pid /var/run/nginx.pid;
error_log  /var/log/httpd.err debug;

events {
    worker_connections  1024;
}

http {
  include       /etc/nginx/mime.types;
  default_type  application/octet-stream;
  log_format access '$remote_addr - $remote_user [$time_local]  '
                    '"$request" $status $body_bytes_sent $bytes_sent '
                    '"$http_referer" "$http_user_agent" "$sent_http_content_type"';

  access_log /var/log/httpd.log access;
  upload_progress proxied 1m;

  sendfile        on;
  server_name_in_redirect off;

  client_body_timeout   120;
  client_header_timeout 120;
  keepalive_timeout     20;
  send_timeout          120;

  upstream mongrel {
    server unix:/tmp/unicorn.sock;

  }

  gzip on;
  gzip_vary on;
  gzip_min_length  0;
  gzip_comp_level  6;
  gzip_buffers     16 8k;
  gzip_proxied     any;
  gzip_types       text/plain text/javascript text/css text/stylesheet application/x-javascript application/javascript;
  gzip_disable     "MSIE [1-6]\.";

  client_max_body_size 128000M;
  client_body_buffer_size 512k;

  ssl_session_timeout  60m;
  ssl_protocols  SSLv3 TLSv1 TLSv1.1 TLSv1.2;
  ssl_ciphers ECDHE-RSA-AES128-SHA256:AES128-GCM-SHA256:RC4:HIGH:!MD5:!aNULL:!EDH;
  ssl_prefer_server_ciphers   on;
  ssl_session_cache shared:SSL:1m;
  ssl_certificate /etc/ssl/server.crt;
  ssl_certificate_key /etc/ssl/private/server.key;

  server {
    listen 80;
    listen [::]:80;
    listen 443 default ssl;
    listen [::]:443 default ssl;
    charset utf-8;

    root /var/sfta/current/public;

    location ~* ^/(message\/create|dropbox\/create|attachment\/create|attachments) {
      upload_pass @internal_upload;
      upload_resumable on;
      upload_pass_args on;
      upload_store /var/data/tmp;
      upload_state_store /var/data/tmp/resume;
      upload_store_access user:rw group:rw all:rw;
      chunked_transfer_encoding on;

      proxy_redirect     off;
      proxy_set_header   Host               $host;
      proxy_set_header   X-Real-IP          $remote_addr;
      proxy_set_header   X-Forwarded-For    $proxy_add_x_forwarded_for;
      proxy_set_header   X_Forwarded_Proto  $scheme;

      if ($request_method != POST) {
        proxy_pass http://mongrel;
        break;
      }

      # Set specified fields in request body
      upload_set_form_field $upload_field_name.name         "$upload_file_name";
      upload_set_form_field $upload_field_name.content_type "$upload_content_type";
      upload_set_form_field $upload_field_name.path         "$upload_tmp_path";
      upload_aggregate_form_field "$upload_field_name.sha1" "$upload_file_sha1";
      upload_aggregate_form_field "$upload_field_name.size" "$upload_file_size";

      upload_max_output_body_len 0;
      upload_pass_form_field "^authenticity_token$|message|dropbox";
      upload_cleanup 400 404 499 500-505;

      # track uploads in the 'proxied' zone
      # remember connections for 30s after they finished
      track_uploads proxied 120s;
    }

    location ~* ^/(send|messages) {
      rewrite ^(.*)$ /message redirect;
    }

    location / {
      proxy_pass         http://mongrel;
      proxy_redirect     off;
      proxy_set_header   Host               $host;
      proxy_set_header   X-Real-IP          $remote_addr;
      proxy_set_header   X-Forwarded-For    $proxy_add_x_forwarded_for;
      proxy_set_header   X_Forwarded_Proto  $scheme;
      proxy_read_timeout 120;
      proxy_connect_timeout 120;

      # track uploads in the 'proxied' zone
      # remember connections for 30s after they finished
      track_uploads proxied 120s;
    }

    location @internal_upload {
      proxy_pass         http://mongrel;
      proxy_redirect     off;
      proxy_set_header   Host               $host;
      proxy_set_header   X-Real-IP          $remote_addr;
      proxy_set_header   X-Forwarded-For    $proxy_add_x_forwarded_for;
      proxy_set_header   X_Forwarded_Proto  $scheme;
      proxy_read_timeout 120;
      proxy_connect_timeout 120;
    }

    location ~* ^/send {
      rewrite ^(.*)$ /message redirect;
    }

    location ^~ /files/ {
      alias /var/data/files/;
      chunked_transfer_encoding on;
      post_action @protected_done;
      if_modified_since off;
      gzip off;
      internal;
    }

    location @protected_done {
      internal;
      proxy_pass         http://mongrel;
      proxy_set_header   RateBytes        $body_bytes_sent;
      proxy_set_header   Host             $host;
      proxy_set_header   X-Real-IP        $remote_addr;
      proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
      proxy_pass_request_body off;
      proxy_pass_request_headers off;
    }

    location ^~ /images/custom {
      alias /var/data/assets/images;
    }

    location ~* /(javascripts|stylesheets|images)/.*\.(ico|css|gif|js|jp?g|png)(\?[0-9]+)?$ {
      access_log off;
      expires 1w;
      break;
    }

    location = /favicon.ico {
       alias /var/data/assets/images/favicon.ico;
       access_log off;
    }

    location = /alive {
      access_log off;
      return 200;
    }

    location ^~ /progress {
      access_log off;
      report_uploads proxied;
      upload_progress_json_output;
    }

    error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /500.html;
  }
}

I have very little experience with Nginx. Any help is greatly appreciated.

Additional Info:

unicorn.rb

listen '/tmp/unicorn.sock'
worker_processes 2
working_directory "/var/sfta/current"
pid "/var/run/unicorn.pid"
stderr_path "/var/log/unicorn.log"
timeout 120
preload_app true

before_fork do |server, worker|
  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.connection.disconnect!

  old_pid = '/var/run/unicorn.pid.oldbin'
  if File.exists?(old_pid) && server.pid != old_pid
    begin
      Process.kill("QUIT", File.read(old_pid).to_i)
    rescue Errno::ENOENT, Errno::ESRCH
      # someone else did our job for us
    end
  end

  # Throttle the master from forking too quickly by sleeping.
  sleep 1
end

after_fork do |server,worker|
  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.establish_connection

  worker.user('_sfta', '_sfta') if Process.euid == 0
end

Best Answer

That means that Nginx tries to send your stream to an application using the UNIX socket /tmp/unicorn.sock. But it seems that this socket doesn't exist.

Unicorn is a Ruby server. You need to launch you ruby application with unicorn specifying the socket /tmp/unicorn.sock.