{"id":21918,"date":"2026-02-24T11:07:08","date_gmt":"2026-02-24T08:07:08","guid":{"rendered":"https:\/\/sunucun.com.tr\/blog\/502-bad-gateway-and-504-gateway-timeout-errors\/"},"modified":"2026-11-12T13:20:00","modified_gmt":"2026-11-12T10:20:00","slug":"502-bad-gateway-and-504-gateway-timeout-errors","status":"publish","type":"post","link":"https:\/\/sunucun.com.tr\/blog\/502-bad-gateway-and-504-gateway-timeout-errors\/","title":{"rendered":"What Are 502 Bad Gateway and 504 Gateway Timeout Errors? Causes and Fixes"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_90 ez-toc-wrap-center counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/sunucun.com.tr\/blog\/502-bad-gateway-and-504-gateway-timeout-errors\/#How_Do_502_and_504_Errors_Happen\" >How Do 502 and 504 Errors Happen?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/sunucun.com.tr\/blog\/502-bad-gateway-and-504-gateway-timeout-errors\/#If_You_Are_a_Visitor_What_Can_You_Do_About_a_502_Error\" >If You Are a Visitor: What Can You Do About a 502 Error?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/sunucun.com.tr\/blog\/502-bad-gateway-and-504-gateway-timeout-errors\/#If_You_Run_the_Site_How_to_Fix_a_502_Error\" >If You Run the Site: How to Fix a 502 Error<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/sunucun.com.tr\/blog\/502-bad-gateway-and-504-gateway-timeout-errors\/#1_Check_that_the_backend_service_is_running\" >1. Check that the backend service is running<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/sunucun.com.tr\/blog\/502-bad-gateway-and-504-gateway-timeout-errors\/#2_Read_the_error_log\" >2. Read the error log<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/sunucun.com.tr\/blog\/502-bad-gateway-and-504-gateway-timeout-errors\/#3_Check_resource_usage\" >3. Check resource usage<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/sunucun.com.tr\/blog\/502-bad-gateway-and-504-gateway-timeout-errors\/#4_Check_the_CDN_and_firewall\" >4. Check the CDN and firewall<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/sunucun.com.tr\/blog\/502-bad-gateway-and-504-gateway-timeout-errors\/#How_to_Fix_a_504_Gateway_Timeout\" >How to Fix a 504 Gateway Timeout<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/sunucun.com.tr\/blog\/502-bad-gateway-and-504-gateway-timeout-errors\/#Preventing_502_and_504_Errors\" >Preventing 502 and 504 Errors<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/sunucun.com.tr\/blog\/502-bad-gateway-and-504-gateway-timeout-errors\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> A 502 error (502 Bad Gateway) means the intermediate server that received the request (a gateway, reverse proxy, CDN or load balancer) got an <em>invalid response<\/em> from the server behind it. A 504 error (504 Gateway Timeout) means the intermediate server <em>didn&#8217;t get a response in time<\/em> from that server. Both errors indicate a problem on the server side, not with the visitor.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" src=\"https:\/\/sunucun.com.tr\/blog\/wp-content\/uploads\/2026\/02\/text-hata-kodlari-rehberi-502-bad-gateway-ve-504-gateway-timeout-cozumleri.png\" alt=\"502 Bad Gateway: the gateway server receives an invalid response from the origin server\" \/><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_Do_502_and_504_Errors_Happen\"><\/span>How Do 502 and 504 Errors Happen?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When you open a website, your request often doesn&#8217;t go straight to the application that runs the site. In between there is a reverse proxy such as Nginx, a CDN such as Cloudflare, or a load balancer. This layer forwards the request to the server behind it (PHP-FPM, Node.js, another web server) and sends you the response. The HTTP standard (RFC 9110) defines the two errors as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>502 Bad Gateway:<\/strong> The server, acting as a gateway or proxy, received an invalid response from the server it contacted to fulfil the request.<\/li>\n<li><strong>504 Gateway Timeout:<\/strong> The server, acting as a gateway or proxy, did not receive a timely response from the upstream server.<\/li>\n<\/ul>\n\n\n\n<div class=\"snc-table-wrap\"><table>\n<thead><tr><th><\/th><th>502 Bad Gateway<\/th><th>504 Gateway Timeout<\/th><\/tr><\/thead>\n<tbody>\n<tr><td>What happened?<\/td><td>The backend returned an invalid response or closed the connection<\/td><td>The backend didn&#8217;t respond before the timeout<\/td><\/tr>\n<tr><td>Common causes<\/td><td>PHP-FPM\/application service stopped or crashed, wrong socket\/port, response header too large, firewall block<\/td><td>Long-running query or job, overload, a slow external API, a short timeout setting<\/td><\/tr>\n<tr><td>Where to look first<\/td><td>Service status and the web server error log<\/td><td>Slow queries, resource usage and timeout values<\/td><\/tr>\n<\/tbody><\/table><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"If_You_Are_a_Visitor_What_Can_You_Do_About_a_502_Error\"><\/span>If You Are a Visitor: What Can You Do About a 502 Error?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Because the problem is on the server side, there is little a visitor can do, but these steps help with temporary issues:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Wait a few seconds and reload the page (F5). A 502 often appears while a server is restarting or during a short spike in traffic.<\/li>\n<li>Try the site in another browser, a private window or over mobile data. If it only happens to you, clear your browser cache.<\/li>\n<li>If you see &#8220;error code 502&#8221; in an app (for example a streaming app such as beIN CONNECT), close and reopen the app and make sure it is up to date. If the problem continues, it is most likely on the service&#8217;s own servers; check its announcements or support channel.<\/li>\n<li>If the problem lasts for minutes, let the site owner know.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"If_You_Run_the_Site_How_to_Fix_a_502_Error\"><\/span>If You Run the Site: How to Fix a 502 Error<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1_Check_that_the_backend_service_is_running\"><\/span>1. Check that the backend service is running<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">On servers running Nginx + PHP-FPM, the most common cause of a 502 is that the PHP-FPM service has stopped. The service name depends on the PHP version (for example <code>php8.3-fpm<\/code>):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>systemctl status php8.3-fpm nginx\nsudo systemctl restart php8.3-fpm<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For an application running on Node.js, Python or Docker, apply the same logic: check that the application process is up and listening on the port or socket the proxy forwards to.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2_Read_the_error_log\"><\/span>2. Read the error log<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The web server&#8217;s error log shows the cause fastest:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Nginx\nsudo tail -n 50 \/var\/log\/nginx\/error.log\n# Apache (Debian\/Ubuntu and RHEL-based)\nsudo tail -n 50 \/var\/log\/apache2\/error.log\nsudo tail -n 50 \/var\/log\/httpd\/error_log<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Common lines in the Nginx log and what they mean:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>connect() to unix:\/run\/php\/php8.3-fpm.sock failed (2: No such file or directory)<\/code>: PHP-FPM isn&#8217;t running, or the socket path in the Nginx configuration is wrong.<\/li>\n<li><code>connect() failed (111: Connection refused) while connecting to upstream<\/code>: The backend application isn&#8217;t listening on the given port.<\/li>\n<li><code>upstream prematurely closed connection<\/code>: The backend closed the connection before finishing the response; usually an application error or a memory limit.<\/li>\n<li><code>upstream sent too big header<\/code>: The response headers are larger than the buffer; increase <code>fastcgi_buffer_size<\/code> or <code>proxy_buffer_size<\/code>.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3_Check_resource_usage\"><\/span>3. Check resource usage<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Check CPU and RAM usage with <code>top<\/code> or <code>htop<\/code>, and memory with <code>free -m<\/code>. When memory runs out, the operating system can kill processes, which leads to a 502; you can see this in &#8220;Out of memory&#8221; entries in the <code>dmesg<\/code> output. If resources are constantly tight, the server needs optimization or a plan with more resources.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"4_Check_the_CDN_and_firewall\"><\/span>4. Check the CDN and firewall<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If you use a CDN such as Cloudflare, its error page usually shows whether the problem is at the CDN or at the origin server. A 502 can also appear if the origin server&#8217;s firewall or a security plugin blocks the CDN&#8217;s IP addresses; make sure the CDN IP ranges are allowed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_to_Fix_a_504_Gateway_Timeout\"><\/span>How to Fix a 504 Gateway Timeout<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If the log shows <code>upstream timed out (110: Connection timed out) while reading response header from upstream<\/code>, the backend didn&#8217;t respond within the proxy&#8217;s waiting time. First find the cause of the slowness: slow database queries, large import\/export jobs or an unresponsive external API are the most common. Moving long-running work to the background (a queue or cron) is the lasting fix.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If the job really has to take long, raise the related timeout values together; raising only one isn&#8217;t enough:<\/p>\n\n\n\n<div class=\"snc-table-wrap\"><table>\n<thead><tr><th>Layer<\/th><th>Setting<\/th><\/tr><\/thead>\n<tbody>\n<tr><td>Nginx \u2192 PHP-FPM<\/td><td><code>fastcgi_read_timeout<\/code><\/td><\/tr>\n<tr><td>Nginx \u2192 another application (proxy_pass)<\/td><td><code>proxy_read_timeout<\/code>, <code>proxy_connect_timeout<\/code>, <code>proxy_send_timeout<\/code><\/td><\/tr>\n<tr><td>PHP-FPM pool<\/td><td><code>request_terminate_timeout<\/code><\/td><\/tr>\n<tr><td>PHP<\/td><td><code>max_execution_time<\/code><\/td><\/tr>\n<\/tbody><\/table><\/div>\n\n\n\n<pre class=\"wp-block-code\"><code># Inside an Nginx server or location block\nfastcgi_read_timeout 120s;\n\n# After the change\nsudo nginx -t &amp;&amp; sudo systemctl reload nginx<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">On shared <a href=\"https:\/\/sunucun.com.tr\/hosting\/\" data-internallinksmanager029f6b8e52c=\"126\" title=\"Web bar\u0131nd\u0131rma paketlerini inceleyin\">hosting<\/a> you usually can&#8217;t access these settings; if the problem persists, contact your hosting provider. To manage the settings yourself you need a server with root access.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" src=\"https:\/\/sunucun.com.tr\/blog\/wp-content\/uploads\/2026\/02\/text2-hata-kodlari-rehberi-502-bad-gateway-ve-504-gateway-timeout-cozumleri.png\" alt=\"Server monitoring to prevent 502 and 504 errors\" \/><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Preventing_502_and_504_Errors\"><\/span>Preventing 502 and 504 Errors<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Set up monitoring:<\/strong> Use a tool that watches site availability and server resources so you are alerted when errors start.<\/li>\n<li><strong>Restart services automatically:<\/strong> With systemd settings such as <code>Restart=on-failure<\/code>, a crashed process comes back up on its own.<\/li>\n<li><strong>Size resources correctly:<\/strong> PHP-FPM&#8217;s <code>pm.max_children<\/code> should match the server&#8217;s memory; too low and requests wait, too high and memory runs out.<\/li>\n<li><strong>Move long jobs to the background:<\/strong> Reporting, bulk email and imports shouldn&#8217;t run inside a web request.<\/li>\n<li><strong>Keep software up to date:<\/strong> Keeping the application, plugins and PHP version current reduces known bugs that cause crashes.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A 502 error means the backend server returned an invalid response; a 504 error means it didn&#8217;t respond in time. For visitors, waiting and reloading is usually enough; for site owners, the service status, the error log and the timeout settings quickly reveal the cause. If you want to manage server resources and settings yourself, take a look at our <a href=\"https:\/\/sunucun.com.tr\/en\/vds-server\">VDS server<\/a> options, and for performance help, our <a href=\"https:\/\/sunucun.com.tr\/en\/server-optimization\">server optimization<\/a> service.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short answer: A 502 error (502 Bad Gateway) means the intermediate server that received the request (a gateway, reverse proxy, CDN or load balancer) got an invalid response from the server behind it. A 504 error (504 Gateway Timeout) means the intermediate server didn&#8217;t get a response in time from that server. Both errors indicate&hellip;<\/p>\n","protected":false},"author":1,"featured_media":20357,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[1519],"tags":[],"class_list":["post-21918","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-sanal-sunucu"],"_links":{"self":[{"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/posts\/21918","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/comments?post=21918"}],"version-history":[{"count":0,"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/posts\/21918\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/media\/20357"}],"wp:attachment":[{"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/media?parent=21918"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/categories?post=21918"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/tags?post=21918"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}