Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx cannot find unix socket file with Unicorn (no such file or directory)

I am deploying a Rails 4 app to a Fedora 19 x64 server using Nginx and Unicorn. The problem is that I get an error when visiting the address: "We're sorry, but something went wrong."

My Nginx error log (/var/log/nginx/error.log) shows:

2014/03/08 03:50:12 [warn] 23934#0: conflicting server name "localhost" on 0.0.0.0:80, ignored
2014/03/08 03:50:12 [warn] 23936#0: conflicting server name "localhost" on 0.0.0.0:80, ignored
2014/03/08 03:50:14 [crit] 23939#0: *1 connect() to unix:/tmp/unicorn.[app name].sock failed (2: No such file or directory) while connecting to upstream, client: [client IP], server: localhost, request: "GET /v1/industries/1.xml HTTP/1.1", upstream: "http://unix:/tmp/unicorn.[app name].sock:/v1/industries.json", host: "api.[app name].ca"

As far as I can see from this, Nginx is not aware that the socket exists. However, looking in /tmp, it does:

[root@localhost tmp]# ls
unicorn.[app name].sock

I keep getting stuck at this point, no matter how I modify my Unicorn config file or my Nginx config file. Both are attacted:

/var/www/[app name]/config/unicorn.rb:

working_directory "/var/www/[app name]"
pid "/var/www/[app name]/pids/unicorn.pid"
stderr_path "/var/www/[app name]/log/unicorn.log"
stdout_path "/var/www/[app name]/log/unicorn.log"
listen "/tmp/unicorn.[app name].sock"
worker_processes 2
timeout 30

/etc/nginx/conf.d/default.conf:

upstream app {
    server unix:/tmp/unicorn.[app name].sock fail_timeout=0;
}
server {
    listen 80;
    server_name localhost;
    root /var/www/[app name]/public;
    try_files $uri/index.html $uri @app;
    location @app {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app;
    }
    error_page 500 502 503 504 /500.html;
    client_max_body_size 4G;
    keepalive_timeout 10;
}

The way I have been starting these two daemons is as follows:

unicorn_rails -c /var/www/[app name]/config/unicorn.rb -D -E production
service nginx start

The Unicorn logs contain no relevant information, nor do the production logs. This setup seems straight forward, has anyone experienced this before? Thanks for any help you can provide.

By the way, I was initially following this tutorial: https://www.digitalocean.com/community/articles/how-to-deploy-rails-apps-using-unicorn-and-nginx-on-centos-6-5

like image 320
jamsesso Avatar asked Mar 08 '14 17:03

jamsesso


2 Answers

After many hours and a grand total of 3 beers, I've managed to figure out the problem. After hours of digging, I finally came across this Server Fault answer

In layman terms, it appears that programs that create files in /tmp (or /var/tmp as I have discovered) are the only programs that are able to see the files in that directory. Unicorn was creating the UNIX socket file, however Nginx could not see it.

The solution I have employed is to have Unicorn create sockets in /var/sockets.

like image 119
jamsesso Avatar answered Oct 20 '22 14:10

jamsesso


I suddenly had a similar situation after changing nginx to use a systemd startup service based on their template.

It ends up that the problem was with PrivateTmp=true, which makes it so that nginx was unable to access the socket file created by gunicorn. Once I changed this to PrivateTmp=false the error resolved.

like image 19
n8henrie Avatar answered Oct 20 '22 14:10

n8henrie