Dreamhost, nginx, 502 Bad Gateway and 504 Gateway Time-out

I’m on dreamhost since 2007, and about a year ago they moved me to a VPS… lots of nodes, etc… etc… no problem with that… the problem was that Apache was using lots of memory and 95% of the requests are to static files (images), so I changed from Apage to nginx but since that my sites were always down due to

502 bad gateway

  1. Open your php.ini
    How to know the location? execute a phpinfo() and search for Loaded Configuration File. On DH, it’s at

  2. search for max_execution_time and set it to 90 (probably was 30)
max_execution_time = 90


504 Gateway Time-out

Here’s how I solved my problem…


Add the following lines to /dh/nginx/servers/httpd-psXXXXXX/nginx.conf in http { section

proxy_connect_timeout  60s;
proxy_send_timeout  60s;
proxy_read_timeout  60s;
fastcgi_send_timeout 60s;
fastcgi_read_timeout 60s;

and then restart nginx.

I also added some sh*ts to cron…

#tCGI BandAid on DH
# Version 4, 10-28-2011
# By JuanJose Galvez (DH Tech Support)
# Set this up as a cron job under root
# I normally save this under /root/php_fastcgi.sh
# *     *       *       *       *       /bin/sh /root/php_fastcgi.sh;
# - Added support for PHP 5.3
# - Removed lsof check
# - Writing to log
# - Remove while loops
# - Added user name to log and fixed date -Tyler

#USERS=`ls -la /home/ | grep -v root | awk {'print $3'} | sed '/^$/d'`
USERS=`grep pragma /dh/nginx/servers/*/nginx.conf | awk {'print $3'} | sort | uniq`
for user in $USERS
        echo "User: $user"

        PHPCOUNT=`ps aux | grep '.p*.sock' | grep $user | wc -l`
        echo "PHP Processes: $PHPCOUNT"

        if [ "$PHPCOUNT" -lt 3 ]
            echo "$DATE: Only $PHPCOUNT PHP Processes for $user" >> /root/cron_log.txt
            pkill -9 -f .p*.sock -u $user
            /etc/init.d/nginx startphp
            PHPCOUNT=`ps aux | grep '.p*.sock' | grep $user | wc -l`
crontab -l | { cat; echo "* * * * * /bin/sh ~/nginx_cron.sh"; } | crontab -

sudo /etc/init.d/nginx stop
pkill -u sadulttools
killall -9 php5.cgi
sudo /etc/init.d/nginx start

Usefull links


Leave a Reply

Your email address will not be published. Required fields are marked *