Nginx – How to setup nginx to serve virtualhosts with rails(unicorn/passenger) and php-fpm

amazon ec2nginxphp-fpmruby-on-railsunicorn

I would like to serve multiple sites on one instance.

I install nginx, php-fpm, and a rails app. I use sites like this to guide me.

I configure php-fpm to listen to a local socket

listen = /var/run/php-fpm/php-fpm.sock

I configure ngnix with multiple hosts:

include /etc/nginx/conf.d/*.conf

I have several site php conf files like /etc/nginx/conf.d/site1.conf

server {
    listen 80; 
    server_name site1.com www.site1.com;

    root /var/www/site1;

    access_log /var/log/nginx/site1.com-access.log;
    error_log /var/log/nginx/site1.com-error.log;

    location / { 
        index index.html index.php;
    }   
    location ~ \.php$ {
        fastcgi_pass        unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index       index.php;
        include             fastcgi_params;
        fastcgi_param       PATH_INFO $fastcgi_script_name;
        fastcgi_param       SCRIPT_FILENAME $document_root/$fastcgi_script_name;
    }       
}

and rails site conf files like

upstream rails {
    server 127.0.0.1:3000;
}

server {
    listen 80;
    server_name site2.com www.site2.com;

    access_log /var/log/nginx/site2.com-access.log;
    error_log /var/log/nginx/site2.com-error.log;

    root /var/www/site2;

    location / {
        proxy_pass                       http://rails;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host            $host;
        proxy_set_header X-Url-Scheme    $scheme;
    }
}

I have a unicorn rails server running via rails s -p 3000

Yet, no sites come up for either site1.com or site2.com. I can get to the rails site at www.site2.com:3000

What is wrong? I've spent 2 days (nearly 30hr) trying many different blogs, SO / SF questions, etc. Please share your insight or answer.

edit 1: No log entries are created when I try to visit either site. It's like the requests never come in.

Best Answer

Always check your firewall / proxy / incoming ports.

Everything in the above config files works perfectly fine. The problem was that port 80 wasn't open to inbound traffic.