Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I've made a website with Ruby and FastCGI, and it works great under Apache. I'd like to switch to nginx, but I can't get it working.

The website is made of two processes: one takes care of AJAX calls, the other takes care of the rest.

So far I figured out I should use spawn-fcgi to start my workers and bind them to a socket. Because I'd like to have multiple workers, I also use multiwatch.

The problem is, whenever I try to access to a page, I get a 502 error, and the following line in error.log:

2013/04/03 23:58:39 [error] 1450#0: *1 upstream prematurely closed connection while reading response header from upstream, client: 127.0.0.1, server: v2.localhost, request: "GET / HTTP/1.1", upstream: "http://unix:/run/fastcgi/site-v2.main.sock:/", host: "v2.localhost:8000"

So no worker ever answer the request. netstat -x doesn't display the socket at all, but it seems like it's in use, because if I try to launch my start script twice it says spawn-fcgi: socket is already in use, can't spawn.

One last thing: if I launch my start script and doesn't try to request a page, the multiwatch processes and the workers terminate properly with one kill to multiwatch. But if I request a page, the multiwatch supposed to send the response and all its children workers sort of hang. I have to kill -9 them all.

The site's entry points look like this:

require 'fcgi'

FCGI.each do |req|
    process req
end

My startup script:

USER=www-site-v2
GROUP=www-site-v2

SOCK_MODE=0660

NB_WORKERS_MAIN=4
NB_WORKERS_AJAX=4

MULTIWATCH_BIN=/usr/bin/multiwatch

APP_MAIN=/var/www/site-v2/www/main.rb
APP_AJAX=/var/www/site-v2/www/ajax.rb

PID_MAIN=/run/fastcgi/site-v2.main.pid
PID_AJAX=/run/fastcgi/site-v2.ajax.pid

SOCK_MAIN=/run/fastcgi/site-v2.main.sock
SOCK_AJAX=/run/fastcgi/site-v2.ajax.sock

SPAWN_BIN=/usr/bin/spawn-fcgi
SPAWN_ARGS="-u $USER -g $GROUP -M $SOCK_MODE -d /var/www/site-v2/www"
SPAWN_ARGS_MAIN="-s $SOCK_MAIN -P $PID_MAIN"
SPAWN_ARGS_AJAX="-s $SOCK_AJAX -P $PID_AJAX"

case "$1" in
    start)
        test \! -d /run/fastcgi && mkdir /run/fastcgi

        $SPAWN_BIN $SPAWN_ARGS $SPAWN_ARGS_MAIN -- $MULTIWATCH_BIN -f $NB_WORKERS_MAIN -- $APP_MAIN && \
        $SPAWN_BIN $SPAWN_ARGS $SPAWN_ARGS_AJAX -- $MULTIWATCH_BIN -f $NB_WORKERS_AJAX -- $APP_AJAX
        ;;
    stop)
        test -f $PID_MAIN && kill `cat $PID_MAIN`
        test -f $PID_AJAX && kill `cat $PID_AJAX`
esac

My nginx configuration file:

server {
    listen 8000;
    server_name v2.localhost;

    location ^~ /theme/      { root /var/www/site-v2/www/static; }
    location ^~ /javascript/ { root /var/www/site-v2/www/static; }
    location ^~ /avatars/    { root /var/www/site-v2/www/static; }

    location / {
        include fastcgi_params;
        proxy_redirect off;
        proxy_pass http://unix:/run/fastcgi/site-v2.main.sock:;
    }

    location /ajax/ {
        include fastcgi_params;
        proxy_redirect off;
        proxy_pass http://unix:/run/fastcgi/site-v2.ajax.sock:;
    }
}
share|improve this question

1 Answer 1

up vote 0 down vote accepted

Ok, I'm dumb. For whatever reason, I used proxy_pass instead of fastcgi_pass in my nginx configuration file. So nginx used the wrong protocol, the workers went "wtf?" and hung.

Works like a charm now.

server {
    listen 8000;
    server_name v2.localhost;

    location ^~ /theme/      { root /var/www/site-v2/www/static; }
    location ^~ /javascript/ { root /var/www/site-v2/www/static; }
    location ^~ /avatars/    { root /var/www/site-v2/www/static; }

    location / {
        include fastcgi_params;
        fastcgi_pass unix:/run/fastcgi/site-v2.main.sock;
    }

    location /ajax/ {
        include fastcgi_params;
        fastcgi_pass unix:/run/fastcgi/site-v2.ajax.sock;
    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.