{"id":1182,"date":"2026-09-19T22:35:27","date_gmt":"2026-09-19T22:35:27","guid":{"rendered":"https:\/\/virtualserversvps.com\/blog\/?p=1182"},"modified":"2026-09-19T22:35:27","modified_gmt":"2026-09-19T22:35:27","slug":"playbook-diagnosing-502-504-gateway-errors-vps","status":"publish","type":"post","link":"https:\/\/virtualserversvps.com\/blog\/playbook-diagnosing-502-504-gateway-errors-vps\/","title":{"rendered":"Troubleshooting Playbook: Diagnosing 502 and 504 Gateway Errors on a VPS"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">A 502 means nginx reached the upstream but got a bad answer, or could not connect at all. A 504 means nginx gave up waiting for an answer. They look similar in a browser and completely different in a log, and the first two minutes of diagnosis determine whether you fix it in five minutes or fifty. This playbook is a decision tree for the common causes on a self-managed VPS running nginx + PHP-FPM + MariaDB.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Minute 0: Read the Error Log, Not the Access Log<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>tail -50 \/var\/log\/nginx\/error.log\njournalctl -u php8.4-fpm --since '10 min ago' --no-pager | tail -50\ndmesg -T | tail -20   # watch for OOM kills<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Error log line<\/th><th>Meaning<\/th><th>Jump to<\/th><\/tr><\/thead><tbody><tr><td><code>connect() to unix:\/run\/php\/php8.4-fpm.sock failed (2: No such file)<\/code><\/td><td>Wrong socket path or FPM is down<\/td><td>Cause A<\/td><\/tr><tr><td><code>connect() ... failed (13: Permission denied)<\/code><\/td><td>Socket ownership \/ SELinux \/ AppArmor<\/td><td>Cause B<\/td><\/tr><tr><td><code>recv() failed (104: Connection reset by peer)<\/code><\/td><td>Worker died mid-request &mdash; usually OOM or <code>request_terminate_timeout<\/code><\/td><td>Cause C<\/td><\/tr><tr><td><code>upstream timed out (110: Connection timed out)<\/code><\/td><td>504 &mdash; upstream slower than <code>fastcgi_read_timeout<\/code><\/td><td>Cause D<\/td><\/tr><tr><td><code>no live upstreams<\/code><\/td><td>All pool workers busy or dead<\/td><td>Cause E<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Match the line you actually see. Guessing which cause applies is how a five-minute fix becomes an afternoon.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Cause A: Socket Path Mismatch (Most Common After an Upgrade)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Ubuntu upgrades from PHP 8.3 to 8.4 create a new socket path while your nginx config still points at the old one. Confirm what exists and what nginx expects, then reconcile them:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ls -l \/run\/php\/\ngrep -r fastcgi_pass \/etc\/nginx\/\nphp-fpm8.4 -tt 2&gt;&amp;1 | grep -i listen<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The durable fix is to pin the pool to a version-independent path so the next upgrade does not repeat the outage:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \/etc\/php\/8.4\/fpm\/pool.d\/www.conf\nlisten = \/run\/php\/php-fpm.sock\nlisten.owner = www-data\nlisten.group = www-data\nlisten.mode = 0660<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Cause B: Permission Denied on the Socket<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If nginx runs as <code>www-data<\/code> and FPM&#8217;s socket is owned by <code>root:root<\/code> with mode <code>0660<\/code>, every request fails with the same 502. Check without guessing:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ps -o user= -C nginx | sort -u\nstat -c '%U %G %a' \/run\/php\/php-fpm.sock\nsudo -u www-data test -w \/run\/php\/php-fpm.sock &amp;&amp; echo OK || echo DENIED<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>listen.owner<\/code> \/ <code>listen.group<\/code> \/ <code>listen.mode<\/code> directives above fix the common case. On systems with SELinux enforcing, the socket also needs the <code>httpd_sys_rw_content_t<\/code> context &mdash; check <code>sudo ausearch -m avc -ts recent<\/code> before blaming permissions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Cause C: Workers Dying (OOM or Terminate Timeout)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A connection reset almost always means a worker process was killed. Two candidates: the kernel OOM killer, or FPM&#8217;s own <code>request_terminate_timeout<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>journalctl -k | grep -i 'killed process'\ngrep -i 'execution timed out' \/var\/log\/php8.4-fpm.log\nfree -m<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If the OOM killer is involved, the pool is oversubscribed. Cap the FPM pool so that <code>pm.max_children &times; memory-per-worker<\/code> stays inside available RAM minus the database. On a 2 GB VPS with 45 MB workers and a 700 MB MariaDB buffer pool, that is roughly <code>max_children = 18<\/code>, not the 50 many guides suggest. The full derivation is in <a href=\"https:\/\/virtualserversvps.com\/blog\/tuning-php-fpm-process-pools-by-workload\">tuning PHP-FPM pools by workload type<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Cause D: The 504 That Is Really a Slow Query<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Raising <code>fastcgi_read_timeout<\/code> from 60 s to 300 s is the wrong fix &mdash; it converts a visible error into a hung worker that blocks the whole pool. Find out what the request was waiting for:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># what was running at the moment of timeout\nmysql -e \"SELECT id, time, state, LEFT(info,80) FROM information_schema.processlist\n          WHERE time &gt; 5 ORDER BY time DESC LIMIT 10;\"\n\n# PHP slow log, if enabled\ntail -100 \/var\/log\/php8.4-fpm-slow.log<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A long-running <code>SELECT ... WHERE<\/code> with <code>state = Sending data<\/code> points at a missing index or a buffer pool that no longer covers the working set. A <code>Waiting for table metadata lock<\/code> points at a concurrent <code>ALTER TABLE<\/code>. Neither is fixed in nginx. The classification method in our <a href=\"https:\/\/virtualserversvps.com\/blog\/profiling-php-fpm-slowlog-strace-bottlenecks\">PHP-FPM slow-log profiling guide<\/a> will tell you which class you are in within one request.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Cause E: No Live Upstreams (Pool Exhaustion)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When every worker is busy, nginx cannot even hand off the request. Confirm the pool is saturated rather than crashed:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># FPM status page: enable pm.status_path = \/fpm-status first\ncurl -s http:\/\/127.0.0.1\/fpm-status?full | \\\n  grep -E 'active processes|max active|listen queue|max children reached'<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>listen queue<\/code> above zero means requests are already waiting. <code>max children reached<\/code> confirms the pool ceiling is the binding constraint. If both are true and CPU is not saturated, your bottleneck is downstream (database or network I\/O) and a bigger pool will make it worse, not better.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Order of Operations<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Read <code>nginx\/error.log<\/code> and match the exact string &mdash; do not skip this.<\/li>\n<li>Check <code>systemctl status php8.4-fpm<\/code> and socket existence.<\/li>\n<li>Check <code>dmesg<\/code> for OOM kills.<\/li>\n<li>Check <code>information_schema.processlist<\/code> for long queries.<\/li>\n<li>Only then consider timeouts or pool sizes.<\/li>\n<li>Fix the root cause; a timeout bump is a painkiller, not a cure.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">A stable 502\/504-free stack needs the right instance size underneath it. If you are repeatedly hitting pool or memory ceilings after correct tuning, the workload has outgrown the tier &mdash; our <a href=\"https:\/\/virtualserversvps.com\/\">VPS configuration comparison<\/a> shows where the memory and vCPU steps fall.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prevent the Next One<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Pin the FPM socket path so PHP upgrades cannot break the vhost.<\/li>\n<li>Enable the FPM status page bound to <code>127.0.0.1<\/code> and alert on listen-queue depth.<\/li>\n<li>Set <code>request_terminate_timeout<\/code> below <code>fastcgi_read_timeout<\/code> so PHP reports the failure before nginx does.<\/li>\n<li>Alert on OOM kills &mdash; they are silent until someone reports a 502.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>A 502 means nginx reached the upstream but got a bad answer, or could not connect at all. A 504 means nginx gave up waiting for an answer. They look&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1182","post","type-post","status-publish","format-standard","hentry","category-vps-guides-tutorials"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.1 (Yoast SEO v26.1) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Troubleshooting Playbook: Diagnosing 502 and 504 Gateway Errors on a VPS - Virtual Servers VPS Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/virtualserversvps.com\/blog\/playbook-diagnosing-502-504-gateway-errors-vps\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Troubleshooting Playbook: Diagnosing 502 and 504 Gateway Errors on a VPS\" \/>\n<meta property=\"og:description\" content=\"Troubleshooting Playbook: Diagnosing 502 and 504 Gateway Errors on a VPS\" \/>\n<meta property=\"og:url\" content=\"https:\/\/virtualserversvps.com\/blog\/playbook-diagnosing-502-504-gateway-errors-vps\/\" \/>\n<meta property=\"og:site_name\" content=\"Virtual Servers VPS Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-19T22:35:27+00:00\" \/>\n<meta name=\"author\" content=\"Virtual-Servers-Vps-Editor\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Virtual-Servers-Vps-Editor\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/virtualserversvps.com\/blog\/playbook-diagnosing-502-504-gateway-errors-vps\/\",\"url\":\"https:\/\/virtualserversvps.com\/blog\/playbook-diagnosing-502-504-gateway-errors-vps\/\",\"name\":\"Troubleshooting Playbook: Diagnosing 502 and 504 Gateway Errors on a VPS - Virtual Servers VPS Blog\",\"isPartOf\":{\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#website\"},\"datePublished\":\"2026-09-19T22:35:27+00:00\",\"author\":{\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/82a299a8284a66ff49f97c74684724a0\"},\"breadcrumb\":{\"@id\":\"https:\/\/virtualserversvps.com\/blog\/playbook-diagnosing-502-504-gateway-errors-vps\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/virtualserversvps.com\/blog\/playbook-diagnosing-502-504-gateway-errors-vps\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/virtualserversvps.com\/blog\/playbook-diagnosing-502-504-gateway-errors-vps\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/virtualserversvps.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Troubleshooting Playbook: Diagnosing 502 and 504 Gateway Errors on a VPS\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#website\",\"url\":\"https:\/\/virtualserversvps.com\/blog\/\",\"name\":\"Virtual Servers VPS Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/virtualserversvps.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/82a299a8284a66ff49f97c74684724a0\",\"name\":\"Virtual-Servers-Vps-Editor\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d820b15f1cd028e97610d9adf536df7be5cb6423869967037d468d5355fa003f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d820b15f1cd028e97610d9adf536df7be5cb6423869967037d468d5355fa003f?s=96&d=mm&r=g\",\"caption\":\"Virtual-Servers-Vps-Editor\"},\"sameAs\":[\"https:\/\/virtualserversvps.com\/blog\"],\"url\":\"https:\/\/virtualserversvps.com\/blog\/author\/virtualserversvps\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Troubleshooting Playbook: Diagnosing 502 and 504 Gateway Errors on a VPS - Virtual Servers VPS Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/virtualserversvps.com\/blog\/playbook-diagnosing-502-504-gateway-errors-vps\/","og_locale":"en_US","og_type":"article","og_title":"Troubleshooting Playbook: Diagnosing 502 and 504 Gateway Errors on a VPS","og_description":"Troubleshooting Playbook: Diagnosing 502 and 504 Gateway Errors on a VPS","og_url":"https:\/\/virtualserversvps.com\/blog\/playbook-diagnosing-502-504-gateway-errors-vps\/","og_site_name":"Virtual Servers VPS Blog","article_published_time":"2026-09-19T22:35:27+00:00","author":"Virtual-Servers-Vps-Editor","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Virtual-Servers-Vps-Editor","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/virtualserversvps.com\/blog\/playbook-diagnosing-502-504-gateway-errors-vps\/","url":"https:\/\/virtualserversvps.com\/blog\/playbook-diagnosing-502-504-gateway-errors-vps\/","name":"Troubleshooting Playbook: Diagnosing 502 and 504 Gateway Errors on a VPS - Virtual Servers VPS Blog","isPartOf":{"@id":"https:\/\/virtualserversvps.com\/blog\/#website"},"datePublished":"2026-09-19T22:35:27+00:00","author":{"@id":"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/82a299a8284a66ff49f97c74684724a0"},"breadcrumb":{"@id":"https:\/\/virtualserversvps.com\/blog\/playbook-diagnosing-502-504-gateway-errors-vps\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/virtualserversvps.com\/blog\/playbook-diagnosing-502-504-gateway-errors-vps\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/virtualserversvps.com\/blog\/playbook-diagnosing-502-504-gateway-errors-vps\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/virtualserversvps.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Troubleshooting Playbook: Diagnosing 502 and 504 Gateway Errors on a VPS"}]},{"@type":"WebSite","@id":"https:\/\/virtualserversvps.com\/blog\/#website","url":"https:\/\/virtualserversvps.com\/blog\/","name":"Virtual Servers VPS Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/virtualserversvps.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/82a299a8284a66ff49f97c74684724a0","name":"Virtual-Servers-Vps-Editor","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d820b15f1cd028e97610d9adf536df7be5cb6423869967037d468d5355fa003f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d820b15f1cd028e97610d9adf536df7be5cb6423869967037d468d5355fa003f?s=96&d=mm&r=g","caption":"Virtual-Servers-Vps-Editor"},"sameAs":["https:\/\/virtualserversvps.com\/blog"],"url":"https:\/\/virtualserversvps.com\/blog\/author\/virtualserversvps\/"}]}},"_links":{"self":[{"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/posts\/1182","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/comments?post=1182"}],"version-history":[{"count":1,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/posts\/1182\/revisions"}],"predecessor-version":[{"id":1183,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/posts\/1182\/revisions\/1183"}],"wp:attachment":[{"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/media?parent=1182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/categories?post=1182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/tags?post=1182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}