502 Bad Gateway Error: Complete Troubleshooting Guide

Author: Ratul Khan | 17 min read | Sep 16, 2026

A Bad Gateway Error happens when a server acts as a gateway or proxy. It gets an invalid response from an upstream server. This stops visitors from accessing a website. The issue may come from server configuration, PHP-FPM, DNS, CDN, or hosting problems.

This 502 error can affect WordPress, Laravel, Nginx, Apache, and other web apps. Sometimes it is temporary. But frequent errors can hurt website availability. They also harm user experience and search performance.

In this guide, you will learn what a Bad Gateway Error means. You will find out why it happens. And you will see how to troubleshoot and fix it step by step.

What Does 502 Bad Gateway Mean?

The 502 Bad Gateway error is an HTTP status code. It means one server on the internet got an invalid response from another server. This happens while trying to fulfill a request. Think of it like a translator. The translator receives a message but cannot interpret it properly to pass along.

A simplified request may look like this:

Visitor → Browser → CDN/Reverse Proxy → Web Server → PHP/Application → Database

When one server talks to another, the first server expects a valid reply. If that reply is missing, invalid, refused, or unexpected, the gateway may return a 502 error.

Role of web servers, reverse proxies, and upstream servers:

  • Web server: Handles HTTP requests and delivers website content
  • Reverse proxy: Sits in front of web servers. It forwards requests and often handles load balancing, caching, and SSL termination.
  • Upstream server: The backend server that processes the request and generates the response.

Common 502 error messages:

  • ✖️ 502 Bad Gateway
  • ✖️ 502 Proxy Error
  • ✖️ 502 Bad Gateway – The server encountered a temporary error
  • ✖️ 502 Service Temporarily Overloaded

Simply put: the middleman (proxy) could not get a proper response from the destination server.

Common Causes of 502 Bad Gateway

A 502 Bad Gateway error works like a broken phone line between servers. One server tries to reach another. The connection fails. Below are the main reasons this happens. We have grouped them into simple categories.

Web Server Problems

Your web server (Nginx or Apache) is the front door for all visitors. When it fails, nobody can access your site.

  • Nginx or Apache failure: The service might stop working. It could crash. It might fail to restart after updates.
  • Server overload: Too many visitors at once can overwhelm the server. The server then drops connections.
  • Web server misconfiguration: Wrong settings in server files stop proper communication between servers.

PHP-FPM Problems

PHP-FPM (FastCGI Process Manager) runs PHP code on your site. When it fails, dynamic content stops loading.

  • PHP-FPM stopped or crashed: The service may stop without warning. Memory issues or software bugs often cause this.
  • Too many PHP workers: All PHP workers can become busy. New requests then get rejected.
  • PHP process timeout: A script runs too long. This makes the upstream server time out.
  • Incorrect PHP-FPM configuration: Wrong settings like pm.max_children cause problems. Request_terminate_timeout also needs correct values.

Reverse Proxy Issues

Many hosting setups use Nginx as a reverse proxy. It sits in front of Apache or other backends. Proxy problems can trigger 502 errors.

  • Upstream server unavailable: The backend server (like Apache) might be down. It could also be unreachable.
  • Incorrect upstream configuration: Proxy settings in Nginx may point to the wrong address. This includes wrong IP, port, or socket.

Server Overload

When your server runs out of resources, it stops handling new connections.

  • High CPU usage: A sudden CPU spike prevents the server from responding.
  • Insufficient RAM: Running out of memory causes services to crash. They may also become unresponsive.
  • Too many concurrent requests: A traffic surge can overwhelm your server’s capacity.
  • Hosting resource limits: Shared hosting plans have CPU and memory caps. Exceeding these limits causes errors.

DNS Problems

DNS issues can send your domain to the wrong server. Sometimes they send it to no server at all.

  • Incorrect DNS records: Wrong A records or CNAME records point to the wrong IP. Visitors cannot reach your site.
  • DNS changes not propagated: Recent DNS updates take 24-48 hours to spread fully.
  • Domain pointing to the wrong server: Misconfigured DNS entries send traffic to an unprepared server.

Firewall and Security Rules

Security measures sometimes block legitimate connections. They are meant to protect your site.

  • Firewall blocking upstream connections: A firewall rule may stop web server communication with the backend.
  • Cloudflare configuration: Wrong proxy settings or SSL/TLS mismatches cause 502 errors with Cloudflare.
  • Security plugins or server rules: Plugins like Wordfence can block requests. Mod_security rules may also cause issues.

Timeout Issues

Slow processes make the upstream server time out.

  • Slow PHP scripts: Poorly optimized code takes too long to execute.
  • Long database queries: Complex queries with large data sets cause delays.
  • API requests taking too long: External API dependencies slow down your site.
  • Server timeout settings: Very low timeout values cut off normal operations.

Plugin, Theme, or Application Errors

Problems in your software stack also cause 502 errors.

  • WordPress plugin conflicts: Two plugins with overlapping functions cause fatal errors.
  • Theme problems: Buggy themes trigger PHP errors. These errors crash server processes.
  • Laravel/PHP application errors: Custom code errors or memory leaks make PHP fail.
  • Recent software updates: Updating WordPress, plugins, or themes creates compatibility issues. These issues crash the backend.

How to Fix a 502 Bad Gateway Error

This section walks you through a complete troubleshooting process. Follow these steps in order. They will help you find and fix the root cause.

Step 1: Refresh the Page

Start with the simplest solution. Many 502 errors are temporary glitches. They come from network congestion or brief server overloads.

  • Perform a hard refresh – On Windows/Linux: Ctrl + F5. On Mac: Cmd + Shift + R. This forces your browser to skip the cache. It requests a fresh copy of the page.
  • Try another browser or device – Open the page on your phone. If it works, the issue is with your desktop browser. If it fails everywhere, the problem is on the server side.
  • Check if the problem is temporary – Wait 2-3 minutes and refresh again. Sometimes a traffic spike or restarting service causes a brief 502. It often resolves itself.

Step 2: Check if the Server Is Down

If the error persists, the issue may be with your hosting provider.

  • Check hosting/server status – Log into your hosting dashboard. Check your provider’s status page. Many providers show real-time server health.
  • Test other pages on the website – If only one page shows a 502, the problem is likely a script or plugin on that page. If the whole site is down, the problem is server-wide.
  • Check other websites on the same server – On shared hosting, see if other sites on your server are also down. If yes, contact your hosting provider right away.

Step 3: Check Server Resource Usage

Many 502 errors happen when the server runs out of resources.

  • Check CPU usage – If CPU usage stays above 80-90%, your server is overloaded. Consider upgrading your hosting plan. You can also optimize your code.
  • Check RAM usage – High RAM usage forces the server to use swap memory. This slows performance dramatically. It can trigger 502 errors.
  • Check disk space – A full disk causes services to crash. Free up space by removing old logs, backups, or unnecessary files.
  • Check active processes – Too many processes exhaust server resources. Use top or htop on Linux. On Windows, use Task Manager to monitor process counts.
  • Check hosting resource limits – Shared hosting has CPU, memory, and entry process limits. Exceeding these limits triggers 502 errors. Your hosting dashboard shows resource usage.

Step 4: Restart Nginx or Apache

Sometimes a simple restart solves the problem.

  • When restarting helps – Did you change a configuration? The change may not be active yet. Is a service not responding? Restarting can help.
  • Check configuration before restarting – Always test your config first. For Nginx: sudo nginx -t. For Apache: sudo apachectl configtest. This prevents taking the server offline with broken settings.
  • Verify the website after restart – After restarting, visit your site. Confirm the error is resolved.

Commands:

  • Nginx: sudo systemctl restart nginx
  • Apache: sudo systemctl restart apache2

Step 5: Check PHP-FPM

WordPress and many dynamic sites rely on PHP-FPM. When it stops or gets overloaded, you see 502 errors.

  • Confirm PHP-FPM is running – Use sudo systemctl status php8.1-fpm. Adjust the version number as needed.
  • Restart PHP-FPM – Run sudo systemctl restart php8.1-fpm. This often fixes memory leaks or stuck processes.
  • Check PHP-FPM logs – Look at /var/log/php-fpm.log. Also check /var/log/php for errors.
  • Check worker limits – Too few workers means PHP-FPM cannot handle requests. Increase the pm.max_children value in your PHP-FPM pool configuration.

Step 6: Check Nginx/Apache Configuration

Misconfigured upstream settings or proxy rules cause 502 errors.

  • Review upstream settings – In Nginx, check the upstream block. It must point to the correct backend server (usually 127.0.0.1 or a specific IP).
  • Check proxy configuration – Ensure proxy_pass in Nginx or ProxyPass in Apache uses the right address and port.
  • Verify ports and sockets – Check that the port (like 9000 for PHP-FPM) or socket file path is correct. Confirm the backend service is listening.
  • Test configuration files – Always test before restarting. Use sudo nginx -t or sudo apachectl configtest. Fix any errors before restarting.

Step 7: Review Server Error Logs

Logs tell you exactly what went wrong.

  • Nginx logs – Look at /var/log/nginx/error.log for upstream timeout or connection refused messages.
  • Apache logs – Check /var/log/apache2/error.log (Ubuntu) or /var/log/httpd/error_log (CentOS).
  • PHP-FPM logs – Check /var/log/php-fpm.log for worker exhaustion or script errors.
  • Application logs – For WordPress, check wp-content/debug.log. Other applications have their own error logs.
  • How to identify the actual error – Look for recurring error messages. “Upstream timed out” means a slow script. “Connection refused” means the backend service is not running. “No input file specified” often points to a misconfigured path or missing file.

Step 8: Check DNS Settings

DNS misconfigurations cause 502 errors.

  • Verify A/AAAA records – Ensure your domain’s A record points to the correct server IP.
  • Confirm the domain points to the correct server – Use ping yourdomain.com or nslookup yourdomain.com. Verify the IP matches your server’s IP.
  • Check recent DNS changes – DNS changes take up to 48 hours to propagate fully. The 502 may come from your domain pointing to an old or incorrect server.

Step 9: Check Cloudflare or CDN Settings

If you use Cloudflare or another CDN, their settings can cause 502 errors.

  • Verify DNS configuration – Ensure your DNS settings in Cloudflare point to the correct origin server IP.
  • Check SSL/TLS mode – SSL/TLS misconfiguration (like Full vs Flexible) causes connection issues between Cloudflare and your origin server.
  • Check origin server connectivity – Use Cloudflare’s “Test” feature. It verifies the origin server is reachable from Cloudflare’s network.
  • Temporarily bypass CDN for testing – Put your site in “Development Mode” in Cloudflare. This bypasses the CDN. If the error clears, the issue is CDN-related.

Step 10: Check WordPress Plugins and Themes

If your 502 error appears after a recent update or plugin install, the conflict may be WordPress-related.

  • Disable recently installed plugins – Deactivate plugins one by one. Use FTP or File Manager if you cannot access the admin dashboard. This helps identify the culprit.
  • Test with a default theme – Temporarily switch to a default WordPress theme (like Twenty Twenty-Four). If the error clears, your theme is the issue.
  • Check WooCommerce-related processes – WooCommerce uses significant server resources. If you see 502s after WooCommerce updates, increase PHP memory limits or worker processes.
  • Review plugin error logs – Many plugins write errors to wp-content/debug.log. Enable debugging in wp-config.php to capture the specific plugin causing the problem.

How to Fix 502 Bad Gateway in WordPress

A 502 error in WordPress means your web server, like Nginx or Apache, could not get a valid response from the backend server. That backend server is usually PHP-FPM. This is a communication breakdown between servers. Here is how to fix it step by step.

Check Hosting Resources

Server overloads are a top cause of 502 errors. Check your hosting control panel for resource usage. Look at CPU, RAM, and memory. If any are maxed out, your server cannot handle the load. Contact your host to upgrade resources if you need more power.

Disable Problematic Plugins

Poorly coded or conflicting plugins often trigger 502 errors . Bulk-deactivate all plugins from your WordPress dashboard. If the error disappears, reactivate them one by one to find the culprit. If you can’t access the admin panel, rename the /wp-content/plugins/ folder via FTP to disable them all at once.

Increase PHP Limits & Check PHP-FPM

Exhausted PHP-FPM workers cause 502 errors when they can’t keep up with requests . Increase pm.max_children and pm.max_requests in your PHP-FPM config (e.g., /etc/php/8.x/fpm/pool.d/www.conf). Also, raise the memory_limit in wp-config.php to 256M or higher .

Review WordPress Debug Logs

Enable debugging by adding to wp-config.php:

php

define(‘WP_DEBUG’, true);

define(‘WP_DEBUG_LOG’, true);

Check /wp-content/debug.log for plugin conflicts or PHP errors that reveal the root cause..

Check Database Performance

If PHP-FPM is fine, the database may be the bottleneck. Slow queries or locked tables can hang PHP processes until they time out . Use a plugin like Query Monitor to spot slow queries or ask your host to review MySQL performance.

Verify CDN/Cloudflare Settings

Sometimes CDN or firewall settings cause problems. They can interfere with server responses and trigger 502 errors. Try temporarily pausing Cloudflare or your CDN. If the error resolves, check your firewall rules. Also review your CDN configuration for issues.

How to Fix 502 Bad Gateway in Laravel

A 502 Bad Gateway error in Laravel usually means Nginx cannot talk to PHP-FPM. This often happens because of a missing PHP-FPM socket. It can also come from exhausted server resources or wrong Nginx settings. Use this systematic checklist to diagnose and fix the issue.

Step-by-Step Checklist to Fix the 502 Error in Laravel

  1. Check storage/logs/laravel.log The first place to look. It often contains specific error messages or clues about what’s breaking.
  2. Verify PHP-FPM – This is the most common fix. If PHP-FPM is down, Nginx can’t process PHP requests. Restart it: sudo service php8.x-fpm restart or use your server’s control panel (like Laravel Forge) to restart the service .
  3. Check Nginx Configuration – Have you modified your Nginx config recently? An error in the config could have caused a restart to fail. This can take PHP-FPM down with it. Verify your config syntax with nginx -t .
  4. Check .env Settings – Ensure your .env file is correctly configured, especially database credentials, as a connection failure can sometimes cascade into a 502 error .
  5. Clear Laravel Cache – Run php artisan config:cache, php artisan route:cache, and php artisan view:cache to ensure your application’s cached configurations aren’t pointing to old or invalid settings .
  6. Check Queue Workers – Too many queue workers (like Laravel Horizon) can consume all available memory, causing PHP-FPM to crash. Review your horizon.php config and ensure the maxProcesses setting is appropriate for your server’s memory .
  7. Review Server Resource Usage –If you have not made any changes, the server may simply be underpowered. It is often running out of RAM. Think about increasing your server’s memory.

How to Fix 502 Bad Gateway in Nginx

Getting a 502 Bad Gateway error with Nginx can be frustrating. But it usually means Nginx cannot connect to your upstream server, like PHP-FPM. Here is a step-by-step troubleshooting guide to help you fix it.

Step-by-Step Troubleshooting

Check Nginx Status
First, ensure Nginx itself is running:

bash

sudo systemctl status nginx

If inactive, start it: sudo systemctl start nginx 

Test Nginx Configuration
Before making any changes, test for syntax errors:

bash

sudo nginx -t

If it reports a syntax error, fix the issue in your configuration files before proceeding .

Verify Upstream Server
For PHP-FPM, check if the service is running:

bash

sudo systemctl status php8.1-fpm   # Replace version with your PHP version

If inactive, start it: sudo systemctl start php8.1-fpm 

Check PHP-FPM Socket/Port
Nginx may use a Unix socket, like /var/run/php/php8.1-fpm.sock. Make sure the socket file exists and has the right permissions. For a TCP connection on port 9000, check that the port is listening: 

bash

ss -lntp | grep :9000

Ensure the communication method in Nginx matches the PHP-FPM configuration .

Review Nginx Error Logs
The error log is your best friend for finding the root cause. Tail the log:

bash

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

Look for specific errors like connect() failed, upstream timed out, or Permission denied. These logs will point you to the exact issue .

Restart Services
After fixing the configuration, restart both PHP-FPM and Nginx for changes to take effect:

bash

sudo systemctl restart php8.1-fpm

sudo systemctl reload nginx

How to Fix 502 Bad Gateway in Apache

Here are the steps to fix the 502 error in Apache:

Check Apache Status
First, confirm Apache is running. On CentOS/RHEL use systemctl status httpd; on Debian/Ubuntu use systemctl status apache2 . Restart it with sudo systemctl restart httpd if it’s stopped.

Review Apache Error Logs
Check the logs—they’re your best friend for diagnosis. The default location is /var/log/httpd/error_log or /var/log/apache2/error.log . Use sudo tail -f /var/log/httpd/error_log to see real-time errors like “(111) Connection refused” .

Check PHP Configuration
When Apache forwards requests to PHP-FPM, the issue is often there. Check the service status (systemctl status php-fpm) and its logs (journalctl -xeu php-fpm) . Ensure the PHP-FPM pool is running .

Verify Proxy Modules
If Apache acts as a reverse proxy, the right modules must be loaded. Run httpd -M | grep proxy to list loaded modules. You need mod_proxy and mod_proxy_http at minimum . Enable missing modules in httpd.conf.

Check Backend/Upstream Services
Since a 502 means the backend is unreachable, directly test the connection . Use ss -lntp | grep 9000 to verify the backend is listening . Use curl http://127.0.0.1:9000 to test the connection.

Restart Apache
After any configuration changes, restart Apache using sudo systemctl restart httpd (CentOS) or sudo systemctl restart apache2 (Debian/Ubuntu) to apply the fixes .

502 Bad Gateway vs Other Server Errors

Understanding the differences helps you diagnose issues faster and communicate more effectively with your hosting provider. Here is a quick breakdown of the most common server errors you will encounter:

Error What It Usually Means
500 Internal Server Error Something broke on the server itself—a script crashed, a permission issue, or an application error. The server knows something is wrong but cannot pinpoint exactly what.
502 Bad Gateway Your server (the gateway) received an invalid response from an upstream server (like your application server or a proxy). One server is talking, but the other is not answering correctly.
503 Service Unavailable The server is temporarily overloaded or down for maintenance. Try again later.
504 Gateway Timeout Your server sent a request to an upstream server, but that server took too long to reply. The gateway gave up waiting.

How to Prevent 502 Errors

Prevention is always better than troubleshooting under pressure. While 502 errors can strike without warning, you can reduce their frequency. Here are key preventive measures every site owner should implement.

  • Use reliable hosting – Choose a provider with solid uptime records and responsive support.
  • Monitor CPU and RAM – Track server resource usage. This lets you scale before overload occurs.
  • Keep PHP and server software updated – Stay current with security patches and performance improvements.
  • Configure PHP-FPM correctly – Ensure process manager settings match your traffic volume. Check pm.max_children and pm.start_servers.
  • Optimize database queries – Index slow queries. Avoid complex joins that can time out.
  • Use caching – Implement object caching with Redis or Memcached. Use page caching to reduce server load.
  • Use a CDN when appropriate – Offload static assets. This reduces direct server pressure.
  • Monitor server logs – Review error logs regularly. Spot recurring warning signs before they escalate.
  • Avoid poorly coded plugins – Review plugins for efficiency. This matters most on high-traffic sites.
  • Set appropriate timeout values – Adjust proxy_read_timeout and fastcgi_read_timeout. Match them to your application’s response times.

When Should You Contact Your Hosting Provider?

Sometimes you need expert help. Contact your hosting provider when the problem is too big to fix alone.

Contact support if:

  • Server services repeatedly crash
  • PHP-FPM cannot be restarted
  • Resource limits are constantly reached
  • You do not have server configuration access
  • Multiple websites are affected
  • The server remains unreachable
  • Gateway errors continue after application-level troubleshooting

Give your provider these details:

  • The time the error started
  • Which URLs are affected
  • Any recent changes you made
  • Screenshots (if helpful)
  • Important log messages

This information can help support teams identify infrastructure-level problems faster.

Frequently Asked Questions

What causes a 502 Bad Gateway error?

A 502 error happens when one server gets a bad response from another server. The gateway server sends a request. The upstream server sends back an invalid reply. This often means server overload, setup mistakes, or PHP-FPM problems.

How do I fix a 502 error on WordPress?

Start by raising PHP memory limits. Turn off plugins to find conflicts. Check that your WordPress version is current. Then restart PHP-FPM or your web server.

Can Cloudflare cause a 502 error?

Yes. Cloudflare works as a reverse proxy. If the link between Cloudflare and your origin server breaks—because of firewall rules, SSL issues, or server overload—Cloudflare may show a 502 error.

Is a 502 error caused by DNS?

No. DNS turns your domain into an IP address. A 502 error happens after the connection is made. The server just cannot send a valid response.

How do I fix 502 Bad Gateway in Nginx?

Look at your Nginx error logs. Increase proxy timeout and buffer settings. Make sure your PHP-FPM service is running. Double-check upstream server settings.

Can PHP-FPM cause a 502 error?

Yes. If PHP-FPM crashes, runs out of processes, or takes too long, Nginx or Apache gets no reply. This triggers a 502 error.

What is the difference between 502 and 503 errors?

A 502 error means a server got an invalid response. A 503 error means the server is temporarily unavailable. This often happens during maintenance or heavy load.

Why does my website show 502 after a server restart?

Services like PHP-FPM or MySQL may not start correctly. Or the web server may start before upstream services are ready. Restart each service by hand to fix this.

Can high traffic cause a 502 Bad Gateway error?

Yes. Sudden traffic spikes can use up all PHP-FPM processes. They can also drain database connections or server resources. This causes timeouts and 502 errors.

Conclusion

A Bad Gateway Error usually means a gateway or proxy could not get a valid response from an upstream server. It might be temporary. But repeated 502 errors often point to server, hosting, or application problems.

Check Nginx or Apache first. Look at PHP-FPM too. Monitor CPU and RAM usage. Review DNS and CDN settings. Check your application logs. For WordPress and Laravel, also look at recent plugin changes. Review theme updates. Check code or configuration changes as well.

If the error keeps coming back, server logs can help. They show which service is failing. Fix the root cause to stop the error. Then monitor your server regularly. This helps keep your website stable and available.

Seeing 502 errors again and again? Talk to our hosting experts today. We offer stable hosting, server monitoring, and expert fixes. We keep your site fast, stable, and online.

Share this article: Facebook X (Twitter) LinkedIn
Ratul Khan
✓ Author

Ratul Khan

Leave a Comment

Leave a Reply

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

🔗 Link copied!