skess mousse
skess mousse

Reputation: 105

RoR - nginx issue when trying to deploy with unicorn

I'm beginner in Ruby on Rails and have some difficulties to deploy my rails application (with nginx + unicorn). I don't know what's going on, but here is the kind of errors I get in log files when I launch nginx :

2013/04/14 00:31:42 [error] 14469#0: *1 connect() to unix:/home/user/www/sahitoo/shared/sockets/unicorn.sock 
**failed (111: Connection refused)** while connecting to upstream, client: XX.XXX.XX.XX, server: myapp.com, 
request: "GET / HTTP/1.1", upstream: "http://unix:/home/user/www/sahitoo/shared/sockets/unicorn.sock:/", 
host: "www.XXXXX.com"

If you could help to find out the problem, or at least give me some advices to track it, that would be very nice !! Thanks a lot.

I also post nginx.conf file :

user www-data;
worker_processes 4;
pid /var/run/nginx.pid;

events {
  worker_connections 768;
  multi_accept on;
}

http {
  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;
  keepalive_timeout 65;
  types_hash_max_size 2048;

  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_http_version 1.1;
  gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

  ##
  # Virtual Host Configs
  ##

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

  upstream sahitoo {
    server unix:/home/kar/www/sahitoo/shared/sockets/unicorn.sock;
  }
}

With /etc/nginx/sites-enabled/sahitoo file :

server {
  listen 80;
  server_name myapp.com;

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

  root /www/sahitoo/public;

      # direct to maintenance if this file exists
      if (-f $document_root/system/maintenance.html) {
        rewrite  ^(.*)$  /system/maintenance.html last;
    break;
  }

  location / {
    proxy_redirect          http://sahitoo/               /;
    proxy_set_header        Host                                                            $host;
    proxy_set_header        X-Real-IP                                                     $remote_addr;
proxy_set_header  X-Forwarded-For                                               $proxy_add_x_forwarded_for;

    # If the file exists as a static file serve it directly
    if (-f $request_filename) {
      break;
    }

    if (!-f $request_filename) {
      proxy_pass http://sahitoo;
      break;
    }
  }

  error_page   500 502 503 504  /500.html;
  location = /500.html {
    root   /home/kar/www/sahitoo/public;
  }
}

Upvotes: 0

Views: 3306

Answers (2)

Code Tree
Code Tree

Reputation: 2209

This would happen if you are running ruby on a different user, possibly root and it doesn't have any privilege on current user, are you sure you are getting results from

'ruby -v' or 'rails -v'

.

Upvotes: 1

R Milushev
R Milushev

Reputation: 4315

I would suggest to take a look at this working example nginx.conf :

upstream unicorn-your_app {
  server unix:/tmp/unicorn.your_app.sock fail_timeout=0;
}

server {
  listen 80;
  server_name myapp.com;
  root /www/sahitoo/current/public;

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

  try_files $uri/index.html $uri @unicorn-yourapp;
  location @unicorn-yourapp {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://unicorn-your_app;
  }

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

As you can see there are some differents:

  • upstram block;
  • root points to your_dir/current/public;
  • try_files routine;
  • location @unicorn-yourapp;

If you would like to get a deeper understanding at the topic, there is very nice Railscast.

Upvotes: 0

Related Questions