php-fpm, nginx, bad hoodoo magic, and the dreaded 502 gateway error on magento sites

When segmentation faults collide ….

If anything disrupts nginx’s ability to talk to php-fpm, you’ll see the 502 gateway error. Sometimes, that’s because the server guys did something. Sometimes, that’s because the dev guys did something. Sometimes, it’s really, really, really hard to tell.

If your host assures you it’s your code, and they are positive they have not updated anything on the server (php version, nginx version, anything at all), and more importantly, if the 502 gateway is intermittent and always worse under load, you need to consider your code. It may function, but function so poorly it kills the server.

On Magento sites, look for long running queries. You can stack trace by enabling the zend logger in lib/Varien/Db/Adapter/Pdo/Mysql.php.

One of the biggest coding mistakes in Magento customizations is inefficient queries. Running SQL queries inside a loop, for example, can really put a drain on your server. A long running query can consume so much memory it crashes php-fpm which times out the response from nginx and gives you a 502 gateway error, even though your server is running just fine as far as you can see.

Running SQL queries is a very expensive operation, and doing it in a loop tends to make it even worse. Instead of doing that we should use data collections to load models and then process the items in the collection.

Instead of:

foreach ($this->getProductIds() as $productId){
    $product = Mage::getModel('catalog/product')->load($productId);
    $this->processProduct($product);
}

Do this:

$collection = Mage:getResourceModel('catalog/product_collection')
    ->addFieldsToFilter('entity_id', array($this->getProductIds()))
    ->addAttributeToSelect(array('name'));

foreach ($collection as $product){
    $this->processProduct($product);
}

Especially be on the lookout for queries run through an adapter. Method fetchAll() used to fetch and iterate over larger result sets will lead to a very long execution time (again, with the memory thing and the time out thing and the 502 gateway thing). The better solution is to fetch the results row by row using the fetch() method.

Assuming you declared/initialized your adapter already, instead of this:

$rowSet = $adapter->fetchAll($select);
foreach ($rowSet as $row) {
    //process row
}

Do this:

$query = $adapter()->query($select);

while ($row = $query->fetch()) {
    //process row
}

Leave a Reply

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