title: How to fix AWS EC2 Nginx 502 Bad Gateway issue caused after rebooting the server?
body: <p>I have a Python/Flask backend with several Rest APIs. I deployed the program in the AWS EC2 instance. <strong>APIs were worked just fine previously</strong> and for a moment, I stopped the instance and started it again (without doing a single bit of a change). <strong>The problem is, now all the APIs are throwing 502 Bad Gateway Error.</strong> How can I fix this?</p>
  <p><strong>Now APIs are working when I manually run the python file</strong> using</p>
  <pre><code>python3 main.py
  </code></pre>
  <p>and it is running on <code>http://127.0.0.1:5000/</code> on the instance console.</p>
  <p><strong>So it seems like the Nginx is not listening to the Flask server, but not sure!</strong></p>
  <p>/etc/nginx/sites-available/default</p>
  <pre><code>server {
          listen 80;
          listen [::]:80;
          location / {
              proxy_pass http://127.0.0.1:5000;
              proxy_set_header X-Real-IP $remote_addr;
          }
  }
  server {
      listen 443 ssl;
      listen [::]:443 ssl;
      include snippets/self-signed.conf;
      include snippets/ssl-params.conf;
   location / {
              proxy_pass https://127.0.0.1:5000;
              proxy_set_header X-Real-IP $remote_addr;
          }
  }
  </code></pre>
  <p>*<strong>I tried copying the above default file to the sites-enabled as well</strong>. Still not working</p>
  <p>netstat -tunlp</p>
  <pre><code>Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
  tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      -                   
  tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      -                   
  tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      -                   
  tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -                   
  tcp6       0      0 :::443                  :::*                    LISTEN      -                   
  tcp6       0      0 :::80                   :::*                    LISTEN      -                   
  tcp6       0      0 :::22                   :::*                    LISTEN      -                   
  udp        0      0 127.0.0.53:53           0.0.0.0:*                           -                   
  udp        0      0 172.31.37.30:68         0.0.0.0:*                           -  
  </code></pre>
  <p>/var/log/nginx/access.log</p>
  <pre><code>43.250.243.234 - - [26/Mar/2022:21:07:20 +0000] &quot;GET /islive?videoUrl=https://smart-cap.obs.ap-southeast-3.myhuaweicloud.com/1648198646232_1648198646232.webm HTTP/1.1&quot; 502 584 &quot;-&quot; &quot;Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.83 Safari/537.36&quot;
  </code></pre>