<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: The Practical Stack </title>
    <description>The latest articles on DEV Community by The Practical Stack  (@thepraticalstack).</description>
    <link>https://dev.to/thepraticalstack</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4274776%2Ff63d6080-1a97-4540-bc34-054c22abad13.png</url>
      <title>DEV Community: The Practical Stack </title>
      <link>https://dev.to/thepraticalstack</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/thepraticalstack"/>
    <language>en</language>
    <item>
      <title>How to Fix a 502 Bad Gateway in Nginx (Without Guessing)</title>
      <dc:creator>The Practical Stack </dc:creator>
      <pubDate>Tue, 01 Dec 2026 18:19:50 +0000</pubDate>
      <link>https://dev.to/thepraticalstack/how-to-fix-a-502-bad-gateway-in-nginx-without-guessing-3n0f</link>
      <guid>https://dev.to/thepraticalstack/how-to-fix-a-502-bad-gateway-in-nginx-without-guessing-3n0f</guid>
      <description>&lt;p&gt;A 502 means your reverse proxy got the request fine — but the app behind it didn't answer properly. Before touching any config, find out which layer is actually failing. That one habit fixes most 502s in minutes.&lt;/p&gt;

&lt;h2&gt;
  
  
  The 4 causes that cover almost every 502
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;What the log says&lt;/th&gt;
&lt;th&gt;What it usually means&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;connect() failed (111: Connection refused)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The app crashed, is bound to the wrong address, or the port is wrong&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;upstream prematurely closed connection&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;A worker timed out, or the container was killed for running out of memory&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;upstream sent too big header&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Large cookies / auth headers overflow Nginx's default proxy buffers&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;connect() to unix:/run/php/php8.3-fpm.sock failed (13: Permission denied)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The socket file is owned by a different user than Nginx runs as&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Step 1 — Read the error log first
&lt;/h2&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo tail -n 5 /var/log/nginx/error.log
sudo docker ps -a --format "table {{.Names}} {{.Status}} {{.Ports}}"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The log line tells you the exact failure. One tell from the full guide: a container showing &lt;code&gt;Exited (137)&lt;/code&gt; was killed by the Linux OOM killer (128 + 9 SIGKILL). Confirm with &lt;code&gt;dmesg -T | grep -i oom&lt;/code&gt;, then add swap or raise the container's memory limit — restarting it without that just repeats the crash.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2 — The Docker binding trap (the most common one)
&lt;/h2&gt;

&lt;p&gt;Two mistakes cause most Docker-related 502s:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Binding to 127.0.0.1 inside the container.&lt;/strong&gt; Frameworks like Vite, Next.js, Flask and FastAPI often default to loopback, so nothing outside the container can reach them. Bind to &lt;code&gt;0.0.0.0&lt;/code&gt; instead (e.g. &lt;code&gt;uvicorn app:main --host 0.0.0.0 --port 8000&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;proxy_pass http://localhost:3000;&lt;/code&gt; when Nginx itself runs in Compose.&lt;/strong&gt; That points at the Nginx container, not your app. Point it at the Compose service name (&lt;code&gt;http://web-app:3000;&lt;/code&gt;) and use Docker's embedded DNS resolver &lt;code&gt;127.0.0.11&lt;/code&gt; so restarts don't leave Nginx caching a dead IP.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The full guide's production config also raises the timeouts (&lt;code&gt;proxy_connect_timeout 60s&lt;/code&gt;, read/send &lt;code&gt;120s&lt;/code&gt;) and the proxy buffers (&lt;code&gt;proxy_buffer_size 128k&lt;/code&gt;, &lt;code&gt;proxy_buffers 4 256k&lt;/code&gt;) — that's what covers the timeout and oversized-header rows of the table above.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3 — Test, reload, verify
&lt;/h2&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo nginx -t &amp;amp;&amp;amp; sudo systemctl reload nginx
curl -I https://app.example.com/api/v1/health
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;code&gt;nginx -t&lt;/code&gt; catches a syntax mistake before the reload, and the &lt;code&gt;curl -I&lt;/code&gt; should come back &lt;code&gt;HTTP/2 200&lt;/code&gt;. If it doesn't, go back to Step 1 — the log line will have changed, and the new line names the next layer to fix.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Full guide with the complete config, the diagnostic matrix and the terminal walkthrough:&lt;/strong&gt; &lt;a href="https://thepracticalstack.blogspot.com/2026/11/how-to-fix-502-bad-gateway-in-nginx.html" rel="noopener noreferrer"&gt;https://thepracticalstack.blogspot.com/2026/11/how-to-fix-502-bad-gateway-in-nginx.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Originally published on The Practical Stack. This is a companion summary — the full step-by-step guide lives there.&lt;/p&gt;

</description>
      <category>nginx</category>
      <category>docker</category>
      <category>devops</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
