3

I am deploying a Rails 2.3 // Spree app with Capistrano/Unicorn/Foreman/Upstart.

The part that I cannot figure out is how to have the /myapp/shared/sockets/unicorn.sock be automatically created by the foreman/upstart process management (at least that I think the unix socket should come from them).

What's responsible for creating the unix socket?

westonplatter
  • 1,475
  • 2
  • 19
  • 30

2 Answers2

1

Let's say your configuration is nginx + unicorn . As you probably know , in the config dir you should create a file named unicorn.rb . In this file there is a description how to handle non-static requests , something like this :

upstream unicapd {
  server unix:/tmp/capd.sock fail_timeout=0;
}

I've named the upstream differently than stated in tutorials , this gives me ability to host a number different applications on the same host .

Then , in your vhosts dir on the Nginx configuration you put something like this (let's say your host file is 'vhosts/myconf.conf':

location @unicorn1 {
proxy_pass http://unicapd;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
   }

Here you see the instruction to nginx to serve non-static requests from the place , named "http://unicapd" , which is related to your unicorn.rb config file . This configuration is triggered by a file , which is in your init.d (in case you're running Debian) directory .

Summary : When you say bundle exec unicorn --restart , the script in your init.d triggers the code , forming a "special" file /tmp/capd.sock , which serves the dynamic content from you Rails app.

R Milushev
  • 4,295
  • 3
  • 27
  • 35
  • can I use the same socket for multiple applications? Like a staging and production instance? – westonplatter Nov 16 '12 at 05:40
  • No , you can't . If you'd like to host more apps on one host , you just name the sockets with different meaningful to you names on your configuration files . – R Milushev Nov 16 '12 at 07:52
  • But about the same app and different environments - I guess you can , because the listener of the socket on the same app uses one and the same socket . – R Milushev Nov 16 '12 at 07:59
  • I did have a nginx/unicorn setup with multistage capistrano. I ended up using different sockets in each stages' /my_app//shared/socksets/unicorn.socket so as not to run into any unix socket issues. I had to non-generically confiugre the Procfile for to pickup a stage/env specific unicorn_.rb file rather than a generic unicorn.rb file. Maybe less than perfect automation, but functionally working. – westonplatter Nov 17 '12 at 19:10
0

Path to unix-socket configured in unicorn's config:
...
listen "/home/user/app/shared/sockets/unicorn.sock", :backlog => 64
...

Then in nginx.conf:

    location / {
            try_files $uri @unicorn;
            proxy_cache cache;
            proxy_cache_valid 10m;
    }        
    location @unicorn {
            proxy_set_header  Client-Ip $remote_addr;
            proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header  Host $host;
            proxy_pass  http://unix:/home/user/apps/shared/sockets/unicorn.sock;
    }

When application will be started, unicorn create socket-file in setted path (user must have write access to this path)

Alexander Randa
  • 868
  • 4
  • 7