Also, using unix sockets is slightly faster since it provides you direct network access without any TCP/IP overhead. On the down side, it is not as scalable as TCP/IP. Nginx will throw 502 errors when the sockets have been depleted. In such a case you can either tweak the OS settings to accommodate the larger connection pool or just switch to switch to TCP/IP.

In your fastcgi conf change:

fastcgi_pass unix:/var/run/php5-fpm.sock;

to:

fastcgi_pass 127.0.0.1:9000;

Note that port 9000 is the default port set in php-fpm, if you have changed php-fpm to listen on another port then swap 9000 with that value. Make sure you restart both php-fpm and nginx.

Now, if after all of this, you still cannot get it to work and free -m returns high memory usage, then it is time to add more ram to your server.

Thanks for the reply. I will make the configuration changes you suggested and i will let you know later.
– vsapountzisAug 3 '14 at 17:23

Hi again. Thanks for the help. Your solution worked perfectly but although i got rid of the 502 error i am experiencing 504 timeouts and free -m is reporting 1.4GB free out of 2GB available. TCP/IP configuration produces this error or is it unrelated?
– vsapountzisAug 3 '14 at 19:13

The 504 is nginx waiting for a reply from php-fpm but not receiving one within X amount of time. Could be due to many reasons. First, you should check your nginx error log, should give you some error output. You should also consider increasing the nginx timeout. You can try setting fastcgi_send_timeout and fastcgi_read_timeout in nginx.conf somewhere between 30s and 60s and see whether that does the trick.
– RijndaelAug 3 '14 at 21:52

Hi again. I eventually changed the timeout in my nginx.conf and php.ini file to 60s and i am still experiencing 504. Any ideas @Rijndael?
– vsapountzisAug 6 '14 at 8:22

Try setting request_terminate_timeout=30s in nginx.conf and max_execution_time=30 in your php-fpm conf. The latter should already be present. Make sure you restart both nginx and php-fpm.
– RijndaelAug 6 '14 at 11:43