Troubleshooting gateway errors and timeouts: 502, 504

Websavers Inc

This article can help you troubleshoot issues related to 504/502/gateway errors. When troubleshooting, it can be helpful to understand how the processing works between nginx and PHP, which is often used for high performance web application hosting, like WordPress.

Common errors that occur that are connected to this configuration:

  • 504 Gateway Timeout
  • Gateway Timeout (504)
  • HTTP Error 504 – Gateway Timeout
  • Gateway Timeout Error
  • 502 bad gateway
  • Bad Gateway

Why is there a gateway?

When a request comes through a web browser, it first hits nginx, our lightweight front-end web server and nginx then decides where the request should go from there, like a train conductor. Nginx will usually handle simpler requests for resources (like images and cached pages) itself, while sending requests for dynamic pages (like a WordPress admin page or shopping cart) through to apache (for modsecurity and .htaccess processing) and then PHP. It looks like this:

nginx -> apache -> php-fpm

nginx acts as a gateway sending the right types of requests through to apache and PHP. If there’s a problem ‘downstream’ — such as with apache or php — you will get a gateway error, like 502 Bad Gateway or 504 Gateway Timeout.

This problem could be extremely slow running code, or an actual issue with the apache or PHP service, though with our hosting the issue is most commonly a code problem.

Use server logs to get more info

To help pinpoint the cause of the gateway error, check the logs to see what’s being reported. sometimes the logs will simply report a 502 or 504 error — just like you’re seeing in the browser — which won’t be helpful. But sometimes you might see an error or warning logged just prior to the Gateway error that will pinpoint what plugin or theme is causing the error.

You can learn how to use Plesk to view logs here. If you can use those errors to get to the bottom of the issue, then definitely do so! Otherwise read on for additional ways to narrow things down.

Help! I can’t even access my website’s admin area! (Like wp-admin for WordPress)


A One-Time Fix for Gateway Errors

If the issue was just a one-time thing (e.g.: you ran a script and it wouldn’t stop running), the solution is to directly restart the underlying services, which forces that script to end.

If you don’t have root access to the server or admin access to Plesk, then you can trigger a restart of PHP services by making a change to your PHP configuration in Plesk. Login to Plesk and visit the “PHP Settings” button for your domain, then make a small change there (like increasing the memory limit by 4MB) then save your changes. This update triggers a restart of apache and PHP processing and could resolve your error! Note: it may take up to 3 minutes (or longer depending on your provider) for the changes to take effect.

If that doesn’t do the trick or the issue returns, you’ll need to go through the following possible causes/symptoms to determine which best fits your case.

Common Causes and Quicker Solutions

Enable a page caching solution

I’m trying to run an export, import, or large report that takes a while

Nearly every request to my site or homepage takes >10s to process

Optimization Plugin using too many resources (examples: JetPack Boost or Hummingbird)

Limited Web Server Buffers (not an issue on Websavers hosting)

PHP-FPM Misconfigured or Needs Tuning (VPS/Dedicated Server Only)

Too many PHP processes needed to serve requests

If you’ve reached this point then it’s likely that your site, as it currently is configured, requires more dedicated PHP processing resources than the existing hosting service provides.

This means your #1 goal must be reducing the amount of dynamic processing that needs to occur on each page load.


If you found this guide helpful, check out the other guides and posts available. If you’re in need of a high-performance Canadian web host or VPS hosting partner, check out our services!

Posted in

Jordan Schelew

Jordan has been working with computers, security, and network systems since the 90s and is a managing partner at Websavers Inc. As a founder of the company, he's been in the web tech space for over 15 years.
WS-Logo-only-image-large

About Websavers

Websavers provides web services like Canadian WordPress Hosting and VPS Hosting to customers all over the globe, from hometown Halifax, CA to Auckland, NZ.

If this article helped you, our web services surely will as well! We might just be the perfect fit for you.

2 Comments

  1. Qasim Abbas on April 14, 2017 at 5:04 am

    @Jordan, had the same issue in my previous website but it is fine now.

Leave a Comment