20 Sep

Let’s debug nginx, unicorn errors

This tutorial is particularly intended for nginx, unicorn and rails environment. But you can replace unicorn with any Rake web server i.e. puma, thin, passenger etc. which runs behind nginx since they all communicate with nginx through sock files and these sock files most of the time become root cause of errors.

Hold a mug of coffee/tea and let’s debug your configurations.

Before digging into configurations make sure your nginx and unicorn are running properly. For nginx run following command and check nginx process is running or not

ps aux | grep nginx

For unicorn

ps aux | grep unicorn

If either of nginx or unicorn not running, make them run and check if this was all you needed.

Lets now go through with errors

502 Bad Gateway

One of the most common problem in unicorn nginx configurations is 502 bad gateway. Followings are possible reasons of 502 bad gateway

Sock file path

Root cause of 502 bad gateway is no communication between nginx and unicorn through a shared socket which means nginx cannot find sock file on which unicorn is listening on, check your nginx configurations

...
upstream unicorn_server { 
server unix:/path/to/your/unicorn.sock; 
}
...

sock file path in upstream block should exactly match listen sock file path in your unicorn conf file

....
listen '/path/to/your/unicorn.sock', :backlog => 64
....

If this is different for your configurations, make them same and restart your nginx and unicorn then check error.

Buffer Size

nginx buffer size could be another reason of bad gateway. Open your nginx log with tail and check whether it’s a buffer size issue

tail -f /var/log/nginx/error.log

Reload your home page and see if you get

upstream sent too big header while reading response header from upstream client 

in your nginx log. If Yes then open your nginx conf /etc/nginx/nginx.conf (default path) and add following to in http block

proxy_buffer_size   128k;
proxy_buffers   4 256k;
proxy_busy_buffers_size   256k;

restart nginx and reload page and check error

Permission Denied

If you are getting following response

pm

then probably it is a sock file permission denied issue. Root cause of this error is when nginx cannot read unicorn’s sock file (i.e. when your unicorn sock file is owned by a user who has root or higher permissions then the nginx user)

This could be either solved by changing permission of the sock file so that nginx can read it or increase the permissions of nginx so that it can read it (but this is a bad way). Best way is to create your sock file inside /tmp directory and point nginx to the sock file inside /tmp directory ( if you are on fedora then sock file should be in /var/run/ )

Restart nginx and unicorn and check

if your on centos then you can be victim of running nginx as httpd_t or unconfined_t follow Nginx + Rails + Unicorn Permission Error: ‘sudo nginx’ vs ‘sudo service nginx start’ for details.

If you still facing same error please comment below with your nginx and unicorn logs. I will surely reply at earliest.

my mug is finished … Happy Deployment 😉